WordPress使用PHPMailer发送gmail邮件

wordpress使用phpmailer发送gmail邮件

  • 0.保证用于gmail账号已经开启imap服务,且你能正常访问到gmail的smtp服务。(需要climb over the wall)

  • 1.引入phpmailer相关类

原来在wp-includes里面有class-phpmailer.php和class-smtp.php两个文件,可以把它们拷贝到你需要编写发送邮件的那个文件的同级目录。然后引入代码如下:

include("class-phpmailer.php");
include("class-smtp.php");
  • 2.设置phpmailer配置
$mail = new PHPMailer();
$body = "Hi, my friend, just a test for audience! from [email protected]";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;

特别注意,要使用tls,而不是ssl。端口号为587,host为smtp.gmail.com。调试阶段可以设置SMTPDebug,否则应该删掉或者屏蔽这行的设置。

  • 3.设置发送内容
$mail->Username = "[email protected]";
$mail->Password = "xxxxxxx";

$body = "This is a test message not in awe of creation."; // 邮件内容
$mail->setFrom('[email protected]', 'HelpForEmail');
$mail->Subject = 'Ask for help now'; // 邮件主题
$mail->msgHTML($body);
$mail->CharSet = "utf-8";
$address = "[email protected]";
$mail->addAddress($address);

关键不在于怎么用PHPMailer,关键在于当我们处于某种特定技术框架下怎么样写出具有good smell 和 particular的写法。

猜你喜欢

转载自www.cnblogs.com/freephp/p/12215311.html