Question : How to send email with attachments from Linux machine using mailx command ?
Answer :
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
here,
-vvv = Verbosity. -s = Specifies the subject. -r = Email sent from. -S = Specifies the smtp server.
Method 1 : using uuencode (old method)
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" user@domain.com
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" user@domain.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" user@domain.com < /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