Send emails through php. Although php provides the mail() function, it is not flexible enough and easy to use. PHPMailer is a good email sending tool and is very simple to use! The development environment is centos6.5+php. The specific steps are as follows:
1. Download class.phpmailer.php
The download address is: http://www.phpddt.com/usr/uploads/2012/11/ 3610674900.rar
2. Enable SMTP in the sender’s email address
The sender’s email address needs to enable the SMTP service. Different email addresses have different opening methods. You can enable it in the email settings. For details, see the help of the email address.
3. Create a new mailer.php with the following code:
);
include 'class.phpmailer.php';
try {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet='UTF-8'; / /Set character set
$mail->SMTPAuth = true;
$mail->Port = 25; ;Username = "your username"; //Your smtp account
$mail->Password = "your password"; //Your smtp password
$mail->IsSendmail();
$mail->From = "example@example.com"; //Customize the sending email address, the sending email address displayed by the recipient
$mail->FromName = "FromName"; //Customize the sender, the sender displayed by the recipient
$to = "example@example.com"; //The email address to send
$mail->AddAddress($to);
$mail->Subject = "Email subject";
$mail->Body = "Mail content";
$mail->IsHTML(false); //Whether it is set to html can be modified by yourself
$mail->Send();
echo 'ok'."n";
} catch ( phpmailerException $e) {
echo "failed".$e->errorMessage();
}
?>
IV. Modify the host name
It is worth noting that the email is sent The host needs to change the host name, otherwise the sending speed will be very slow. During the test, the domain name of the cloud host was used, and it could be sent. Other methods were unsuccessful.
5. Test
[root@xxxx testmail]# php mailer.php
ok The above has introduced how PHP uses Mailer to send emails, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.