打开终端,输入命令:
sudo apt-get install sendmail
安装完
ps aux |grep sendmail
输出如下:
root 9391 0.0 0.3 8748 2000 ? Ss 00:14 0:00 sendmail: MTA: accepting connections
andy 13332 0.0 0.1 3236 792 pts/0 R+ 03:58 0:00 grep sendmail
证明sendmail已经成功启动了
sudo vi /etc/php5/apache2/php.ini
找到 sendmail_path,修改为:
sendmail_path = /usr/sbin/sendmail -i -t
重启apache
sudo apache2ctl restart
测试
常用发送邮件方式如下:
1.如何写一般的邮件: mail linux@ 编辑抄送对象,邮件主题,邮件正文后,按Ctrl-D结束
2.快速发送方式: echo “邮件正文” | mail -s 邮件主题 linux@
3.以文件内容作为邮件正文来发送: mail -s test linux@ < test.txt
4.发送带附件的邮件: uuencode 附件名称 附件显示名称 | mail -s 邮件主题 发送地址
例如: uuencode test.txt test.txt | mail -s Test linux@
<?php
$now = date("Y-m-d h:i:s");
$from_name = "yourname";
$from_email = "yourqq@qq.com";
$headers = "From: $from_name <$from_email>";
$body = "嗨,这是一封测试信件来自".$from_name."<".$from_email.">";
$subject = "[".$now."] 测试信件发送";
if (mail($from_email, $subject, $body, $headers)) {
echo "success!";
} else {
echo "fail";
}
?>