0

I have my script for sending email to some user in Linux. I want to read the string content as HTML in bash, but bash is not able to read string as HTML.

Start of Script:

body ='
 Dear $user,
<p> The password for your account is due to expire on in 14 days. and must be changed.<br>
<p>You can reset your password by visiting the Password Reset Portal.</p>
<p><a href="https://google.com/">Google</a> </p>
<p>You can contact the XYZ Team in case of any issue. </p>'

#send email
user='ABC'
email="abc@gmail.co."
from="xyz@gmail.com"    
echo $body1 | mail -s test -r $from $email  

I wanted my message to user will be shown as below and read string as html

Dear ABC,
The password for your account is due to expire on in 14 days. and must be changed.
You can reset your password by visiting the Password Reset Portal.
Google
You can contact the XYZ Team in case of any issue

However bash is reading the body as a string and not as HTML as below

Dear ABC,
<p> The password for your account is due to expire on in 14 days. and must be changed.</p><br>
<p>You can reset your password by visiting the Password Reset Portal.</p>
<p><a href="https://google.com/">Google</a> </p>
<p>You can contact the XYZ Team in case of any issue. </p>

Please anyone can help me on this.

I have tried the answers from the link Sending HTML mail using a shell script but i am not succeeded.

Robert
  • 7,394
  • 40
  • 45
  • 64
user2641452
  • 105
  • 2
  • 10
  • 1
    duplicate of [this](https://stackoverflow.com/questions/2591755/how-to-send-html-email-using-linux-command-line)? – Neil Oct 11 '19 at 20:54
  • Possible duplicate of [How to send HTML email using linux command line](https://stackoverflow.com/questions/2591755/how-to-send-html-email-using-linux-command-line) – Eugene Astafiev Oct 12 '19 at 08:18
  • i tried using the solution https://stackoverflow.com/questions/2591755/how-to-send-html-email-using-linux-command-line but it didnt work – user2641452 Oct 22 '19 at 14:51

1 Answers1

0

The first issue is that mail is a generic name for quite a few different programs (depending on OS and distro), and sometimes mail is a symlink to some other program (eg, mailx). Net result is that it's going to be hard to troubleshoot your issue without a good bit more detail on the program you're using called mail. This in turn can make (semi) portability of scripts a real b*tch (even between hosts in the same domain). For these reasons I tend to use sendmail when I need to do some special formatting on the body of an email.

The second issue is that most mail programs need to be instructed to interpret (html) tags. This typically includes adding (at the top of the email) the clauses MIME-VERSION: 1.0 and Content-Type: text/html.

One simple example you could start with:

body="Dear $user,
<p> The password for your account is due to expire on in 14 days. and must be changed.<br>
<p>You can reset your password by visiting the Password Reset Portal.</p>
<p><a href=\"https://google.com/\">Google</a> </p>
<p>You can contact the XYZ Team in case of any issue. </p>"

email="abc@gmail.co."
from="xyz@gmail.com"

echo "Subject: My test subject
To: ${email}
From: ${from}
MIME-Version: 1.0
Content-type: text/html
<html><body>
${body}
</body></html>" | /usr/sbin/sendmail -t

NOTE: Verify the path on your host for sendmail.

NOTE: If you don't have sendmail installed then that's a whole 'nother post since the sysadmin will need to setup a config file to tell sendmail how to process (e)mail messages (eg, what's the name of the smtp server?).

With this basic wrapper you should be able to add more html tags as needed.

markp-fuso
  • 28,790
  • 4
  • 16
  • 36