- 监控邮件的接收地址,最好绑定手机,这样可以收到手机短信提醒,比如我的就是139.com
- 发送侧的SMTP,不要选择sina.com之类。否则发送速度有延迟。
CentOS 7 需要安装 php-pear,其他依次查找下面四个模块。
php-pear-Auth
php-pear-Net-Socket
php-pear-Net-SMTP
php-pear-Mail
输入 pear list,确认是否已经安装
[root@localhost script]# pear list
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.11 stable
Auth_SASL 1.0.6 stable
Console_Getopt 1.3.1 stable
Mail 1.4.1 stable
Net_SMTP 1.7.3 stable
Net_Socket 1.0.14 stable
PEAR 1.9.4 stable
Structures_Graph 1.0.4 stable
XML_Util 1.2.1 stable
示例代码,用php-cli,脚本方式就能调用。
#!/bin/php
<?php
require_once("Mail.php");
$conf['mail'] = array(
'host' => 'smtp.sh163.net', //smtp
'auth' => true, //true表示smtp服务器需要验证,false代码不需要
'username' => 'myusername@sh163.net',
'password' => 'mypassword');
$headers['From'] = 'myusername@sh163.net';
$headers['To'] = 'mymobile@139.com';
$headers['Subject'] = 'test mail send by php';
$mail_object = &Mail::factory('smtp', $conf['mail']);
$body = 'hello world!!!';
$mail_res = $mail_object->send($headers['To'], $headers, $body); //发送
if( PEAR::isError($mail_res) ){ //检测错误
die($mail_res->getMessage());
}
echo 'Send finished.';
?>
测试通过!