PHP Realizes the Sending Instance of SMTP Mail
- 2021-11-01 23:49:48
- OfStack
While you're still struggling that php's built-in mail () function can't send emails, you're lucky now, and this article can help you at this time!
php using smtp class to send mail is really tried and tested, I used for a long time, basically no problem. This blog backstage, when the blogger replies to the message, will automatically send a net friend a new reply prompt email is also used in this article this method to achieve.
smtp class send mail method is actually very simple, but also very stable, the class is someone else has been written, you just need to call on the line. A few lines of simple configuration can send mail, is it looking forward to trying 1!
<?php
/**
* @param $address mixed Recipient Multiple recipients / Or an array when you need to set the recipient nickname array($address1,$address1)/array(array('address'=>$address1,'nickname'=>$nickname1),array('address'=>$address2,'nickname'=>$nickname2))
* @param $subject string Mail Subject
* @param $body string Mail content
* @param $file string Annex
* @return bool|string Send successful return true On the contrary, an error message is returned
* @throws Exception
*/
function send_mail_by_smtp($address, $subject, $body, $file = '')
{
require('./PHPMailer-master/Exception.php');
require('./PHPMailer-master/PHPMailer.php');
require('./PHPMailer-master/SMTP.php');
//date_default_timezone_set("Asia/Shanghai");// Set time zone east 8 District
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP(); // Use SMTP Mode sending
$mail->Host = 'smtp.126.com'; // SMTP Mailbox domain name
$mail->SMTPAuth = true; // Enable SMTP Verification function
$mail->Username = "*****@126.com"; // Mailbox User Name ( Intact email Address )
$mail->Password = "*****"; // smtp Authorization code, non-mailbox login password
$mail->Port = 25;
$mail->CharSet = "utf-8"; // Set character set encoding "GB2312"
// Set the sender information, which is displayed as I look like a good man there to you (xxxx@126.com)
$mail->setFrom($mail->Username, ' I look like a good man there to you ');
// Set Recipients Parameter 1 Is the recipient mailbox Parameter 2 The nickname set for this recipient Add multiple recipients You can call it multiple times
//$mail->addAddress('********@163.com', ' I look like a good man there to you ');
if (is_array($address)) {
foreach ($address as $item) {
if (is_array($item)) {
$mail->addAddress($item['address'], $item['nickname']);
} else {
$mail->addAddress($item);
}
}
} else {
$mail->addAddress($address, 'adsf');
}
// Set the responder Parameter 1 Mailbox for the respondent Parameter 2 The nickname set for this respondent
//$mail->addReplyTo('*****@126.com', 'Information');
if ($file !== '') $mail->AddAttachment($file); // Add attachments
$mail->isHTML(true); // Is the message body html Code true Or false
$mail->Subject = $subject; // Mail Subject
$mail->Body = $body; // Message body If isHTML Set to true Can be complete html String Such as using file_get_contents Function to read html Documents
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // Additional information, which can be omitted
return $mail->Send() ? true : 'ErrorInfo:' . $mail->ErrorInfo;
}
$path = '.\wpic907.jpg';
$ret = send_mail_by_smtp('*******@163.com', 'PHPMailer Message header ', 'PHPMailer Mail content ', $path);