Original text: http://www.jbxue.com/php/25502.html
This article introduces two methods of sending emails in PHP. They are to use PHP’s built-in mail() function to send emails, and to use the email class that encapsulates the SMTP protocol. To send emails, it is recommended to use the email class encapsulated by SMTP protocol to send emails.
How to send emails in php? There are many methods, but the most commonly used one is to use the SMTP protocol to send emails. Let’s learn together.
Special recommendation: php email code collection
1. Use PHP’s built-in mail() function
Copy the code Code example:
$to = "test@163. com"; //Recipient
$subject = "Test"; //Subject
$message = "This is a test mail!"; //Text
mail($to,$subject,$message);
Report an error directly:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() inD:/www /Zend/email/email.php on line 10
You need a local SMTP server, the code is modified to:
Copy the code Code example:
$to = "test@163. com";//Recipient
$subject = "Test";//Email subject
$message = "This is a test mail!";//Email text
ini_set('SMTP','smtp.163.com ');//Sending SMTP server
ini_set('smtp_port',25);//Sending SMTP server port
ini_set('sendmail_from',"admin@163.com");//Sender's email address
mail($to,$subject,$message);
Continue error:
Warning: mail() [function.mail]: SMTP server response: 553 authentication is
required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S2 1301220947 inD:/www/Zend/email/email.php on line 9
Verification information is required. How to write verification information? Where to configure it?
To use the mail() function to send emails, you must have a mail server that can send emails without SMTP authentication. But today's SMTP mail servers basically require authentication, so if you want to use it to send emails, you can only set up a local SMTP server that does not require authentication.
Conclusion: To use the mail() function to send emails, you must have an SMTP server that does not require authentication.
2. Use an email class that encapsulates the SMTP protocol
It is recommended to use the SMTP protocol to send emails.
It is recommended to use the Mail class in the PEAR extension. It has powerful functions: it can support emails in plain text and HTML formats; encoding can be set for each field, and correct configuration will not cause Chinese garbled characters; it can support attachments, etc.
You can use the pear install Mail command on the server to quickly install it. Students who do not have sufficient server permissions can also directly download the PHP source code of the class and include it.
Note: The Mail class depends on Net/SMTP.php and Mail/mime.php, which need to be downloaded together and included together when using them.
Detailed installation methods can be viewed on the official website, http://pear.php.net/package/Mail.
Example, Mail class method of sending emails.
Copy code Code example:
// Pear Mail extension
require_once('Mail.php');
require_once('Mail/mime.php');
require_once('Net/SMTP.php');
$smtpinfo = array();
$smtpinfo["host"] = "smtp.163.com";//SMTP server
$smtpinfo["port"] = "25"; //SMTP server port
$smtpinfo["username" ] = "username@163.com"; //Sender's email
$smtpinfo["password"] = "password";//Sender's email password
$smtpinfo["timeout"] = 10;//Network Timeout, seconds
$smtpinfo["auth"] = true;//Login verification
//$smtpinfo["debug"] = true;//Debug mode
// Recipient list
$mailAddr = array( 'receiver@163.com');
//Sender display information
$from = "Name
// Recipient display information
$to = implode(' ,',$mailAddr);
// Email title
$subject = "This is a test email";
// Email body
$content = "Write anything you like
";
//Email text type, format and encoding
$contentType = "text/html; charset=utf-8";
//Newline symbol Linux: n Windows: rn
$crlf = "n"; $mime = new Mail_mime($crlf);
$mime->setHTMLBody($content);
$param['text_charset'] = 'utf-8';
$param['html_charset'] = 'utf- 8';
$param['head_charset'] = 'utf-8';
$body = $mime->get($param);
$headers = array();
$headers["From"] = $from;
$headers["To"] = $to;
$headers["Subject"] = $subject;
$headers["Content-Type"] = $contentType;
$headers = $mime-> ;headers($headers);
$smtp =& Mail::factory("smtp", $smtpinfo);
$mail = $smtp->send($mailAddr, $headers, $body);
$smtp->disconnect();
if (PEAR::isError($mail)) {
//Sending failed
echo 'Email sending failed: ' . $mail->getMessage()."n";
}
else{
//Sent successfully
echo "success!n";
}