Thursday 5 December 2019

linux - Mailx send html message

There are many different versions of mail around. When you go beyond mail -s subject to1@address1 to2@address2



  • With some mailx implementations, e.g. from mailutils on Ubuntu or
    Debian's bsd-mailx, it's easy, because there's an option for that.



    mailx -a 'Content-Type: text/html' -s "Subject" to@address 

  • With the Heirloom mailx, there's no convenient way. One possibility
    to insert arbitrary headers is to set editheaders=1 and use an
    external editor (which can be a script).




    ## Prepare a temporary script that will serve as an editor.

    ## This script will be passed to ed.
    temp_script=$(mktemp)
    cat <<'EOF' >>"$temp_script"
    1a
    Content-Type: text/html
    .
    $r test.html

    w
    q
    EOF
    ## Call mailx, and tell it to invoke the editor script
    EDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <~e
    .
    EOF
    rm -f "$temp_script"


  • With a general POSIX mailx, I don't know how to get at headers.




If you're going to use any mail or mailx, keep in mind that




  • This isn't portable even within a given Linux distribution. For example, both Ubuntu and Debian have several alternatives for mail and mailx.


  • When composing a message, mail and mailx treats lines beginning with ~ as commands. If you pipe text into mail, you need to arrange for this text not to contain lines beginning with ~.





If you're going to install software anyway, you might as well install something more predictable than mail/Mail/mailx. For example, mutt. With Mutt, you can supply most headers in the input with the -H option, but not Content-Type, which needs to be set via a mutt option.



mutt -e 'set content_type=text/html' -s 'hello' 'to@address' 


Or you can invoke sendmail directly. There are several versions of sendmail out there, but they all support sendmail -t to send a mail in the simplest fashion, reading the list of recipients from the mail. (I think they don't all support Bcc:.) On most systems, sendmail isn't in the usual $PATH, it's in /usr/sbin or /usr/lib.



cat <<'EOF' - test.html | /usr/sbin/sendmail -t
To: to@address
Subject: hello

Content-Type: text/html

EOF

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...