{"id":277,"date":"2018-07-25T12:19:32","date_gmt":"2018-07-25T09:19:32","guid":{"rendered":"https:\/\/small-service.com\/?p=277"},"modified":"2018-07-27T18:07:10","modified_gmt":"2018-07-27T15:07:10","slug":"email-from-console-or-via-bash-script","status":"publish","type":"post","link":"https:\/\/small-service.com\/?p=277","title":{"rendered":"Email from console or via bash script"},"content":{"rendered":"<p>Sometimes required to send email from console or from bash script. And here I will provide few use-cases&#8230;<\/p>\n<p><!--more--><\/p>\n<ul>\n<li>mail &#8211; simple email:<br \/>\n<code>echo \"BODY MESSAGE\" | mail -s \"Subject\" rcpt@some-domain.com<\/code><\/li>\n<li>mail &#8211; with attachment:<br \/>\n<code>echo \"BODY MESSAGE\" | mail -s \"Subject\" -a attachmet.txt rcpt@some-domain.com<\/code><\/li>\n<li>sendmail &#8211; simple email:\n<pre>\tsendmail email@some-domain.com &lt;&lt;EOF\r\n\tSubject: Test Subject\r\n\tFrom: test@some-domain\r\n\t    Email body.\r\n\tEOF\r\n<\/pre>\n<\/li>\n<li>sendmail &#8211; email with HTML content:\n<pre>\tsendmail  email@some-domain.com &lt;&lt;EOF\r\n\tSubject: Test Subject\r\n\tFrom: test@some-domain\r\n\tMIME-Version: 1.0\r\n\tContent-Type: text\/html\r\n\t&lt;html&gt;\r\n\t\u2003&lt;head&gt;\r\n\t\u2003\u2003&lt;title&gt; My email &lt;\/title&gt;\r\n\t\u2003&lt;\/head&gt;\r\n\t\u2003&lt;body&gt;\r\n\t\u2003\u2003&lt;h2&gt; Here will be your message body &lt;\/h2&gt;\r\n\t\u2003&lt;\/body&gt;\r\n\t&lt;\/html&gt;\r\n\tEOF\r\n<\/pre>\n<\/li>\n<li>sendmail &#8211; binary file (here will be sent picture):\n<pre>\tmailpart=\"$(uuidgen)\"\r\n\tsendmail  -t &lt;&lt;EOM\r\n\r\n\tTo: email@some-domain.com\r\n\tSubject: Test Subject\r\n\tFrom: test@some-domain\r\n\tMIME-Version: 1.0\r\n\tContent-Type: multipart\/mixed; boundary=\\\"${mailpart}\\\"\r\n\r\n\t--${mailpart}\r\n\tContent-Type: text\/html; charset=ISO-8859-15\r\n\tContent-Transfer-Encoding: 7bit\r\n\r\n\t&lt;html&gt;\r\n\t\u2003&lt;head&gt;\r\n\t\u2003\u2003&lt;title&gt; My email &lt;\/title&gt;\r\n\t\u2003&lt;\/head&gt;\r\n\t\u2003&lt;body&gt;\r\n\t\u2003\u2003&lt;img src=\"cid:part1.06090408.01060107\" alt=\"\"\\&gt;\r\n\t\u2003&lt;\/body&gt;\r\n\t&lt;\/html&gt;\r\n\r\n\t--${mailpart}\r\n\tContent-Type: image\/jpeg;name=\"\"\r\n\tContent-Transfer-Encoding: base64\r\n\tContent-ID: \r\n\tContent-Disposition: inline; filename=\"filename.png\"\r\n\r\n\t$(base64 \/path\/to\/filename.png)\r\n\t--${mailpart}--\r\n\r\n\tEOM\r\n<\/pre>\n<\/li>\n<li>sendmail &#8211; email with HTML content and embedded picture:\n<pre>\tmailpart=\"$(uuidgen)\"\r\n\tmailpartBody=\"$(uuidgen)\"\r\n\r\n\t\/usr\/sbin\/sendmail -t &lt;&lt;EOF\r\n\tSubject: Test Subject\r\n\tFrom: email@some-domain.com;\r\n\tTo: email@some-domain.com\r\n\tContent-Type: multipart\/related; type=\"multipart\/alternative\"; boundary=\"${mailpart}\"\r\n\tMime-Version: 1.0\r\n\r\n\t--${mailpart}\r\n\tContent-Type: multipart\/alternative; boundary=\"${mailpartBody}\"\r\n\r\n\t--${mailpartBody}\r\n\tContent-Type: text\/plain\r\n\tContent-Transfer-Encoding: 7bit\r\n\r\n\tHere we have message-body\r\n\r\n\t--${mailpartBody}\r\n\tContent-Type: text\/html; charset=\"utf-8\"\r\n\tContent-Transfer-Encoding: 7bit\r\n\r\n\t&lt;html&gt;\r\n\t\u2003&lt;head&gt;\r\n\t\u2003\u2003&lt;title&gt; My email &lt;\/title&gt;\r\n\t\u2003&lt;\/head&gt;\r\n\t\u2003&lt;body&gt;\r\n\t\u2003&lt;h3&gt;Here we have message-body&lt;\/h3&gt;\r\n\t\u2003\u2003&lt;img src=\"cid:image.png@some-domain.com\" alt=\"\"\\&gt;\r\n\t\u2003&lt;\/body&gt;\r\n\t&lt;\/html&gt;\r\n\r\n\t--${mailpartBody}--\r\n\r\n\t--${mailpart}\r\n\tContent-ID: &lt;image.png@some-domain.com&gt;\r\n\tContent-Type: image\/jpeg; name=image.png\r\n\tContent-Disposition: inline; filename=image.png\r\n\tContent-Transfer-Encoding: base64\r\n\r\n\t$(base64 image.png)\r\n\r\n\t--${mailpart}--\r\n\r\n\tEOF<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes required to send email from console or from bash script. And here I will provide few use-cases&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[90,91,92,89,93,94,96,95,97],"_links":{"self":[{"href":"https:\/\/small-service.com\/index.php?rest_route=\/wp\/v2\/posts\/277"}],"collection":[{"href":"https:\/\/small-service.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/small-service.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/small-service.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/small-service.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=277"}],"version-history":[{"count":20,"href":"https:\/\/small-service.com\/index.php?rest_route=\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/small-service.com\/index.php?rest_route=\/wp\/v2\/posts\/277\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/small-service.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/small-service.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/small-service.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}