Tuesday 30 June 2015

How to send mail using telnet and mail command


MX Record
An MX record comprises a FQDN and a priority. The priority is simply a number which is used to choose which mail server to use if multiple MX records exist for a domain name. A mail server trying to send an email to you will always try the lowest number priority first.

e.g <priority> hostname

How to find the MX (mail exchanges) of a domain/host name
#dig tel.example.com MX

How to send mail via telnet
#telnet tel.example.com 25
HELO                                                              <starts>
MAIL from: <sender@example.com>
RCPT to: <recipient@example.com>
DATA                                                              <enter the contents of email after DATA>
Hi how are you??
.                                                                        <END the message with a period(.)>

Attachments can be embedded using base64 encoding

Sending mails using mailx/mail command.

Install mailx command. 
#sudo yum install mailx

1: Simple mail
Run the following command, and then mailx would wait for you to enter the message of the email. You can hit enter for new lines. When done typing the message, press Ctrl+D and mailx would display EOT. After that mailx automatically delivers the email to the destination.
$ mail -s "This is the subject" someone@example.com
Hi someone
How are you
I am fine
Bye
EOT

2: Take message from a file
The message body of the email can be taken from a file as well.
$ mail -s "This is Subject" someone@example.com < /path/to/file
The message can also be piped using the echo command.
$ echo "This is message body" | mail -s "This is Subject" someone@example.com

3: Multiple recipients
To send the mail to multiple recipients, specify all the emails separated by a comma
$ echo "This is message body" | mail -s "This is Subject" someone@example.com,someone2@example.com

4: CC and BCC
The "-c" and "-b" options can be used to add CC and BCC addresses respectively.
$ echo "This is message body" | mail -s "This is Subject" -c ccuser@example.com someone@example.com

5: Specify From name and address
To specify a "FROM" name and address, use the "-r" option. The name should be followed by the address wrapped in "<>".
$ echo "This is message body" | mail -s "This is Subject" -r "Harry<harry@gmail.com>" someone@example.com

6: Specify "Reply-To" address
The reply to address is set with the internal option variable "replyto" using the "-S" option.
# replyto email
$ echo "This is message" | mail -s "Testing replyto" -S replyto="mark@gmail.com" someone@example.com

# replyto email with a name
$ echo "This is message" | mail -s "Testing replyto" -S replyto="Mark<mark@gmail.com>" someone@example.com

7: Attachments
Attachments can be added with the "-a" option.
$ echo "This is message body" | mail -s "This is Subject" -r "Harry<harry@gmail.com>" -a /path/to/file someone@example.com

8: Verbose - watch smtp communication
Use -v option with mailx




No comments:

Post a Comment