Sending Mail from Linux servers

Hi Experts,

Can some body help me with code to send mails from Linux server which is connected to internet.

Thanks in Advance

Regards,
GK GKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mankowitzCommented:
there are many different mail programs you can use, but the simplest is to type

%mail person@example.com

from the command line, using ^D to end the message.

Of course, you have to make sure that your linux machine doesn't have any blocked ports, has sendmail running, is associated with a DNS so that others can find you, has a trusted site that it can relay mail from, etc, etc.

here is a tutorial on setting up sendmail: http://computernetworkingnotes.com/network-administrations/sendmail-server.html
0
madunixCommented:
You could use sendmail,,,,

In /etc/mail/sendmail.mc file contains following line:
dnl define(`SMART_HOST',`smtp.your.provider')

Replace "smtp.your.provider" with the fully qualified domain name to look like this:
define(`SMART_HOST',`my.relay.server')

Then restart
# service sendmail restart
0
savoneCommented:
Is the machine already setup to send email?  

There is a lot that goes into sending mail.  You can configure postfix or sendmail to use your ISP email account to send mail which is the easiest way.  What flavor of linux are you using?
0
Ernesto Couso ArtilesNetwork And Systems AdministratorCommented:
Sendmail is a most popular smtp server used in most of Linux/Unix distribution. sendmail allows to send email from command line. Use below instructions to send email using ‘sendmail‘ command.

Create a file using following content.

# vim /tmp/email.txt
Subject: Terminal Email Send

Email Content line 1
Email Content line 2
Subject: line read as Subject of email.

Now send email using following command.

# sendmail user@example.com  < /tmp/email.txt
Read more: Install and Configure Sendmail on CentOS/RHEL

2. Using 'mail' Command

mail command is most popular command to send emails from Linux terminal. Use few of below examples to send email.

# mail -s "Test Email" user@example.com < /dev/null
-s is used for subject

To send email with attachment.

# mail -a /opt/backup.sql -s "Backup File" user@example.com < /dev/null
-a is used for attachments

Also we can add comma separated emails to send email to multiple recipients together.

# mail -s "Test Email"  user@example.com,user2@example.com < /dev/null
3. Using 'mutt' command

Mutt is basically use for reading mails from Linux terminal from local user mail boxes, also useful to read emails from POP/IMAP servers. Mutt command is little similar to mail command. Use few of below examples to send email.

# mutt -s "Test Email" user@example.com < /dev/null
Send email including attachment

# mutt  -s "Test Email" -a /opt/backup.sql user@example.com < /dev/null
4. Using 'SSMTP' Command

sSMTP allows users to send emails from SMTP server from Linux command line. For example to send email to user admin@example.com use following command. Now type your subject of email as below with keyword Subject. After that type your message to be sent to user, After finishing your message press CTRL+d (^d) to send email.

# ssmtp admin@example.com
Subject: Test SSMTP Email
Email send test using SSMTP
via SMTP server.
^d
Read more: How to Setup SSMTP Server on Linux

5. Using 'telnet' Command

As my experience all system administrators use telnet command to test remote port connectivity test or login to server remotely. Most of newbie in Linux doesn't know that we can send email using telnet also, which is better way to trouble shoot email sending problems. Below is an example of email sending.

Red marked text is the user input and remaining is the responses of that commands.

# telnet localhost smtp

Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 fbreveal.com ESMTP Sendmail 8.13.8/8.13.8; Tue, 22 Oct 2013 05:05:59 -0400
HELO yahoo.com
250 tecadmin.net Hello tecadmin.net [127.0.0.1], pleased to meet you
mail from: sender@tecadmin.net
250 2.1.0 sender@tecadmin.net... Sender ok
rcpt to: myemail@ymail.com
250 2.1.5 myemail@ymail.com... Recipient ok
data
354 Enter mail, end with "." on a line by itself
Hey
This is test email only

Thanks
.
250 2.0.0 r9M95xgc014513 Message accepted for delivery
quit
221 2.0.0 fbreveal.com closing connection
Connection closed by foreign host.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.