Question : How to send email with attachments from Linux machine using mailx command ?
Basic syntax to send emails from linux machine using mailx command is show below :
# echo "this is the body of the email" | mailx -vvv -s "test mail" -r "From" -S smtp="your-smtp" someone@address
-vvv = Verbosity. -s = Specifies the subject. -r = Email sent from. -S = Specifies the smtp server.
Method 1 : using uuencode (older way)
If the mailx version is below 12.x, you can use the uuencode command to send mails with attachments.
# uuencode [path/to/file] [name_of_attachment] | mailx -s "Subject" firstname.lastname@example.org
Method 2 : -a switch in mailx command
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.
# mailx -a file.txt -s "Subject" email@example.com
The above command will print a new blank line. Type the body of the message here and press [ctrl] + [d] to send. This will attach the file to the outbound email correctly with proper Content-Type and boundary headers.
To make this more “scriptable”, you can send the message without a body with the following:
# mailx -a file.txt -s "Subject" firstname.lastname@example.org < /dev/null
To send mails with a message body, replace /dev/null in above command with your message body file.
In the newer version of mailx, the headers that are used in outgoing email changed from:
From: Date: To: Subject:
to to the below form:
From: Date: To: Subject: User-Agent: MIME-Version: Content-Type: Content-Transfer-Encoding:
How to send mails with attachments using the solaris mailx command