Example of sending SMTP mail in PHP

When you are still struggling with php's built-in mail() function, you are very lucky now. This article can help you!

php makes use of the smtp class to send e-mail. I've been using it for a long time, but I haven't had any problems. In the background of this blog, when the blogger replies to the message, he will automatically send an email with a new reply prompt to the netizen, which is also realized by this method of this article.

The method of sending e-mail by smtp class is very simple and stable. The class is already written by others. You only need to call it. Several lines php source code Simple configuration can send mail, is not very looking forward to a try!

 1 <?php
 2   
 3 /**
 4  * @param $address mixed Array ($address1, $address1) / array (array ('address' = > $address1, 'nickname' = > $nickname1), array ('address' = > $address2, 'nickname' = > $nickname2)) is used when there are multiple recipients or the nickname of the recipient needs to be set
 5  * @param $subject string Mail theme
 6  * @param $body string Mail content
 7  * @param $file string Enclosure
 8  * @return bool|string Send successfully and return true; otherwise, return error message
 9  * @throws Exception
10  */
11 function send_mail_by_smtp($address, $subject, $body, $file = '')
12 {
13  require('./PHPMailer-master/Exception.php');
14  require('./PHPMailer-master/PHPMailer.php');
15  require('./PHPMailer-master/SMTP.php');
16   
17  //date_default_timezone_set("Asia/Shanghai");//Set up the eighth east zone of timing zone
18   
19  $mail = new PHPMailer();
20   
21  //Server settings
22  $mail->SMTPDebug = 2;
23  $mail->isSMTP();     // Use SMTP Mode sending
24  $mail->Host = 'smtp.126.com';    // SMTP Mailbox domain name
25  $mail->SMTPAuth = true;    // Enable SMTP Verification function
26  $mail->Username = "*****@126.com";   // Mailbox user name(complete email address)
27  $mail->Password = "*****";    // smtp Authorization code, non email login password
28  $mail->Port = 25;
29  $mail->CharSet = "utf-8";    //Set character set encoding "GB2312"
30   
31  // Set the sender's information to show you that I look like a good person (xxxx@126.com)
32  $mail->setFrom($mail->Username, 'You see me as a good guy');
33   
34  //Set recipient parameter 1 to recipient mailbox parameter 2 to add multiple recipients to the nickname set for the recipient
35  //$mail - > address ('******* @ 163. Com','I look like a good man to you ');
36   
37  if (is_array($address)) {
38  foreach ($address as $item) {
39  if (is_array($item)) {
40  $mail->addAddress($item['address'], $item['nickname']);
41  } else {
42  $mail->addAddress($item);
43  }
44  }
45  } else {
46  $mail->addAddress($address, 'adsf');
47  }
48   
49   
50  //Set replier parameter 1 to replier mailbox parameter 2 to the nickname set for the replier
51  //$mail->addReplyTo('*****@126.com', 'Information');
52   
53  if ($file !== '') $mail->AddAttachment($file); // Add attachments
54   
55  $mail->isHTML(true); //Is the message body html Code true or false
56  $mail->Subject = $subject; //Mail theme
57  $mail->Body = $body;  //Message body if isHTML Set up true´╝îCan be complete html String such as: use file_get_contents Function read html file
58  //$mail - > altbody = 'this is the body in plain text for non HTML mail clients'; / / additional information can be omitted
59   
60  return $mail->Send() ? true : 'ErrorInfo:' . $mail->ErrorInfo;
61 }
62   
63 $path = '.\wpic907.jpg';
64 $ret = send_mail_by_smtp('*******@163.com', 'PHPMailer Mail title', 'PHPMailer Mail content', $path);

Keywords: PHP encoding

Added by Crogge on Tue, 10 Dec 2019 17:18:51 +0200