WordPress uses PHPMailer to send gmail email

wordpress gmail messages sent using phpmailer

  • 0. guarantee for gmail imap service account has been opened, and you can normally access to gmail's smtp service. (Need to climb over the wall)

  • 1. Class introduced phpmailer

That there are class-phpmailer.php and class-smtp.php two files in wp-includes inside, you can copy them to the same directory that you need to write a file to send mail. Is then introduced into the code as follows:

include("class-phpmailer.php");
include("class-smtp.php");
  • 2. Set configuration 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;

Special attention to the use of tls, instead ssl. Port number is 587, host to smtp.gmail.com. Commissioning phase can be set SMTPDebug, should be deleted or otherwise mask setting this line.

  • 3. Set send content
$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);

The key is not how to use PHPMailer, the key is when we are in a particular technology framework for how to write with a good smell and particular wording of.

Guess you like

Origin www.cnblogs.com/freephp/p/12215311.html