I've been trying to work through sending a html email from mailx on a linux server.
Few Notes:
- I have to specify a smtp server so therefore I cannot use sendmail
(This is not something I can change on my end) - I cannot install 3rd party things such as mutt. I will have to use
mail or mailx - Since my mail/x version is heirloom I do not have the –append or -a (attach header options)
- not sure if this helps at all but my linux distro is 7.3 (Maipo)
What I've seen in most posts on stackoverflow for my case:
mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject\nContent-Type: text/html")" -r FROM TO < htmlmail.txt
This just returns a plain text email in my case.
So here is what I have tried so far:
Try 1:
I saw in a post to add Content-Disposition: inline.
mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject v1\nContent-Type: text/html\nMIME-Version: 1.0\nContent-Disposition: inline")" -r FROM TO < htmlmail.txt
This ends up sending an html email but since the headers are included inline with the body it outputs as such:
Content-Disposition: inline Message-ID: User-Agent: Heirloom
mailx 12.5 7/5/10 MIME-Version: 1.0 Content-Type: text/plain;
charset=us-ascii Content-Transfer-Encoding: 7bit Hello World
Try 2:
So I do not want the headers printed out in the body. So I tried to remove the Content-Disposition: inline
mailx -v -S smtp=SERVER -s "$(echo -e "This is the subject v2\nContent-Type: text/html\nMIME-Version: 1.0")" -r FROM TO < htmlmail.txt
This ends up sending a plain test email as such:
<html>
<b>Hello World</b>
</html>
Try 3:
Tried flip flopping content-type and mime-version
mailx -v -S SERVER -s "$(echo -e "This is the subject v3\nMIME-Version: 1.0\nContent-Type: text/html")" -r FROM TO < htmlmail.txt
I ended up receiving no email from this code
Try 4:
I saw online to try another header to help find where the issue lies. So I added the header option to set the mail priority.
mailx -v -S smtp=SERVER -s "$(echo -e "This is a subject v4\nContent-Type: text/html\nX-Priority: 1 (Highest)")" -r FROM TO < htmlmail.txt
This ended up sending a high priority email but all in plain text.
Try 5:
I added on the MIME header to the previous try
mailx -v -S smtp=SERVER -s "$(echo -e "This is a subject v5\nMIME-Version: 1.0\nContent-Type: text/html\nX-Priority: 1 (Highest)")" -r FROM TO < htmlmail.txt
This ended up sending an email with headers in the body and the priority not set to high…weird
X-Priority: 1 (Highest) Message-ID: User-Agent: Heirloom mailx
12.5 7/5/10 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hello World
After all of this, I've tried many other adaptations of the above tries but they led to no new output.
So any suggestions or ideas are gladly accepted! Please keep in mind my constraints listed above…I know they limit my options but that's out of my control.
thanks for your time!
Best Answer
First some context: I am using heirloom-mailx version as the one on the following thread: https://serverfault.com/questions/136106/what-package-to-install-for-sending-emails-from-localhost-ubuntu
I am using Ubuntu 16.04 Xenial. Tried also on Ubuntu Server 16.04.
In order to send emails I am using the following function for sending mail using mailx (heirloom mailx in bash):
Where $2 is the attachment and $1 is the destination. Notes: 1. Files attached are printed inside the body as well but this could be useful to you if you wish just to send an html file with no attachments. 2. Using "-v" option prints verbose so you might get an issue with .mime.types that can be ignored. Remove the option if you don't want verbose on mailx. 3. You will still get the following in the body if you use the "-a" option: This is a multi-part message in MIME format. --=-=fFPa7dLqoSF1TGj-YDc2k8bdvmjpix_4sKFT=-= Content-Type: text/plain; charset=US-ASCII Content-Disposition: inline
In this case I am attaching a plain text file. Removing "-a $2" from the command and you are all set to print the html message. So the final result would be:
Try it out and let me know. I have tested on my end and it works.