{"id":71,"date":"2024-04-23T00:58:58","date_gmt":"2024-04-23T00:58:58","guid":{"rendered":"https:\/\/www.rajeshkumar.xyz\/blog\/?p=71"},"modified":"2024-04-23T00:58:58","modified_gmt":"2024-04-23T00:58:58","slug":"how-to-send-an-email-from-aws-ec2-or-azure-vm-or-google-instance-using-mail-or-sendmail-linux-commands","status":"publish","type":"post","link":"https:\/\/www.rajeshkumar.xyz\/blog\/how-to-send-an-email-from-aws-ec2-or-azure-vm-or-google-instance-using-mail-or-sendmail-linux-commands\/","title":{"rendered":"How to Send an email from AWS ec2 or Azure VM or Google Instance using mail or sendmail linux commands"},"content":{"rendered":"\n<p>Sending an email directly from cloud virtual machines (VMs) like AWS EC2, Azure VM, or Google Cloud Compute Engine using command-line tools such as <code>mail<\/code> or <code>sendmail<\/code> can be a straightforward task, but it does require some initial setup and consideration of the cloud provider\u2019s policies and limitations. Here\u2019s a general guide on how to set up and send an email using these commands on a cloud VM:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">General Steps for Setup<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install Mail Utilities<\/strong>: Depending on the Linux distribution, you might need to install mail utilities. Common utilities include <code>mailx<\/code>, <code>sendmail<\/code>, or <code>postfix<\/code>.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">For Ubuntu\/Debian systems:\n\n$ sudo apt update\n$ sudo apt install mailutils\n\nFor RedHat\/CentOS systems:\n\n$ sudo yum update\n$ sudo yum install mailx<\/code><\/span><\/pre>\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Configure SMTP<\/strong>: To send emails, you need to configure an SMTP server. You can use your cloud provider\u2019s email service (like Amazon SES, Google&#8217;s SMTP relay, or SendGrid) or any external SMTP service.<\/li>\n\n\n\n<li><strong>Edit Configuration Files<\/strong>: If using <code>sendmail<\/code> or <code>postfix<\/code>, you\u2019ll need to configure them to send emails via your chosen SMTP server. This typically involves editing <code>\/etc\/postfix\/main.cf<\/code> or similar files to set up SMTP relay settings.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Using AWS SES, Google SMTP, or SendGrid<\/h2>\n\n\n\n<p>For simplicity, here\u2019s how you could configure using an external SMTP like AWS SES:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Configure SMTP Settings<\/strong>: Modify your <code>postfix<\/code> configuration to use AWS SES:<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">sudo postconf -e <span class=\"hljs-string\">'relayhost = &#91;email-ses-region.amazonaws.com]:587'<\/span>\nsudo postconf -e <span class=\"hljs-string\">'smtp_sasl_auth_enable = yes'<\/span>\nsudo postconf -e <span class=\"hljs-string\">'smtp_sasl_security_options = noanonymous'<\/span>\nsudo postconf -e <span class=\"hljs-string\">'smtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd'<\/span>\nsudo postconf -e <span class=\"hljs-string\">'smtp_use_tls = yes'<\/span>\nsudo postconf -e <span class=\"hljs-string\">'smtp_tls_security_level = encrypt'<\/span>\nsudo postconf -e <span class=\"hljs-string\">'smtp_tls_note_starttls_offer = yes'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol class=\"wp-block-list\">\n<li>Replace <code>email-ses-region.amazonaws.com<\/code> with the appropriate endpoint for your SES instance.<\/li>\n\n\n\n<li><strong>Create SASL Password File<\/strong>: You need to create a file to store your SES credentials.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">sudo sh -c <span class=\"hljs-string\">'echo \"&#91;email-ses-region.amazonaws.com]:587 USERNAME:PASSWORD\" &gt; \/etc\/postfix\/sasl_passwd'<\/span>\nsudo postmap \/etc\/postfix\/sasl_passwd\nsudo chmod <span class=\"hljs-number\">600<\/span> \/etc\/postfix\/sasl_passwd\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol class=\"wp-block-list\">\n<li>Replace <code>USERNAME<\/code> and <code>PASSWORD<\/code> with your SES SMTP credentials.<\/li>\n\n\n\n<li><strong>Reload\/Restart Postfix<\/strong>:<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">sudo systemctl restart postfix\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Send an Email<\/h3>\n\n\n\n<p>After configuration, you can send an email using the <code>mail<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo \"This is the body of the email\" | mail -s \"Subject Here\" user@example.com<br><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sending an email directly from cloud virtual machines (VMs) like AWS EC2, Azure VM, or Google Cloud Compute Engine using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":1,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}