Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailx send html message [duplicate]

I want to send a html message with Mailx. When I try the following command

mailx -s "Subject"  [email protected]  < email.html  

I get the content of email.html in plain text. In the message the header Content-Type is set to text/plain. The -a option tries to send a file so I didn't find out how to modify the header. This answer almost worked, it sets well the Content-Type to text/html but doesn't substitute the default Content-Type which is text/plain.

mailx -s "$(echo -e "This is the subject\nContent-Type: text/html")" [email protected]  < email.html 

gives this result :

From: [email protected] To: [email protected] Subject: This is the subject Content-Type: text/html Message-ID: <538d7b66.Xs0x9HsxnJKUFWuI%[email protected]> User-Agent: Heirloom mailx 12.4 7/29/08 MIME-Version: 1.0  boundary="=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK"  This is a multi-part message in MIME format.  --=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline  <html> <body> <p>Helo wolrd</p> </body> </html> 

PS : I also tried with uuencode. When I try to display the message in the webmail I get a blank page...

like image 531
user3580716 Avatar asked Jun 03 '14 08:06

user3580716


People also ask

What is the difference between mailx and sendmail?

mailx is mail client. It can compose emails and deliver them to local mail transfer agent (sendmail, postfix, etc) which does actual sending to remote addresses. It can view and edit local user mailbox file. sendmail is mail server.

How do I write a message body in mailx?

Use the new attachment switch (-a) in mailx to send attachments with the mail. The -a options is easier to use that the uuencode command. The above command will print a new blank line. Type the body of the message here and press [ctrl] + [d] to send.

Is mailx a SMTP?

smtp Normally, mailx invokes sendmail(8) directly to transfer messages. If the smtp variable is set, a SMTP connection to the server specified by the value of this variable is used instead.

Is there a way to send mail that is not mailx?

With mail that ISN'T mailx -a is append header. A bit late for user3580716 but any linux system that can send mail should have a sendmail command that will work. It may not be actually sendmail, but a compatible mode of the system mailer (postfix, exim, whatever), but it should work for this case. There are many different versions of mail around.

What does -a mean in mailx -a?

With mailx -a is "append file". With mail that ISN'T mailx -a is append header. A bit late for user3580716 but any linux system that can send mail should have a sendmail command that will work. It may not be actually sendmail, but a compatible mode of the system mailer (postfix, exim, whatever), but it should work for this case.

Why can't I create a subject line in mailx?

The problem is, mailx already creates a content-type line. The command mail doesn't, but will not create a subject. This can be solved by adding a -m option, which will not put in the MIME type of the message. Also, you still need to insert the content-type in the header. Putting it in as first line in the text, doesn't work...

What are some alternatives to mail and mailx?

For example, both Ubuntu and Debian have several alternatives for mail and mailx. When composing a message, mail and mailx treats lines beginning with ~ as commands. If you pipe text into mail, you need to arrange for this text not to contain lines beginning with ~.


Video Answer


2 Answers

It's easy, if your mailx command supports the -a (append header) option:

$ mailx -a 'Content-Type: text/html' -s "my subject" [email protected] < email.html 

If it doesn't, try using sendmail:

# create a header file $ cat mailheader To: [email protected] Subject: my subject Content-Type: text/html  # send $ cat mailheader email.html | sendmail -t 
like image 161
dogbane Avatar answered Oct 04 '22 06:10

dogbane


There are many different versions of mail around. When you go beyond mail -s subject to1@address1 to2@address2

  • With some mailx implementations, e.g. from mailutils on Ubuntu or Debian's bsd-mailx, it's easy, because there's an option for that.

    mailx -a 'Content-Type: text/html' -s "Subject" to@address <test.html 
  • With the Heirloom mailx, there's no convenient way. One possibility to insert arbitrary headers is to set editheaders=1 and use an external editor (which can be a script).

    ## Prepare a temporary script that will serve as an editor.  ## This script will be passed to ed. temp_script=$(mktemp) cat <<'EOF' >>"$temp_script" 1a Content-Type: text/html . $r test.html w q EOF ## Call mailx, and tell it to invoke the editor script EDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <<EOF ~e . EOF rm -f "$temp_script" 
  • With a general POSIX mailx, I don't know how to get at headers.

If you're going to use any mail or mailx, keep in mind that

  • This isn't portable even within a given Linux distribution. For example, both Ubuntu and Debian have several alternatives for mail and mailx.

  • When composing a message, mail and mailx treats lines beginning with ~ as commands. If you pipe text into mail, you need to arrange for this text not to contain lines beginning with ~.

If you're going to install software anyway, you might as well install something more predictable than mail/Mail/mailx. For example, mutt. With Mutt, you can supply most headers in the input with the -H option, but not Content-Type, which needs to be set via a mutt option.

mutt -e 'set content_type=text/html' -s 'hello' 'to@address' <test.html 

Or you can invoke sendmail directly. There are several versions of sendmail out there, but they all support sendmail -t to send a mail in the simplest fashion, reading the list of recipients from the mail. (I think they don't all support Bcc:.) On most systems, sendmail isn't in the usual $PATH, it's in /usr/sbin or /usr/lib.

cat <<'EOF' - test.html | /usr/sbin/sendmail -t To: to@address Subject: hello Content-Type: text/html  EOF 
like image 32
Wakaru44 Avatar answered Oct 04 '22 05:10

Wakaru44