HTML E-mail

Bash – send html using mailx

I’d like to send a html file from a host using mailx but instead of interpreting it as html it would send it out as text (html codes). Is there a way to send it as html?

Best Answer

You need to make it a multipart/mixed message, boundaries and all.

How to send HTML email using linux ‘mail’ command

    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  
## Prepare a temporary script that will serve as an editor. ## This script will be passed to ed. temp_script=$(mktemp) cat >"$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  

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'  

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 .

Linux – HTML email from heirloom mailx on linux

I am using Ubuntu 16.04 Xenial. Tried also on Ubuntu Server 16.04.

In order to send emails I am using the following function for sending mail using mailx (heirloom mailx in bash):

Where $2 is the attachment and $1 is the destination. Notes: 1. Files attached are printed inside the body as well but this could be useful to you if you wish just to send an html file with no attachments. 2. Using "-v" option prints verbose so you might get an issue with .mime.types that can be ignored. Remove the option if you don't want verbose on mailx. 3. You will still get the following in the body if you use the "-a" option: This is a multi-part message in MIME format. --=-=fFPa7dLqoSF1TGj-YDc2k8bdvmjpix_4sKFT=-= Content-Type: text/plain; charset=US-ASCII Content-Disposition: inline

In this case I am attaching a plain text file. Removing "-a $2" from the command and you are all set to print the html message. So the final result would be:

Try it out and let me know. I have tested on my end and it works.

Источник

How to Send HTML Email Using Linux Command Line

It is highly probable that all users of the internet have or are still using mailing platforms to communicate with one another from electronic gadgets like laptops. With a stable internet connection, Linux users on different Linux distributions can instantaneously send and receive electronic messages.

Structure of an Email

Since this article will be demonstrating to us how to send an Email via the Linux command-line environment, it is important for us to understand the raw structure of an email.

This raw structure can be broken down into the following segments:

  • Sender – The sender is a unique user-identified email address that acts as the source of the mail yet to be sent.
  • Receiver – The receiver is a unique user-identified email address that receives the emailed message from the sender.
  • Subject – This portion of an email details the summarized purpose of the mail.
  • Message – Message details a composition of the electronic message to be sent to the Receiver from the Sender.

HTML Structure

Also, since we will be sending HTML emails, the following preview of an HTML document structure is important:

In reference to the above HTML document skeleton, this tutorial is primarily concerned with HTML elements applicable between the and tags.

The need to know how to send an HTML email via the Linux terminal environment is an exceptional advantage to Linux users not bound to the Linux desktop environment. This user could mostly be on a server environment with limited GUI interaction.

Sending HTML Email Using Mail Command in Linux

This protocol-independent mail framework is rich and powerful enough to handle electronic mail transfer via the Linux terminal. It can be installed on various Linux OS distributions as follows:

$ sudo apt install mailutils [On Debian, Ubuntu and Mint] $ sudo yum install mailx [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a mail-client/mailx [On Gentoo Linux] $ sudo pacman -S mailutils [On Arch Linux] $ sudo zypper install mailutils [On OpenSUSE]

To send mail via mail command, reference the following syntax:

Its implementation is as follows:

We can add some HTML touch to this email.

Send HTML Mail in Linux Commandline

We can now comfortably send HTML Emails from the Linux Command Line environment.

Источник

How do I send HTML email using linux 'mail' command?

mail -s "subject" xxxxx@gmail.com

The scripts here are fine for simple English text with short lines, but proper MIME support requires you to handle non-ASCII character sets and arbitrarily long lines by wrapping them properly. In short, you can assemble a valid MIME structure with echo and cat , but for many real-world scenarios, you really really don't want to.

9 Answers 9

    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  
## Prepare a temporary script that will serve as an editor. ## This script will be passed to ed. temp_script=$(mktemp) cat >"$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  

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'  

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 .

Источник

How to send a html email with the bash command "sendmail"?

Is sendmail a requirement? I can do the same thing using basic POSIX mail but don't have access to sendmail.

8 Answers 8

( echo "From: $"; echo "To: $"; echo "Subject: $"; echo "Content-Type: text/html"; echo "MIME-Version: 1.0"; echo ""; echo "$"; ) | sendmail -t 

For troubleshooting msmtp, which is compatible with sendmail , see:

@NiKiZe MIME-Version is required if you use MIME features such as Content-Type: which obviously you need to use here.

True, it should be there to follow spec. however many clients will use the Content-Type header without caring for MIME-Version. If this is for an bigger audience ofcourse you should follow spec, but for a quick hack (if you need to type it out to send email to self) it might not be needed to get desired result.

If I understand you correctly, you want to send mail in HTML format using linux sendmail command. This code is working on Unix. Please give it a try.

echo "From: me@xyz.com To: them@xyz.com MIME-Version: 1.0 Content-Type: multipart/alternative; boundary='PAA08673.1018277622/server.xyz.com' Subject: Test HTML e-mail. This is a MIME-encapsulated message --PAA08673.1018277622/server.xyz.com Content-Type: text/html    Click Here   --PAA08673.1018277622/server.xyz.com " | sendmail -t 

For the sendmail configuration details, please refer to this link. Hope this helps.

If the content of email is pre-generated and restored in a file called content.html,how to send it to B from A with sendmail?

if you want to read the contents from an external file you can use some bash script to read lines from external html file and put in at data field of the email OR just simply copy paste the html code. Also, please have a look to link given by virus. Hope this helps.

The only part I'm not clear about is :boundary="PAA08673.1018277622/server.xyz.com". What does it mean?

I believe the boundary='. ' line needs to appended to the line right above it, the Content-Type. ; line, right? I tried to make the edit, but StackExchange blocked it as not enough characters to be changed. 🙁

I understand you asked for sendmail but why not use the default mail? It can easily send html emails.

Works on: RHEL 5.10/6.x & CentOS 5.8

cat ~/campaigns/release-status.html | mail -s "$(echo -e "Release Status [Green]\nContent-Type: text/html")" to.address@company.com -v 

It includes a script to send e-mail with a MIME attachment, ie with a HTML page and images included.

sendEmail -f "oracle@server" -t "name@domain.com" -u "Alert: Backup complete" -o message-content-type=html -o message-file=$LOG_FILE -a $LOG_FILE_ATTACH 
-a file Attach the given file to the message. 
Content-Type: text/html: No such file or directory 

To follow up on the previous answer using mail :

Often times one's html output is interpreted by the client mailer, which may not format things using a fixed-width font. Thus your nicely formatted ascii alignment gets all messed up. To send old-fashioned fixed-width the way the God intended, try this:

< echo -e "
" echo "Descriptive text here." shell_command_1_here another_shell_command cat  EOF > | mail -s "$(echo -e 'Your subject.\nContent-Type: text/html')" to.address@company.com 

You don't necessarily need the "Descriptive text here." line, but I have found that sometimes the first line may, depending on its contents, cause the mail program to interpret the rest of the file in ways you did not intend. Try the script with simple descriptive text first, before fine tuning the output in the way that you want.

Источник

Читайте также:  Узнать версию smb linux
Оцените статью
Adblock
detector