{"id":17068,"date":"2024-05-24T06:27:08","date_gmt":"2024-05-24T06:27:08","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=17068"},"modified":"2024-05-24T07:41:49","modified_gmt":"2024-05-24T07:41:49","slug":"how-to-send-email-using-shell-script","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2024\/05\/24\/how-to-send-email-using-shell-script\/","title":{"rendered":"How to Send Email using Shell Script (gmail)."},"content":{"rendered":"\n<p>In this article, we will learn how to send email using shell scripting. Sending emails programmatically can be very useful for automated notifications, error alerts, or any other form of communication that needs to be sent without manual intervention. Shell scripting offers a powerful way to achieve this through various utilities and tools available in Unix-like operating systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AWS Account with Ubuntu 24.04 LTS EC2 Instance.<\/li>\n\n\n\n<li>Basic knowledge of Shell scripting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #1:Generate the App Password<\/strong><\/h3>\n\n\n\n<p>First go to your account:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"489\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/8-3-1024x489-1.png\" alt=\"\" class=\"wp-image-17075\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/8-3-1024x489-1.png 1024w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/8-3-1024x489-1-300x143.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/8-3-1024x489-1-768x367.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Search for\u00a0<strong>App Password<\/strong>\u00a0in the search bar.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"317\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/9-4.png\" alt=\"\" class=\"wp-image-17076\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/9-4.png 905w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/9-4-300x105.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/9-4-768x269.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Enter your google account password to verify it\u2019s you:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"475\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/3-5-1024x475-1.png\" alt=\"\" class=\"wp-image-17077\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/3-5-1024x475-1.png 1024w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/3-5-1024x475-1-300x139.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/3-5-1024x475-1-768x356.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Now Enter the app name for which you wanted to get the app password like Shell Script.<\/p>\n\n\n\n<p>And click on Create to create it:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"492\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/10-2-1024x492-1.png\" alt=\"\" class=\"wp-image-17078\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/10-2-1024x492-1.png 1024w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/10-2-1024x492-1-300x144.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/10-2-1024x492-1-768x369.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>the password will be generated. Note it down cause will be using it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"560\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/11-2.png\" alt=\"\" class=\"wp-image-17079\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/11-2.png 675w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/11-2-300x249.png 300w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #2:Create a file in Ubuntu<\/strong><\/h3>\n\n\n\n<p>Open the terminal and use the\u00a0<strong>nano\u00a0<\/strong>command to create a new file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>nano email.sh<\/strong>\n\n<strong>Step #3:Write a Script to Send Emails<\/strong>\nBelow is a basic script to send an email.\n-----------------------------------------\n\n<strong>#!\/bin\/bash\n\n# Prompt the user for input\nread -p \"Enter your email: \" sender\nread -p \"Enter recipient email: \" receiver\nread -s -p \"Enter your Google App password: \" gapp\necho\nread -p \"Enter the subject of mail: \" sub\n\n# Read the body of the email\necho \"Enter the body of mail (Ctrl+D to end):\"\nbody=$(&lt;\/dev\/stdin)\n\n# Sending email using curl\nresponse=$(curl -s --url 'smtps:\/\/smtp.gmail.com:465' --ssl-reqd \\\n    --mail-from \"$sender\" \\\n    --mail-rcpt \"$receiver\" \\\n    --user \"$sender:$gapp\" \\\n    -T &lt;(echo -e \"From: $sender\\nTo: $receiver\\nSubject: $sub\\n\\n$body\"))\n\nif &#91; $? -eq 0 ]; then\n    echo \"Email sent successfully.\"\nelse\n    echo \"Failed to send email.\"\n    echo \"Response: $response\"\nfi<\/strong>\n------------------------------------------<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"525\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/emails.png\" alt=\"\" class=\"wp-image-17072\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/emails.png 733w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/emails-300x215.png 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/figure>\n\n\n\n<p>Save the file and exit the editor.<\/p>\n\n\n\n<p><strong>Explanation of the Script:<\/strong><\/p>\n\n\n\n<p>This script is designed to send an email using Gmail\u2019s SMTP server through the command line. Here\u2019s a step-by-step explanation of each part of the script:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Shebang: The<strong>\u00a0<code>#!\/bin\/bash<\/code><\/strong>\u00a0line at the beginning of the script indicates that the script should be run using the Bash shell.<\/li>\n\n\n\n<li>User Input Prompts: These lines prompt the user to enter\n<ul class=\"wp-block-list\">\n<li>The sender\u2019s email address.<\/li>\n\n\n\n<li>The recipient\u2019s email address.<\/li>\n\n\n\n<li>The sender\u2019s Google App password (entered silently using\u00a0<code><strong>-s<\/strong><\/code>\u00a0to hide the input).<\/li>\n\n\n\n<li>The subject of the email.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Reading the Email Body: This section prompts the user to enter the body of the email. The user can type the email content and end input by pressing\u00a0<code><strong>Ctrl+D<\/strong><\/code>. The body of the email is then captured into the variable\u00a0<code>body<\/code>.<\/li>\n\n\n\n<li>Sending the Email: This part of the script uses\u00a0<code>curl<\/code>\u00a0to send the email\n<ul class=\"wp-block-list\">\n<li><code><strong>-s<\/strong><\/code>\u00a0makes\u00a0<code><strong>curl<\/strong><\/code>\u00a0run silently.<\/li>\n\n\n\n<li><code><strong>--url 'smtps:\/\/smtp.gmail.com:465'<\/strong><\/code>\u00a0specifies the Gmail SMTP server with <strong>SSL<\/strong> on port <strong>465<\/strong>.<\/li>\n\n\n\n<li><code><strong>--ssl-reqd<\/strong><\/code>\u00a0ensures <strong>SSL<\/strong> is required.<\/li>\n\n\n\n<li><code><strong>--mail-from \"$sender\"<\/strong><\/code>\u00a0specifies the sender\u2019s email address.<\/li>\n\n\n\n<li><code><strong>--mail-rcpt \"$receiver\"<\/strong><\/code>\u00a0specifies the recipient\u2019s email address.<\/li>\n\n\n\n<li><code><strong>--user \"$sender:$gapp\"<\/strong><\/code>\u00a0provides the sender\u2019s email and Google App password for authentication.<\/li>\n\n\n\n<li><code><strong>-T &lt;(echo -e \"From: $sender\\nTo: $receiver\\nSubject: $sub\\n\\n$body\")<\/strong><\/code>\u00a0sends the email content including headers and body.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Handling the Response: This final part checks the exit status of the\u00a0<code><strong>curl<\/strong><\/code>\u00a0command<strong> (<code>$?<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If the exit status is\u00a0<code><strong>0<\/strong><\/code>, it prints<strong> \u201cEmail sent successfully.\u201d<\/strong><\/li>\n\n\n\n<li>If the exit status is <strong>not\u00a0<code>0<\/code><\/strong>, it prints <strong>\u201cFailed to send email.\u201d<\/strong> and outputs the response from\u00a0<code><strong>curl<\/strong><\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #4:Make file executable<\/strong><\/h3>\n\n\n\n<p>Change the file permissions to make it executable using the\u00a0<strong>chmod\u00a0<\/strong>command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>chmod +x email.sh<\/strong>\n\n<strong>Step #5:Run the script<\/strong>\nRun the script by executing the following command:\n\n<strong>.\/email.sh<\/strong>\n\nYou will be prompted to enter the sender\u2019s email, recipient\u2019s email, Google App password, subject, and body of the email:<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"657\" height=\"242\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/5-3.png\" alt=\"\" class=\"wp-image-17073\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/5-3.png 657w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/5-3-300x111.png 300w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/figure>\n\n\n\n<p>You will get the message&nbsp;<strong>Email sent successfully.<\/strong><\/p>\n\n\n\n<p>Now check the mail box to see if you receive the mail or not.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"906\" height=\"652\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/7-4.png\" alt=\"\" class=\"wp-image-17074\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/7-4.png 906w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/7-4-300x216.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/7-4-768x553.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p>In conclusion, sending emails using shell scripting can greatly enhance your automation tasks, enabling seamless communication and notifications. This approach is especially useful for automated notifications, error alerts, and other routine communications. With the power of shell scripting, you can integrate emails functionality into your workflows, ensuring timely and efficient information exchange.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.fosstechnix.com\/how-to-send-email-using-shell-script\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn how to send email using shell scripting. Sending emails programmatically can be very useful for automated notifications, error alerts, or any other form of communication that needs to be sent without manual intervention. Shell scripting offers a powerful way to achieve this through various utilities and tools available in &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2024\/05\/24\/how-to-send-email-using-shell-script\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Send Email using Shell Script (gmail).&#8221;<\/span><\/a><\/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-17068","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=17068"}],"version-history":[{"count":3,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17068\/revisions"}],"predecessor-version":[{"id":17081,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17068\/revisions\/17081"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=17068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=17068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=17068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}