PHP 邮件发送

电子邮件的发送需要遵从特定的协议,常用的电子邮件协议包括SMTP、POP3、IMAP,其中邮件的创建和发送用SMTP协议即可。SMTP是Simple Mail TransferProtocol的简称,即简单邮件传输协议。

(2)PHP版本

利用PHPMailer来完成PHP的邮箱发送:

1、需要引入PHPMailer模块代码,源码地址为:https://github.com/PHPMailer/PHPMailer,可以在Clone or download下选择“Download ZIP”,下载的源码如下:


将其重命名为PHPMailer,并添加到ThinkPHP的Library下的Vendor下,如图:


2、在config.php中添加配置THINK_EMAIL参数:

'THINK_EMAIL' => array(
	'SMTP_HOST'     => 'smtp.163.com',   // SMTP 服务器的地址
	'SMTP_PORT'     => 25,                  // SMTP 端口
	'SMTP_SECURE'   => '',                  // 使用安全协议, '' 或 'ssl' 或 'tls'
	'SMTP_USER'     => 'timchen525@163.com', // 登录 SMTP 的用户名
	'SMTP_PASS'     => '********',          // 登录 SMTP 的密码
	'FROM_EMAIL'    => 'timchen525@163.com', // 发送邮件的邮箱
),

3、在function.php中添加函数:

function think_send_mail($to, $subject = '', $body = '', $attachment = null)
{
    $config = C('THINK_EMAIL');
    Vendor('PHPMailer.PHPMailerAutoload');   //从PHPMailer目录导class.phpmailer.php类文件
    $mail             = new PHPMailer(); //PHPMailer对象
    $mail->CharSet    = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
    $mail->isSMTP();  // 设定使用SMTP服务
    $mail->SMTPDebug  = 0;                      // 关闭SMTP调试功能
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                   // 启用 SMTP 验证功能
    $mail->Host       = $config['SMTP_HOST'];   // SMTP 服务器
    $mail->SMTPSecure = $config['SMTP_SECURE']; // 使用安全协议
    $mail->Port       = $config['SMTP_PORT'];   // SMTP服务器的端口号
    $mail->Username   = $config['SMTP_USER'];   // SMTP服务器用户名
    $mail->Password   = $config['SMTP_PASS'];   // SMTP服务器密码
    $mail->setFrom($config['FROM_EMAIL'], $config['FROM_NAME']);
    $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];
    $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];
    $mail->addReplyTo($replyEmail, $replyName);
    $mail->Subject    = $subject;
    $mail->AltBody    = '为了查看该邮件,请切换到支持 HTML 的邮件客户端';
    $mail->msgHTML($body);
    $addresses = explode(',', $to);
    foreach ($addresses as $address) {
        if (!empty($address)) {
            $mail->addAddress($address);
        }
    }
public function index(){
     $businessAlarmEmail = C('BUSINESS_ALARM_EMAIL');
     $title = 'timchen5345';
     $body = "各SSP返回如下";
     $result = think_send_mail($businessAlarmEmail,$title, $body);
     echo $result;
}

    if(is_array($attachment)){ // 添加附件
        foreach ($attachment as $file){
            is_file($file) && $mail->addAttachment($file);
        }
    }
    return  $mail->send() ? true : $mail->ErrorInfo;
}


4、编写测试代码

public function index(){
     $businessAlarmEmail = C('BUSINESS_ALARM_EMAIL');
     $title = 'timchen5345';
     $body = "各SSP返回如下";
     $result = think_send_mail($businessAlarmEmail,$title, $body);
     echo $result;
}

注意:

如果端口号被限制或者网络问题,或者密码服务器输入错误,都可能导致如下失败信息:

SMTP connect() failed.https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

成功执行的效果如图所示:

5、成功收到发送的邮件



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值