2

I am trying to sent an email from bash script which should have HTML table in body , I am using mail command in Redhat . but its keep sending me as Text file .

difference=`expr $artu_removed - $artu_added`

mail  -s "Built notification" test@gmail.com << EOF


<html>

<head><title></title>
</head>
<body>

<table>  
 <tr>
  <Td> Before </td>  <td>after </td>  <td>differece </td>
 </tr>

<tr>
  <Td> $_before </td>  <td>$_after </td>  <td>$difference </td>
 </tr>

</table>

 Before:$_before
 After:$_after
 Difference:$difference
</body>
</html>
EOF

Can any one please let me know what shall I do, I am using Redhat, Not ubuntu

Thanks

alammd
  • 339
  • 4
  • 14
  • 2
    See: [How to send HTML email using linux command line](https://stackoverflow.com/q/2591755/3776858) – Cyrus Apr 12 '19 at 18:18
  • @Cyrus, Please see the issues in other comments, I am getting the HTML table in outlook but its giving this line as text "Content-Transfer-Encoding: 7bit Cc: test@outlookcom User-Agent: Heirloom mailx 12.4 7/29/08 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit " – alammd Apr 13 '19 at 07:38

2 Answers2

1

Try it via mail (do not forget to add your variables):

mail -a "Content-type: text/html" -s "HTML message" test@gmail.com << EOF
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Title</title>
</head>
<body>
    <table>
        <tr>
            <td> Before </td>
            <td> After </td>
            <td> Differece </td>
        </tr><tr>
            <td> $_before </td>
            <td> $_after </td>
            <td> $difference </td>
        </tr>
    </table>
    Before: $_before
    After: $_after
    Difference: $difference
</body>
</html>
EOF

If you need send from CentOS/RedHat via mailx use it:

mail -s "$(echo -e "HTML message\nContent-Type: text/html")" test@gmail.com << EOF

Try it for outlook:

mail -s "$(echo -e "HTML message\nContent-Transfer-Encoding: 7bit\nUser-Agent: Heirloom mailx 12.4 7/29/08\nMIME-Version: 1.0\nContent-Type: text/html; charset=us-ascii\nContent-Transfer-Encoding: 7bit")" test@outlook.com << EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
    <title>Title</title>
...
qwsj
  • 426
  • 3
  • 13
  • Thanks I tryed that solution already , its seems mail -a command works in ubuntu but it does not work in Redhat, i get this error Content-type: text/html: No such file or directory – alammd Apr 12 '19 at 19:06
  • now email comes but its still same, in email, its showing as text , today i tried both mail and maix but none of them giving me html out put . – alammd Apr 12 '19 at 19:24
  • @alammd Maybe because you have no CSS? replace `` to ``. if you get the text then you should just see the HTML tags, true? – qwsj Apr 12 '19 at 19:27
  • basically, in email, I am seeing all Text between < – alammd Apr 12 '19 at 19:32
  • @alammd Where do you get message? gmail? i am trying: gmail.com, mail.ru, and my own mx - all perfectly! (CentOS 7.6.1810 / mailx 12.5-19.el7) – qwsj Apr 12 '19 at 19:37
  • !!! this is odd, yes in Gmail its fine, but i was trying to get this in outlook , but there its comming as text !! it seems outlook does take this as html, any idea why ? – alammd Apr 12 '19 at 19:42
  • !! Thanks for the help, I can see now the table, one problem left, Along with Table, bellow is coming as TExt value "Content-Transfer-Encoding: 7bit User-Agent: Heirloom mailx 12.4 7/29/08 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit " – alammd Apr 12 '19 at 20:15
  • @alammd i am updated again. unfortunately i can not send myself mail in outlook. have problems with the domain. try update. – qwsj Apr 12 '19 at 20:20
  • with the recent code in out look its the same problem, Can you give me the old one (before this one ) then lets try to see why that line is comming as text , I really appreciated your help – alammd Apr 12 '19 at 20:31
  • @alammd i restored msg. now I will check another variant – qwsj Apr 12 '19 at 20:35
  • with your last update, it does not work on outlook ( gmail is fine) , it outlook its comes as text totally. – alammd Apr 12 '19 at 20:49
  • @alammd the problem is that the outlook doesn't want to understand what content is `text/html`, he is recived `text/plain`, even if present `Content-Type: text/html` – qwsj Apr 12 '19 at 21:11
  • its is understanding, its showing the table in HTML, the out put is fine, but what i dont understand why its showing this line ";Content-Transfer-Encoding: 7bit Cc: test@gmail.com User-Agent: Heirloom mailx 12.4 7/29/08 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii" – alammd Apr 12 '19 at 21:18
  • with last one , Full HTML comes as a Text. – alammd Apr 12 '19 at 21:37
  • i am not make a test, have a problem with outlook. – qwsj Apr 12 '19 at 21:59
0

After digging through many posts I found a solution for RedHat distros running postfix. You need to use the -t switch, not the -a switch as in other distros. Below is an example complete with the email content. You can copy the entire code example, swap in your information in notepad, and then paste the entire example to a CLI at the $ prompt.

mail -t << EOF
From: no-reply@domain.tld
To: uaser@domain.tld
Subject: Some subject here
Content-Type: text/html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
    This is normal text.<br>
    <b>This is bold text.</b><br>
    <i>This is text in italics.</i><br>
  </body>
</html>
EOF

This solution allows you to use all of the header modifiers and even add attachments if you wish.

  • It depends on which `mail` exactly you have installed. See https://stackoverflow.com/a/48588035/874188 – tripleee Apr 02 '23 at 17:21