Sending HTML mail using a shell script

前端 未结 13 1405
死守一世寂寞
死守一世寂寞 2020-11-28 03:46

How can I send an HTML email using a shell script?

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

    First you need to compose the message. The bare minimum is composed of these two headers:

    MIME-Version: 1.0
    Content-Type: text/html
    

    ... and the appropriate message body:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title></title>
    </head>
    <body>
    
    <p>Hello, world!</p>
    
    </body>
    </html>
    

    Once you have it, you can pass the appropriate information to the mail command:

    body = '...'
    
    echo $body | mail \
    -a "From: me@example.com" \
    -a "MIME-Version: 1.0" \
    -a "Content-Type: text/html" \
    -s "This is the subject" \
    you@example.com
    

    This is an oversimplified example, since you also need to take care of charsets, encodings, maximum line length... But this is basically the idea.

    Alternatively, you can write your script in Perl or PHP rather than plain shell.

    Update

    A shell script is basically a text file with Unix line endings that starts with a line called shebang that tells the shell what interpreter it must pass the file to, follow some commands in the language the interpreter understands and has execution permission (in Unix that's a file attribute). E.g., let's say you save the following as hello-world:

    #!/bin/sh
    
    echo Hello, world!
    

    Then you assign execution permission:

    chmod +x hello-world
    

    And you can finally run it:

    ./hello-world
    

    Whatever, this is kind of unrelated to the original question. You should get familiar with basic shell scripting before doing advanced tasks with it. Here you are a couple of links about bash, a popular shell:

    http://www.gnu.org/software/bash/manual/html_node/index.html

    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

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

    The question asked specifically on shell script and the question tag mentioning only about sendmail package. So, if someone is looking for this, here is the simple script with sendmail usage that is working for me on CentOS 8:

    #!/bin/sh
    TOEMAIL="youremail@server.com"
    REPORT_FILE_HTML="$REPORT_FILE.html"
    echo "Subject: EMAIL SUBJECT" >> "${REPORT_FILE_HTML}"
    echo "MIME-Version: 1.0" >> "${REPORT_FILE_HTML}"
    echo "Content-Type: text/html" >> "${REPORT_FILE_HTML}"
    echo "<html>" >> "${REPORT_FILE_HTML}"
    echo "<head>" >> "${REPORT_FILE_HTML}"
    echo "<title>Best practice to include title to view online email</title>" >> "${REPORT_FILE_HTML}"
    echo "</head>" >> "${REPORT_FILE_HTML}"
    echo "<body>" >> "${REPORT_FILE_HTML}"
    echo "<p>Hello there, you can put email html body here</p>" >> "${REPORT_FILE_HTML}"
    echo "</body>" >> "${REPORT_FILE_HTML}"
    echo "</html>" >> "${REPORT_FILE_HTML}"
    
    sendmail $TOEMAIL < $REPORT_FILE_HTML
    
    0 讨论(0)
  • 2020-11-28 04:25

    I've been trying to just make a simple bash script that emails out html formatted content-type and all these are great but I don't want to be creating local files on the filesystem to be passing into the script and also on our version of mailx(12.5+) the -a parameter for mail doesn't work anymore since it adds an attachment and I couldn't find any replacement parameter for additional headers so the easiest way for me was to use sendmail.

    Below is the simplest 1 liner I created to run in our bash script that works for us. It just basically passes the Content-Type: text/html, subject, and the body and works.

    printf "Content-Type: text/html\nSubject: Test Email\nHTML BODY<b>test bold</b>" | sendmail <Email Address To>
    

    If you wanted to create an entire html page from a variable an alternative method I used in the bash script was to pass the variable as below.

    emailBody="From: <Email Address From>
    Subject: Test
    Content-Type: text/html; charset=\"us-ascii\"
    <html>
    <body>
    body
    <b> test bold</b>
    
    </body>
    </html>
    "
    echo "$emailBody" | sendmail <Email Address To>
    
    0 讨论(0)
  • 2020-11-28 04:28

    So far I have found two quick ways in cmd linux

    1. Use old school mail

    mail -s "$(echo -e "This is Subject\nContent-Type: text/html")" test@yahoo.com < mytest.html

    1. Use mutt

    mutt -e "my_hdr Content-Type: text/html" test@yahoo.com -s "subject" < mytest.html

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

    Mime header and from, to address also can be included in the html file it self.

    Command

    cat cpu_alert.html | /usr/lib/sendmail -t
    

    cpu_alert.html file sample.

    From: donotreply@example.com
    To: admin@example.com
    Subject: CPU utilization heigh
    Mime-Version: 1.0
    Content-Type: text/html
    
    <h1>Mail body will be here</h1>
    The mail body should start after one blank line from the header.
    

    Sample code available here: http://sugunan.net/git/slides/shell/cpu.php

    0 讨论(0)
  • 2020-11-28 04:32
    cat > mail.txt <<EOL
    To: <email>
    Subject: <subject>
    Content-Type: text/html
    
    <html>
    $(cat <report-table-*.html>)
    This report in <a href="<url>">SVN</a>
    </html>
    
    EOL
    

    And then:

    sendmail -t < mail.txt
    
    0 讨论(0)
提交回复
热议问题