Sending HTML using shell script

How to send HTML message using shell script?

+46
html linux bash email sendmail
Jul 23 2018-10-23T00:
source share
12 answers

First you need to compose a message. The bare minimum consists of these two headers:

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

... and the corresponding message text:

 <!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> 

After that, 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 a simplified example, since you also need to take care of the encodings, the encodings, the maximum length of the string ... But this is basically an idea.

Alternatively, you can write a script in Perl or PHP, rather than in a simple shell.

Update

A shell script is a text file with the end of a Unix line that starts with a shebang line that tells the shell that the interpreter must transfer the file, execute some commands in a language that the interpreter understands and has permission to execute (in Unix, the file attribute). For example, 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 it is, it is not related to the original question. You should familiarize yourself with the basic shell scripts before performing complex tasks with it. Here you will find 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

+57
Jul 23 '10 at 10:27
source share

Tags include sendmail, so the following solution can be used here:

 ( echo "From: me@xyz.com " echo "To: them@xyz.com " echo "MIME-Version: 1.0" echo "Content-Type: multipart/alternative; " echo ' boundary="some.unique.value.ABC123/server.xyz.com"' echo "Subject: Test HTML e-mail." echo "" echo "This is a MIME-encapsulated message" echo "" echo "--some.unique.value.ABC123/server.xyz.com" echo "Content-Type: text/html" echo "" echo "<html> <head> <title>HTML E-mail</title> </head> <body> <a href='http://www.google.com'>Click Here</a> </body> </html>" echo "------some.unique.value.ABC123/server.xyz.com--" ) | sendmail -t 

A wrapper for sendmail can make this work easier, for example mutt :

 mutt -e 'set content_type="text/html"' me@mydomain.com -s "subject" < message.html 
+44
Jul 26 '10 at 13:47
source share

So far I have found two quick ways in cmd linux

  • Use old school mail

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

  • Use mutt

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

+37
Sep 07
source share

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 the file as the body of the message. See the link for more details.

+2
Jul 28 '10 at 6:52
source share

Another option is msmtp.

You need to configure your .msmtprc with something like this (e.g. gmail is used):

 account default host smtp.gmail.com port 587 from example@gmail.com tls on tls_starttls on tls_trust_file ~/.certs/equifax.pem auth on user example@gmail.com password <password> logfile ~/.msmtp.log 

Then just call:

 (echo "Subject: <subject>"; echo; echo "<message>") | msmtp <email@domain.tld> 

in script

Update. For HTML mail, you should also put headers, so you can create a file like this:

 From: sender@domain.tld To: email@domain.tld Subject: Important message Mime-Version: 1.0 Content-Type: text/html <h1>Mail body will be here</h1> The mail body <b>should</b> start after one blank line from the header. 

And write him how

 cat email-template | msmtp email@domain.tld 

The same can be done using the command line, but it may be easier to use the file.

+2
Aug 01 '10 at
source share

shell send html email - UNIX and Linux Forums

http://www.unix.com/shell-programming-scripting/80973-shell-send-html-email.html

Sending email from a shell script - Shell scripts

http://www.daniweb.com/forums/thread15280.html

+1
Jul 31 '10 at 5:22
source share
 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
Jan 08 '15 at 12:22
source share

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

Team

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

cpu_alert.html sample file.

 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 is available here: http://sugunan.net/git/slides/shell/cpu.php

0
Apr 19 '15 at 4:59
source share

Here is mine (this "mail" is configured correctly):

scanuser @owncloud: ~ $ vi sendMailAboutNewDocuments.sh

 mail -s "You have new mail" -a "Content-type: text/html" -a "From: sender@xxx.com" $1 << EOF <html> <body> Neues Dokument: $2<br> <a href="https://xxx/index.php/apps/files/?dir=/Post">Hier anschauen</a> </body> </html> EOF 

make an executable file:

 chmod +x sendMailAboutNewDocuments.sh 

then call:

 ./sendMailAboutNewDocuments.sh recipient@xxx.com test.doc 
0
Jun 29 '18 at 13:14
source share

Using CentOS 7 by default mailx (displayed as heirloom-mailx), I simplified this to a simple use of a text file with the required headers and a static border for a multi-part / mixed and multi-part / alternative installation.

I am sure you can figure out multipart / related if you want with the same setting.

test.txt:

 --000000000000f3b2150570186a0e Content-Type: multipart/alternative; boundary="000000000000f3b2130570186a0c" --000000000000f3b2130570186a0c Content-Type: text/plain; charset="UTF-8" This is my plain text stuff here, in case the email client does not support HTML or is blocking it purposely My Link Here <http://www.example.com> --000000000000f3b2130570186a0c Content-Type: text/html; charset="UTF-8" <div dir="ltr"> <div>This is my HTML version of the email</div> <div><br></div> <div><a href="http://www.example.com">My Link Here</a><br></div> </div> --000000000000f3b2130570186a0c-- --000000000000f3b2150570186a0e Content-Type: text/csv; charset="US-ASCII"; name="test.csv" Content-Disposition: attachment; filename="test.csv" Content-Transfer-Encoding: base64 X-Attachment-Id: f_jj5qmzqz0 

Borders define compound segments.

A border identifier that does not have a dash at the end is the starting point of the segment.

The one with two traits at the end is the end point.

In this example, there is a part in the main multipart / mixed section for multipart / alternative.

The multi-element / alternative method basically says: “Rollback to this IF the priority part is not executed” - in this example, HTML clients usually perceive priority as HTML. If the mail client does not display HTML, it returns to plain text.

The multipart / mixed method, which encapsulates the whole message, basically says that there is different content displaying both.

In this example, I posted the attachment as a CSV file to my email. You will see that the attachment is connected using base64 in the command below.

I added an attachment as an example, you will need to configure the type of content according to the attachment and indicate whether it will be embedded or not.

X-Attachment-Id is required for some providers, randomly identify the identifier you set.

The command to send by mail is:

 echo -e "'cat test.txt; openssl base64 -e < test.csv'\n--000000000000f3b2150570186a0e--\n" | mailx -s "Test 2 $( echo -e "\nContent-Type: multipart/mixed; boundary=\"000000000000f3b2150570186a0e\"" )" -r fromaddress@example.com toaddress@example.com 

As you can see in the mailx subject line, I statically insert a multilayer border, this is the first header that the mail client will see.

Then comes the dumping of the contents of test.txt.

As for attachments, I use openssl (which is pretty standard on systems) to convert a file attachment to base64.

In addition, I added a border closing operator at the end of this echo to mark the end of the message.

This works around heirloom issues and with little or no scripting.

Instead, the echo can be a feed or any other number of methods.

0
Jul 03 '18 at 14:01
source share

Use the CLI for mass mailing , an efficient and powerful tool for sending dynamic emails to the mailing list with one simple command: bulkmail mail !

Do quick, mini, hassle-free email marketing with this small but Powerful tool 💌

0
Apr 02 '19 at 15:42
source share

In addition to the correct answer from mdma, you can also use the mail command as follows:

 mail person@email.com -s"Subject Here" -a"Content-Type: text/html; charset=\"us-ascii\"" 

You will get what you are looking for. Be sure to include <HTML> and </HTML> in the email. Here is a quick script that I use to send a daily report in HTML format:

 #!/bin/sh (cat /path/to/tomorrow.txt mysql -h mysqlserver -u user -pPassword Database -H -e "select statement;" echo "</HTML>") | mail email@email.com -s"Tomorrow orders as of now" -a"Content-Type: text/html; charset=\"us-ascii\"" 
-one
Mar 15 2018-11-11T00:
source share



All Articles