Implementation code for PHPMailer mail sending

  • 2020-06-01 08:59:26
  • OfStack

Local environment: LAMP (ubuntu12.10);

The SMTP server USES stmp.163.com. At the beginning, it was painful. The first one I used was stmp.qq.com. As a result, all the emails I sent were treated as spam by tencent and could not be sent out.

I didn't say anything about it. Then I changed it to stmp.gmail.com, and opened the POP service in the Google mailbox, but I couldn't verify it was successful. Finally, register 1 163 and run the code successfully.

No pain, no pressure

The code is as follows:


<?php
 require("PHPMailer/class.phpmailer.php"); 
 require("PHPMailer/class.smtp.php");  

 $mail=new PHPMailer();

 //  Set up the PHPMailer use SMTP Server send Email
 $mail->IsSMTP();

 //  Sets the character encoding of the message, if not specified 'UTF-8'
 $mail->CharSet='UTF-8';

 //  Add recipient addresses, which can be used multiple times to add multiple recipients 
 $mail->AddAddress('*********@qq.com');

 //  Set the message body 
 $message='<B> This is a 1 Test email </B>';
 $mail->Body=$message;
 //  Set the message header From Field. 
 //  For netease SMTP Service, this part must be the same as your actual account, otherwise the validation will be wrong. 
 $mail->From='****@163.com';

 //  Set the sender name 
 $mail->FromName='yourname';

 //  Set the subject line 
 $mail->Subject=' Mail test ';

 //  Set up the SMTP The server. I'm using netease here SMTP The server. 
 $mail->Host='smtp.163.com';

 //  Set to "need validation" 
 $mail->SMTPAuth=true;

 //  Set the username and password, that is, the username and password for the netease mail. 
 $mail->Username='****';
 $mail->Password='****';

 //  Send an email. 
 $mail->Send();
 ?>

Line 2 and line 3 is PHP mailing package contains, other blog online download address is this http: / / phpmailer sourceforge. net /, but I didn't open, or in the writing.

Mine is under baidu. Baidu 1 can still be found.

PHP has an mail() function for sending emails, but I need to install an sendmail. I installed it but didn't send it. I don't know if there is some configuration problem or some reason, but it was finally implemented in this simple way.


Related articles: