Five different examples to show you how to send an email using the Linux terminal. Learn how to send an email with the subject, attachment, and body from the terminal.
We have seen how to configure SMTP in Linux, in this post we will see different examples to send a mail through terminal. This post will elaborate on how to attach a file to mail, how to add a subject line, how to add body content to mail-in command-line interface.
Also read :
First of all, you need to check if SMTP is configured correctly and you are able to send mail from your Linux server. You can test it by sending test mail like below :
# echo test |sendmail -v firstname.lastname@example.org [<-] 220 mailserver.xyz.com ESMTP Postfix [->] HELO testsrv2 [<-] 250 mailserver.xyz.com [->] MAIL FROM:<email@example.com> [<-] 250 2.1.0 Ok [->] RCPT TO:<firstname.lastname@example.org> [<-] 250 2.1.5 Ok [->] DATA [<-] 354 End data with <CR><LF>.<CR><LF> [->] Received: by testsrv2 (sSMTP sendmail emulation); Fri, 23 Dec 2016 02:29:07 +0800 [->] From: "root" <email@example.com> [->] Date: Fri, 23 Dec 2016 02:29:07 +0800 [->] test [->] [->] . [<-] 250 2.0.0 Ok: queued as 19F75822B8 [->] QUIT [<-] 221 2.0.0 Bye
Once you receive test mail, it’s confirmed that your SMTP configurations are correct. Let’s see different examples to send mail.
1. An email with the subject line :
Sendmail is low-level utility hence it doesn’t support
-s option for a subject line like other email utilities do. We have to stdin subject to sendmail like below :
# echo "Subject : test" |sendmail -v firstname.lastname@example.org email@example.com... Connecting to mailserver.xyz.com via relay... 220 mailserver.xyz.com ESMTP Postfix >>> EHLO server2.xyz.com 250-mailserver.xyz.com 250-PIPELINING 250-SIZE 25600000 ---- output clipped -----
Text following “Subject:” will be treated as the subject line for mail.
You can also use
mailx command with
-s option to specify the subject line.
# mailx -s "test email subject line" firstname.lastname@example.org Your email body goes here... type and hit ctrl+d Cc:
2. An email with mail body :
If you have a long email body then you can save it in a file. Then you can pass that file to
sendmail command. Sendmail will parse the file and the content of files will be seen in the body of the email.
# cat /tmp/mail_body.txt | sendmail -v email@example.com firstname.lastname@example.org... Connecting to mailserver.xyz.com via relay... 220 mailserver.xyz.com ESMTP Postfix >>> EHLO server2.xyz.com 250-mailserver.xyz.com 250-PIPELINING 250-SIZE 25600000 250-VRFY
The same format works with
mailx command too. You can even source file using redirector for mail body like below:
# mailx user4.xyz.com < /tmp/mail_body.txt
3. Email with attachment
Sending attachment with sendmail/mail is a bit tricky. We need to encode files before sending as an attachment using
# uuencode /tmp/attachement.txt | mail -s "Subject line" user4.xyz.com
4. An email with different FROM field
You can even change from the field of the email so that you can send an email with nicer names in the FROM field rather than ugly account names which mail utility picks up from your account details. There are few options when one is not available on your system to try another.
# mailx -r email@example.com -s "subject" firstname.lastname@example.org # mailx -s "subject" user4.xyz.com -- -f email@example.com # mailx -s "subject" -a "From: Sender <firstname.lastname@example.org>" user4.xyz.com
5. Adding CC and BCC fields:
You can add BC and BCC fields too. Using options
-c cc and
-b for bcc:
# mail -s "test subject" -c email@example.com -b firstname.lastname@example.org email@example.com
How to add a subject line in sendmail command
You can add an email subject line in
sendmail by sending “Subject: xxxxx” to
sendmail command. See below examples –
(echo "Subject: Test email"; cat mailbody.txt) | sendmail -v firstname.lastname@example.org
Errors and logfile for email in Linux
Once you send an email check
/var/mail/maillog file for errors or success messages. If your configuration is correct and everything is working as expected you can see below message in there.
Sep 17 23:44:38 testsrv postfix/qmgr: 1086212AA0C: from=<root@testsrv>, size=470, nrcpt=1 (queue active) Sep 17 23:44:38 testsrv postfix/smtp: 1086212AA0C: to=<email@example.com>, relay=smtp.kerneltalks.com[10.10.1.1]:25, delay=4193, delays=4193/0.02/0.03/0.01, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 66AE24043) Sep 17 23:44:38 testsrv postfix/qmgr: 1086212AA0C: removed
It means your message is successfully sent to the SMTP server. If you still did not receive the messages in your mailbox then you should check with the SMTP server owner that if they have valid email address restriction. Since if you don’t configure sender address then command by default sent an email with TO address as
You can use
-r option in
mailx command to define TO email address as well which matches the policy of the SMTP server.
# echo test | mailx -s "Test from `hostname`" -r firstname.lastname@example.org -S smtp="smtp.kerneltalks.com" email@example.com
Like we have defined the sender address as
firstname.lastname@example.org in the above test command. Now, this email will be accepted by the SMTP server and will be delivered to the recipient.
Another error can be seen in
maillog is as below –
Sep 17 23:07:33 testsrv postfix/smtp: 1086212AA0C: to=<email@example.com>, relay=none, delay=1968, delays=1968/0.01/0.26/0, dsn=4.3.5, status=deferred (Host or domain name not found. Name service error for name=smtp.kerneltalks.com type=A: Host not found)
Solution: type A record, the host is not found means server is unable to resolve SMTP server name from DNS. Make sure you have DNS configured in
/etc/resolv.conf and SMTP name is registered in DNS. You can verify DNS resolution and DNS server being used using
nslookup smtp.kerneltalks.com command.
One more same error but for record type AAAA can be seen as below :
Sep 15 22:41:04 testsrv postfix/smtp: 97E9C12A7D9: to=<firstname.lastname@example.org>, relay=none, delay=349010, delays=349010/0.01/0.26/0, dsn=4.3.5, status=deferred (Host or domain name not found. Name service error for name=smtp.kerneltalks.com type=AAAA: Host not found)
Solution: This means the server is looking for an IPv6 record for the SMTP server. Edit
/etc/postfix/main.cf file and remove all protocols and define only ipv4 and you will be good.
# vi /etc/postfix/main.cf inet_protocols = ipv4