前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于php5.5使用PHPMailer-5.2发送邮件

基于php5.5使用PHPMailer-5.2发送邮件

作者头像
Ryan-Miao
发布2018-10-15 17:21:20
9160
发布2018-10-15 17:21:20
举报
文章被收录于专栏:Ryan MiaoRyan Miao

PHPMailer - A full-featured email creation and transfer class for PHP。

在PHP环境中可以使用PHPMailer来创建和发送邮件。

最新版本(20181012)是PHPMailer 6.0.5,这个无法兼容php5.5以下的环境。由于我需要维护 php5.3的项目,需要切换到PHPMailer5.2来发送邮件。

下载地址: https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.24

基本使用

下载解压后。新建一个测试demo。

代码语言:javascript
复制
<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.exmail.qq.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'xxx@qq.com';                 // SMTP username
$mail->Password = 'yourpassword';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->setFrom('fromWho@qq.com', 'Mailer');
$mail->addAddress('toWhom@qq.com', 'Ryan Miao');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

开启SMTPDebug可以查看日志

代码语言:javascript
复制
 `0` No output
 `1` Commands
 `2` Data and commands
 `3` As 2 plus connection status
 `4` Low-level data output

错误信息保存在 $mail->ErrorInfo对象中。

保存为mail.php, 命令行执行

代码语言:javascript
复制
php mail.php

即可看到日志,以及邮件发送成功。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-10-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档