GitHub地址: https://github.com/PHPMailer/PHPMailer
解压下载文件 将 class.phpmailer.php class.pop3.php class.smtp.php 三个文件放入 ThinkPHP/Library/Vendor/PHPMailer 文件中
下面的测试函数仅使用了 SMTP协议。
注意的问题
1. 使用QQ邮箱测试的时候,需要注意QQ邮箱开启SMTP时设置的独立密码,而且QQ邮箱使用ssl加密时,发送邮件失败;
2. 当不使用加密时,$mail->SMTPSecure =''; 最好设置为空,因为本身是有默认值的
~~~
function sendEmailTest(){
vendor('PHPMailer.class#phpmailer'); // 从PHPMailer目录导class.phpmailer.php类文件
vendor('PHPMailer.class#smtp'); // 从PHPMailer目录导class.phpmailer.php类文件
$mail = new PHPMailer;
$mail->SMTPDebug = 0; // Enable verbose debug output 0表示关闭调试,1为打开调试
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '18559259538@qq.com'; // SMTP username
$mail->Password = '654312ab'; // SMTP password
$mail->SMTPSecure = ''; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->CharSet ="UTF-8";//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->From = '18559259538@qq.com';
$mail->FromName = 'leviio';
$mail->addAddress('18559259538@qq.com', 'TESTNAME'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('leviio@qq.com', 'Informationeee'); // 回复设置 回复的邮件地址,回复的名字
//$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 测试头555';
$mail->Body = 'This is the HTML message body in bold!';
$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';
}
}
~~~