Note: If your server is installed with the maximum virus protection function for selling coffee and opening an account, we need to turn off an email protection, otherwise you The email cannot be sent because it is intercepted by this antivirus.
1. Script sent using gmail
Copy the code as follows
include("class.phpmailer.php");
include("class.smtp.php");
//Get the content of an external file
$ mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[]",'',$body);
//Set smtp parameters
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//Fill in your gmail account and password
$mail-> Username = "yourname@gmail.com";
$mail->Password = "password";
//Set the sender, it is best not to forge the address
$mail->From = "yourname @gmail.com";
$mail->FromName = "Webmaster";
$mail->Subject = "This is the subject";
$mail->AltBody = $body;
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
//Set reply address
$mail->AddReplyTo(" yourname@gmail.com","Webmaster"(www.111cn.net));
//Add attachment, where the attachment and script are in the same directory
//Otherwise fill in the full path
$mail ->AddAttachment("attachment.jpg");
$mail->AddAttachment("attachment.zip");
//Set the email address and name of the email recipient
$mail-> AddAddress("toname@gmail.com","FirstName LastName");
//Send email using HTML format
$mail->IsHTML(true);
//Send email via Send method
//Process accordingly according to the sending result
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
2. Script to send email using 163
You only need to change the SMTP configuration and account password. The SMTP configuration is as follows
Copy the code as follows
//Set smtp parameters
//Note that the ssl protocol is not required here
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true;
$mail->Host = "smtp.163.com";
$mail->Port = 25;
To pass the test in the local wampserver environment, you need to enable the php_openssl extension.
Experience Sharing
Some friends refer to this tutorial to use Sina mail to log in and send emails. When you find that Sina mail does not support this function, in fact, this is not that Sina mail does not support this function. We need to find the pop3 protocol in Sina mail. The way to open an account is to log in to Sina. We have a settings button next to More on the right. After we click to enter, we will see an "Account Settings". After entering, there is a pop3 syndicate class at the bottom. We can open an account with it. , so you can try the above code again and it will be solved. Some friends say why 163 mail does not need to be set, because 163 mail opens pop3 by default.
from:http://www.111cn.net/phper/php-cy/61846.htm
You can telnet SMTP host 25 on Windows 2003 to see if the value is acceptable.
The SMTP of 163 should be smtp.163.com. It depends on the value you define.
telnet smtp.163.com 25
Pay attention to the encoding of the php file
Also set the encoding when sending
date_default_timezone_set("Asia/Shanghai");//Set the time zone Dongba District
$mail ->CharSet ="UTF-8";//Set the email encoding, the default is ISO-8859-1, if you send Chinese, this must be set