How to send HTML email using linux command line

前端 未结 11 2037
无人共我
无人共我 2020-11-28 04:29

I need to send email with html format. I have only linux command line and command \"mail\".

Currently have used:

echo \"To: address@example.com\" >         


        
相关标签:
11条回答
  • 2020-11-28 04:48

    With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.

    The script I use (~/bin/sendmail-hook):

    #!/bin/bash
    
    sed '1,/^$/{
    s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
    s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
    }' | sendmail $@
    

    This script changes the values in the mail header as follows:

    • Content-Type: to text/html; charset=utf-8
    • Content-Transfer-Encoding: to 8bit (not sure if this is really needed).

    To send HTML email:

    mail -Ssendmail='~/bin/sendmail-hook' \
        -s "Built notification" address@example.com < /var/www/report.csv
    
    0 讨论(0)
  • 2020-11-28 04:52

    Very old question, however it ranked high when I googled a question about this.

    Find the answer here:

    Sending HTML mail using a shell script

    0 讨论(0)
  • 2020-11-28 04:53

    On OS X (10.9.4), cat works, and is easier if your email is already in a file:

    cat email_template.html  | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl@marx.com
    
    0 讨论(0)
  • 2020-11-28 04:54

    The problem is that when redirecting a file into 'mail' like that, it's used for the message body only. Any headers you embed in the file will go into the body instead.

    Try:

    mail --append="Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.csv
    

    --append lets you add arbitrary headers to the mail, which is where you should specify the content-type and content-disposition. There's no need to embed the To and Subject headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address@example.com automatically becomes the To).

    0 讨论(0)
  • 2020-11-28 04:58

    Try with :

    echo "To: address@example.com" > /var/www/report.csv
    echo "Subject: Subject" >> /var/www/report.csv
    echo "MIME-Version: 1.0" >> /var/www/report.csv
    echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
    echo "Content-Disposition: inline" >> /var/www/report.csv
    
    echo "<html>" >> /var/www/report.csv
    mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
    echo "</html>" >> /var/www/report.csv
    
    mail -s "Built notification" address@example.com < /var/www/report.csv
    
    0 讨论(0)
提交回复
热议问题