简体   繁体   English

如何在Linux中使用sendmail命令发送电子邮件

[英]How to send an email using sendmail command in linux

I tried the below two commands. 我尝试了以下两个命令。 1) From: mail_id To: Recepient_mail_id Hi, this is my message, and I'm sending it to you! 1)发件人:mail_id收件人:Recepient_mail_id您好,这是我的邮件,我正在将其发送给您! . 2) echo "My message" | 2)回显“我的消息” | sendmail -s subject Receipient_mail_id sendmail -s主题Receipient_mail_id

But didn't get any mail to the receipient's mail address. 但是没有收到任何收件人的邮件地址的邮件。

SMTP server is installed on another server ans it is in up and running. SMTP服务器已安装在另一台服务器上,并且已启动并正在运行。 So can anyone help me out on how to send a test email through that SMTP server using sendmail or smtp commands? 因此,有人可以帮助我解决如何使用sendmail或smtp命令通过该SMTP服务器发送测试电子邮件吗?

1. Using sendmail Command: 1.使用sendmail命令:

Created a file with email content: 创建一个包含电子邮件内容的文件:

$cat /tem/email.txt $ cat /tem/email.txt

Subject: Terminal Email Send 主题:终端电子邮件发送

Email Content line 1 电子邮件内容行1

Email Content line 2 电子邮件内容行2

Now send email using the following command: 现在,使用以下命令发送电子邮件:

$sendmail user@example.com < /tem/email.txt $ sendmail user@example.com </tem/email.txt

2. Using 'mail' Command: 2.使用“邮件”命令:

$mail -s "Test Subject" user@example.com < /dev/null $ mail -s“测试主题” user@example.com </ dev / null

Also, you can send an attachment with this command. 另外,您可以使用此命令发送附件。 Use -a for mailx and -A for mailutils. 使用-a表示mailx,使用-A表示mailutils。

$mail -a /opt/file.sql -s "Backup File" user@example.com < /dev/null $ mail -a /opt/file.sql -s“备份文件” user@example.com </ dev / null

Also, we can add comma separated emails to send the email to multiple recipients together. 另外,我们可以添加逗号分隔的电子邮件,以将电子邮件一起发送给多个收件人。

$mail -s "Test Email" user@example.com,user2@example.com < /dev/null $ mail -s“测试电子邮件” user @ example.com,user2 @ example.com </ dev / null

3. Using 'mutt' command: 3.使用“ mutt”命令:

$mutt -s "Test Email" user@example.com < /dev/null $ mutt -s“测试电子邮件” user@example.com </ dev / null

Send an email including an attachment 发送包含附件的电子邮件

$mutt -s "Test Email" -a /opt/backup.sql user@example.com < /dev/null $ mutt -s“测试电子邮件” -a /opt/backup.sql user@example.com </ dev / null

sendmail expects email in "raw" format. sendmail希望电子邮件采用“原始”格式。 Usually it is better to use higher level commands eg mail . 通常,最好使用更高级别的命令,例如mail

You may try the following shell script 您可以尝试以下shell脚本

#!/bin/sh

# sendmail command line optons:
# -i - do not treat lines starting with dot specially
# -t - read recipients lists from message headers: TO,CC,BCC
# -v - use verbose mode (describe what is happening) 

/usr/sbin/sendmail -i -t << MESSAGE_END
From: mail_id 
To: Recepient_mail_id 

Hi, this is my message, 
and I'm sending it to you! 
MESSAGE_END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM