CentOS 7 ,利用PHP SMTP发送监控邮件

  1. 监控邮件的接收地址,最好绑定手机,这样可以收到手机短信提醒,比如我的就是139.com
  2. 发送侧的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.';
?>

测试通过!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值