linux邮件发送
A. 如何在linux命令行中通过SMTP服务器发送电子邮件
假定你想配置一个 Linux 应用,用于从你的服务器或桌面客户端发送邮件信息。邮件信息可能是邮件简报、状态更新(如 Cachet)、监控警报(如 Monit)、磁盘时间(如 RAID mdadm)等等。当你要建立自己的 邮件发送服务器 传递信息时 ,你可以替代使用一个免费的公共 SMTP 服务器,从而避免遭受维护之苦。
谷歌的 Gmail 服务就是最可靠的 免费 SMTP 服务器 之一。想要从应用中发送邮件通知,你仅需在应用中添加 Gmail 的 SMTP 服务器地址和你的身份凭证即可。
使用 Gmail 的 SMTP
服务器会遇到一些限制,这些限制主要用于阻止那些经常滥用服务器来发送垃圾邮件和使用邮件营销的家伙。举个例子,你一次只能给至多 100
个地址发送信息,并且一天不能超过 500
个收件人。同样,如果你不想被标为垃圾邮件发送者,你就不能发送过多的不可投递的邮件。当你达到任何一个限制,你的 Gmail
账户将被暂时的锁定一天。简而言之,Gmail 的 SMTP 服务器对于你个人的使用是非常棒的,但不适合商业的批量邮件。
说了这么多,是时候向你们展示 如何在 Linux 环境下使用 Gmail 的 SMTP 服务器 了。
Google Gmail SMTP 服务器设置
如果你想要通过你的应用使用 Gmail 的 SMTP 服务器发送邮件,请牢记接下来的详细说明。
邮件发送服务器 (SMTP 服务器): smtp.gmail.com
使用认证: 是
使用安全连接: 是
用户名: 你的 Gmail 账户 ID (比如 "alice" ,如果你的邮箱为 alice@gmail.com)
密码: 你的 Gmail 密码
端口: 587
确切的配置根据应用会有所不同。在本教程的剩余部分,我将向你展示一些在 Linux 上使用 Gmail SMTP 服务器的应用示例。
从命令行发送邮件
作为第一个例子,让我们尝试最基本的邮件功能:使用 Gmail SMTP 服务器从命令行发送一封邮件。为此,我将使用一个称为 mutt 的命令行邮件客户端。
先安装 mutt:
对于 Debian-based 系统:
$ sudo apt-get install mutt
对于 Red Hat based 系统:
$ sudo yum install mutt
创建一个 mutt 配置文件(~/.muttrc),并和下面一样,在文件中指定 Gmail SMTP 服务器信息。将 替换成自己的 Gmail ID。注意该配置只是为了发送邮件而已(而非接收邮件)。
$ vi ~/.muttrc
set from = "@gmail.com"set realname = "Dan Nanni"set smtp_url = "smtp://@smtp.gmail.com:587/"set smtp_pass = ""
一切就绪,使用 mutt 发送一封邮件:
$ echo "This is an email body." | mutt -s "This is an email subject" alice@yahoo.com
想在一封邮件中添加附件,使用 "-a" 选项
$ echo "This is an email body." | mutt -s "This is an email subject" alice@yahoo.com -a ~/test_attachment.jpg
使用 Gmail SMTP 服务器意味着邮件将显示是从你 Gmail 账户发出的。换句话说,收件人将视你的 Gmail 地址为发件人地址。如果你想要使用自己的域名作为邮件发送方,你需要使用 Gmail SMTP 转发服务。
当服务器重启时发送邮件通知
如果你在 虚拟专用服务器(VPS)
上跑了些重要的网站,建议监控 VPS 的重启行为。作为一个更为实用的例子,让我们研究如何在你的 VPS
上为每一次重启事件建立邮件通知。这里假设你的 VPS 上使用的是 systemd,并向你展示如何为自动邮件通知创建一个自定义的 systemd
启动服务。
首先创建下面的脚本 reboot_notify.sh,用于负责邮件通知。
$ sudo vi /usr/local/bin/reboot_notify.sh
#!/bin/sh
echo "`hostname` was rebooted on `date`" | mutt -F /etc/muttrc -s "Notification on `hostname`" alice@yahoo.com
$ sudo chmod +x /usr/local/bin/reboot_notify.sh
在这个脚本中,我使用 "-F" 选项,用于指定系统级的 mutt 配置文件位置。因此不要忘了创建 /etc/muttrc 文件,并如前面描述的那样填入 Gmail SMTP 信息。
现在让我们创建如下一个自定义的 systemd 服务。
$ sudo mkdir -p /usr/local/lib/systemd/system$ sudo vi /usr/local/lib/systemd/system/reboot-task.service
[Unit]
Description=Send a notification email when the server gets rebooted
DefaultDependencies=no
Before=reboot.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/reboot_notify.sh
[Install]
WantedBy=reboot.target
在创建服务后,添加并启动该服务。
$ sudo systemctl enable reboot-task$ sudo systemctl start reboot-task
从现在起,在每次 VPS 重启时,你将会收到一封通知邮件。
通过服务器使用监控发送邮件通知
作为最后一个例子,让我展示一个现实生活中的应用程序,Monit,这是一款极其有用的服务器监控应用程序。它带有全面的 VPS 监控能力(比如 CPU、内存、进程、文件系统)和邮件通知功能。
如果你想要接收 VPS 上由 Monit 产生的任何事件的邮件通知,你可以在 Monit 配置文件中添加以下 SMTP 信息。
set mailserver smtp.gmail.com port 587
username "" password ""
using tlsv12
set mail-format {
from: @gmail.com
subject: $SERVICE $EVENT at $DATE on $HOST
message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.
Yours sincerely,
Monit
}
# the person who will receive notification emails
set alert alice@yahoo.com
这是一个因为 CPU 负载超载而由 Monit 发送的邮件通知的例子。
B. linux下如何发送mail,难道一定要开smtp服务吗
是的
Linux下的SMTP服务的配置,比较复杂一些,且各发行版本有些差异,以下是一个示例,供参考(建议使用第三个方法)
linux下smtp配置方法有几种,具体如下:
方法一,使用mail函数发送邮件;使用时时需要在本地系统上正确设置SMTP,否则将不能发送邮件。由于对系统的依赖性比较大,很多时候很不稳定,在一些提供虚拟主机服务的代理商中使用mail函数发送邮件往往很不好用,所以不推荐使用这种方法。
方法二,使用管道的形式发送邮件,主要是使用php中的popen函数。使用管道的方法发送邮件属于比较底层的操作,它取决于用户调用程序的稳定性。所以相比mail函数,这是一种可选的发送邮件的方式,但是这些本地的邮件系统都太复杂了,用户可能不会配置。
方法三(推荐),使用phpmailer。phpmailer类是一个开源的发送邮件类,可以从http://phpmailer.sourceforge.net官网下载,它含两个文件class.smtp.php和class.phpmailer.php。代码如下
include_once("class.phpmailer.php");
/**
* 定义邮件模块配制信息
*/
define("SMTP_HOST","smtp.mail.yahoo.com"); // SMTP 主机
define("SMTP_MAIL"," XXXX@yahoo.cn"); // SMTP 用户email
define("SMTP_PASS"," XXXX"); // SMTP 用的密码
define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP 用户email
define("SERVICE_NAME","PHPBOOK邮件测试"); // SMTP 用的名字
/**
* 使用phpmailer发邮件模块
*
* @param string $email
* @param string $user
* @param string $subject
* @param string $body
* @return bool
*/
function sendMail($email,$user,$subject,$body)
{
$mail = new PHPMailer();
//$this;
$mail->IsSMTP(); // 设置使用SMTP
$mail->Host = SMTP_HOST; // 设置SMTP服务器地址
$mail->SMTPAuth = true; // 打开SMTP权限验证
$mail->Username = SMTP_MAIL; // SMTP 用户名
$mail->Password = SMTP_PASS; // SMTP 服务器密码
$mail->From = SERVICE_MAIL; // 设置发送者地址
$mail->FromName = SERVICE_NAME; // 设置发送者名字
$mail->AddAddress($email, $user); // 添加接收者地址
$mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); // 设置回复地址
$mail->WordWrap = 50; // 设置显示格式
$mail->IsHTML(true); // 设置邮件支持html
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = ""; // 文本类型的邮件
if(!$mail->Send())
{
return $mail->ErrorInfo;
}
return true;
}
//开始发送测试邮件ng: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/xiehui/admin/mail/class.smtp.php on line 89
$tomail = " XXXX@126.com";
$user = " XXXXlinux";
$_mailSubject = "邮件测试示例!"; // 发给用户的邮件标题小组
$_mailBody = "新浪网"; // 邮件内容小组
sendMail($tomail,$user,$_mailSubject,$_mailBody);
?>
实验证明yahoo的smtp很好用,号称sina的其实并不好用,我卡在着好长时间。
方法四,给予socket编写的程序,源代码如下
使用socket发送邮件的封装类:
class sendmail{
var $lastmessage; //记录最后返回的响应信息
var $lastact; //最后的动作,字符串形式
var $welcome; //用在HELO后面,欢迎用户
var $debug; //是否显示调试信息
var $smtp; //smtp服务器
var $port; //smtp端口号
var $fp; //socket句柄
//发送邮件函数
function send_mail($smtp, $welcome="", $debug=false) {
if(empty($smtp)) die("SMTP不能为空!");
$this->smtp=$smtp;
if(empty($welcome)) {
$this->welcome=gethostbyaddr("localhost");
}else
$this->welcome=$welcome;
$this->debug=$debug;
$this->lastmessage="";
$this->lastact="";
$this->port="25";
}
//显示调试信息
function show_debug($message, $inout) {
if ($this->debug) {
if($inout=="in"){ //响应信息
$m='<< ';
}else
$m='>> ';
if(!ereg("\n$", $message))
$message .= "
";
$message=nl2br($message);
echo "${m}${message}";
}
}
//执行传递的命令
function do_command($command, $code) {
$this->lastact=$command;
$this->show_debug($this->lastact, "out");
fputs ( $this->fp, $this->lastact );
$this->lastmessage = fgets ( $this->fp, 512 );
$this->show_debug($this->lastmessage, "in");
if(!ereg("^$code", $this->lastmessage))
return false;
else
return true;
}
//邮件发送处理
function send( $to,$from,$subject,$message) {
//连接服务器
$this->lastact="connect";
$this->show_debug("连接到SMTP 服务器: ".$this->smtp, "out");
$this->fp = fsockopen ( $this->smtp, $this->port );
if ( $this->fp ) {
$this->set_socket_blocking( $this->fp, true );
$this->lastmessage=fgets($this->fp,512);
$this->show_debug($this->lastmessage, "in");
if (! ereg ( "^220", $this->lastmessage ) ) {
return false;
}else{
$this->lastact="HELO " . $this->welcome . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
$this->lastact="MAIL FROM: $from" . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
$this->lastact="RCPT TO: $to" . "\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
//开始发送邮件正文
$this->lastact="DATA\n";
if(!$this->do_command($this->lastact, "354")){
fclose($this->fp);
return false;
}
//开始处理邮件主题头
$head="Subject: $subject\n";
if(!empty($subject) && !ereg($head, $message)){
$message = $head.$message;
}
//开始处理邮件From头
$head="From: $from\n";
if(!empty($from) && !ereg($head, $message)) {
$message = $head.$message;
}
//开始处理邮件To头
$head="To: $to\n";
if(!empty($to) && !ereg($head, $message)) {
$message = $head.$message;
}
//处理结束串
if(!ereg("\n\.\n", $message))
$message .= "\n.\n";
$this->show_debug($message, "out");
fputs($this->fp, $message);
$this->lastact="QUIT\n";
if(!$this->do_command($this->lastact, "250")){
fclose($this->fp);
return false;
}
}
return true;
}else{
$this->show_debug("连接失败!!", "in");
return false;
}
}
}
?>
使用socket发送邮件示例:
include ("./sendmail.class.php");
$mail = new sendmail();
$email = "您好,这是一个测试邮件!";
$sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //显示调示信息
if($mail->send("XXXX@126.com", "XXXX@126.com", "测试SOCKET邮件", $email)) {
echo "发送成功!
";
}else{
echo "发送失败!
";
}
?>
C. linux 怎么发送邮件
可以从网页上进行发送邮件,或者用邮件客户端都可以,一般linux系统都自带邮件客户端,如Evolution等。
D. linux下发送邮件过程
发邮件:
mail -s “标题” 对方@地址.com
然后输入内容
完成时 输入 . 然后回车
E. Linux sendmail到底怎么往外发邮件
我用的是
sendemail
还是比较简单的
部分参数如下:
-f 表示from,发件人地址
-t 表示to,收件人地址
-s mail服务器域名
-u 主题
-xu 用户名(@之前的)
-xp 用户密码
-m 纯文本信息
-o message-file=/root/.. 发送文件中的内容
-a 发送附件 (-m,-o,-a可以同时使用)
需要注意的是填写服务器地址时最好添加上端口号
qq邮箱的端口号就不是默认的端口号
sudo apt-get install sendemail
建议在安装前先安装另外两个包:
libio-socket-ssl-perl libnet-ssleay-perl
F. Linux认证系统管理:linuxmail命令发送邮件失败
Linux认证系统管理:linuxmail命令发送邮件失败
使用linux自带的邮件功能测试邮件发送功能如下:
$ mail -s test xxx@163.com
sldkfjlskdjf[CTRL+D]
cc:
有时会出再提示:
You have a new mail in /var/spool/mail/root
并且在邮箱中看不到邮件;
解决办法:
1.查看出错的日志
$ tail -n 50 /var/spool/mail/root
From MAILER-DAEMON@ltv_73 Thu Dec 12 11:35:28 2013
Return-Path:
Received: from localhost (localhost)
by ltv_73 (8.13.8/8.13.8) id rBC3ZSPe006446;
Thu, 12 Dec 2013 11:35:28 +0800
Date: Thu, 12 Dec 2013 11:35:28 +0800
From: Mail Delivery Subsystem
Message-Id: <201312120335.rBC3ZSPe006446@ltv_73>
To:
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
boundary="rBC3ZSPe006446.1386819328/ltv_73"
Subject: Returned mail: see transcript for details
Auto-Submitted: auto-generated (failure)
This is a MIME-encapsulated message
--rBC3ZSPe006446.1386819328/ltv_73
The original message was received at Thu, 12 Dec 2013 11:35:18 +0800
from 41_154 [127.0.0.1]
----- The following addresses had permanent fatal errors -----
(reason: 530 5.7.1 Client was not authenticated)
----- Transcript of session follows -----
...while talking to mail.163.com.:
>>> MAIL From: SIZE=519
<<< 530 5.7.1 Client was not authenticated
554 5.0.0 Service unavailable
--rBC3ZSPe006446.1386819328/ltv_73
Content-Type: message/delivery-status
Reporting-MTA: dns; ltv_73
Received-From-MTA: DNS; sohu_41_154
Arrival-Date: Thu, 12 Dec 2013 11:35:18 +0800
Final-Recipient: RFC822; xxx@163.com
Action: failed
Status: 5.7.1
Diagnostic-Code: SMTP; 530 5.7.1 Client was not authenticated
Last-Attempt-Date: Thu, 12 Dec 2013 11:35:28 +0800
2.修改DNS
$ vim /etc/resolv.conf
修改成可用的DNS,这个地方的修改保存后即生效;
3.修改主机名【也可不做这一步】
$ sysctl kernel.hostname=newhostname
4.重启邮件服务
$ service sendmail restart
;