How can I send an HTML email using a shell script?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
- Is shmid returned by shmget() unique across proces
So far I have found two quick ways in cmd linux
mail -s "$(echo -e "This is Subject\nContent-Type: text/html")" test@yahoo.com < mytest.html
mutt -e "my_hdr Content-Type: text/html" test@yahoo.com -s "subject" < mytest.html
The tags include 'sendmail' so here's a solution using that:
A wrapper for sendmail can make this job easier, for example, mutt:
And then:
Another option is the sendEmail script http://caspian.dotconf.net/menu/Software/SendEmail/, it also allows you to set the message type as html and include a file as the message body. See the link for details.
Mime header and from, to address also can be included in the html file it self.
Command
cpu_alert.html file sample.
Sample code available here: http://sugunan.net/git/slides/shell/cpu.php
Using CentOS 7's default mailx (appears as heirloom-mailx), I've simplified this to just using a text file with your required headers and a static boundary for multipart/mixed and multipart/alternative setup.
I'm sure you can figure out multipart/related if you want with the same setup.
test.txt:
The boundaries define multipart segments.
The boundary ID that has no dashes at the end is a start point of a segment.
The one with the two dashes at the end is the end point.
In this example, there's a subpart within the multipart/mixed main section, for multipart/alternative.
The multipart/alternative method basically says "Fallback to this, IF the priority part does not succeed" - in this example HTML is taken as priority normally by email clients. If an email client won't display the HTML, it falls back to the plain text.
The multipart/mixed method which encapsulates this whole message, is basically saying there's different content here, display both.
In this example, I placed a CSV file attachment on the email. You'll see the attachment get plugged in using base64 in the command below.
I threw in the attachment as an example, you'll have to set your content type appropriately for your attachment and specify whether inline or not.
The X-Attachment-Id is necessary for some providers, randomize the ID you set.
The command to mail this is:
As you can see in the mailx Subject line I insert the multipart boundary statically, this is the first header the email client will see.
Then comes the test.txt contents being dumped.
Regarding the attachment, I use openssl (which is pretty standard on systems) to convert the file attachment to base64.
Additionally, I added the boundary close statement at the end of this echo, to signify the end of the message.
This works around heirloom-mailx problems and is virtually script-less.
The echo can be a feed instead, or any other number of methods.