我在 CentOS 上安装了 sendmail,但没有配置它并将其作为守护进程启动。我可以使用 sendmail 命令发送电子邮件吗? sendmail [电子邮件受保护] < ./myemailcontent I have no luck with this command. It seems to connect to 127.0.0.1 then times out. I think sendmail will connect to the remote smtp server(remotedomain.com) to deliver the email but it seems to use local smtp server to relay the email. How can I use sendmail to send an email?
sendmail
是发送电子邮件的“低级”工具/命令。它需要“原始”电子邮件。mail
。
如果您想发送简单的电子邮件并且更喜欢便携性,请查看下面的脚本:
#!/bin/sh
# sendmail or "sendmail look alike" provided by postfix/exim/...
SENDMAIL=/usr/sbin/sendmail
## Or use custom "sendmail look alike"
## e.g. msmtp which can send without local SMTP server
#SENDMAIL=/usr/bin/msmtp
[email protected]
$SENDMAIL -i -- $TO <<END_OF_EMAIL
Subject: My test message subject
To: $TO
X-Comment: Use empty line to separate email headers from email body
My test message body
END_OF_EMAIL
要使用sendmail,您首先需要安装postfix:
>> sudo apt-get install postfix
您必须通过运行
>> dpkg-reconfigure postfix
并按照您的说明来配置后缀设置。然后运行 >> service postfix reload
来运行该服务。
注意:
/usr/sbin/sendmail
通过使用 sendmail
命令来别名。实际上,您可以通过输入 which sendmail
来解决这个问题。因此,您无需输入文件名,只需输入 sendmail :)
使用
sendmail
:
示例:
>> sendmail [email protected]
Subject: Subject Line
... Email Body Here ...
然后在新行按
CTRL+D
,这将发送电子邮件
如果您发现电子邮件速度缓慢或无法正常工作,此链接对如何操作有很好的说明:https://www.digitalocean.com/community/questions/sendmail-is-slow-to-send-mail
如果您想使用 sendmail
添加
文件附件,我建议使用
uuencode
。要使用它,您需要安装:
>> sudo apt install sharutils
uuencode
将文件编码为电子邮件友好的文本 (https://linux.die.net/man/1/uuencode)
示例:
uuencode /path/to/file.txt /path/to/file.txt | sendmail "[email protected]"
请记住,您必须将
/path/to/file.txt
放置两次,因为它需要一个输入文件和一个输出,否则它将运行下面的命令行条目。
sendmail命令不是那么直观,也许你可以通过这个函数来使用它:
send_email() {
local recipient_email="$1"
local subject="$2"
local body="$3"
if [ -z "$recipient_email" ] || [ -z "$subject" ] || [ -z "$body" ]; then
echo "Usage: send_email <recipient_email> <subject> <body>"
return 1
fi
echo -e "Subject: $subject\n\n$body" | sendmail "$recipient_email"
}