databasehow tolinuxMiscunix

Sending an email in unix with attachments

Mailx is an easy way to send email from your unix/linux system.  With mailx you can get your server to send regular email or attach a file using your smtp server.  Ensure your sendmail is configured to let mailx send emails.  A good application for using mailx would be to send alerts, or process a file and then email it to somebody, extract data from your database or etl system and email the resulting data or file, etc.

Here is how to send an email :

mailx -s “Subject goes here” “toemail@address” “body goes here”

mailx -r “sender@emailaddress” -s “Subject” “toemail@address” “body goes here”

If the body is on a file :

mailx -s “Subject goes here” “toemail@address” < body.txt

If you want to send an attachment in the file :

uuencode data.csv data.csv|mailx –s “Subject goes here” “toemail@address”

If you want to send an email with body and attachment :

uuencode data.csv data.csv > datafile.csv  
cat body.txt datafile.csv > combined.file
mailx -s “Subject goes here” “toemail@address” < combined.file

The above three lines of code will enable you to send an email with body and an attachment.