Example of using phpmailer to send emails
-
Require_once (dirname (_ FILE _). "/../PHPMailer/class. phpmailer. php ");
-
// Contains class. phpmailer. php
- /**
- * @ Param string $ send_to_mail target email
- * @ Param stinrg $ subject topic
- * @ Param string $ body content
- * @ Param string $ extra_hdrs additional information
- * @ Param string $ username recipient
- * @ Param string $ replyname reply
- * @ Param string $ replymail reply address
- * @ Return array (bealoon, string) the returned array contains two elements. bealoon indicates whether the operation is successful. string indicates the prompt information.
- */
- Function SendMail ($ send_to_mail, $ subject, $ body, $ extra_hdrs, $ username, $ replyname = "reply", $ replymail = "reply@reply.com "){
- $ Mail = new PHPMailer ();
- $ Mail-> IsSMTP (); // mail sending method
- $ Mail-> Host = "smtp.host.com"; // SMTP server Host address
- $ Mail-> SMTPAuth = true; // whether it is a trusted SMTP
- $ Mail-> Username = "reply@reply.com"; // SMTP user name Note: normal mail authentication does not need to add @ domain name
- $ Mail-> Password = "******"; // SMTP user Password
- $ Mail-> From = "send@send.com"; // The sender's email address
- $ Mail-> FromName = "send"; // sender
- $ Mail-> CharSet = "GB2312"; // specify the character set
- $ Mail-> Encoding = "base64 ";
- $ Mail-> AddAddress ($ send_to_mail, $ username); // add the target sending address
- $ Mail-> AddReplyTo ($ replymail, $ replyname); // add a reply address
- $ Mail-> IsHTML (true); // The mail type is in HTML format.
- $ Mail-> Subject = $ subject; // mail Subject
- // Email content
- $ Mail-> Body ="
- ". $ Body ."
- ";
- $ Mail-> AltBody = "text/html"; // content text format
- If (@! $ Mail-> Send ()){
- $ Results = array ("result" => false, "message" => $ mail-> ErrorInfo );
- Return $ results;
- } Else {
- $ Results = array ("result" => true, "message" => "the email has been sent to {$ send_to_mail }! ");
- Return $ results;
- }
- }
-
$ Send_mail = SendMail ($ to, $ subject, $ content, $ headers, $ name );
- If ($ send_mail ["result"]) {
- Echo $ send_mail ["message"];
- } Else {
- Echo $ send_mail ["message"];
- }
- Exit ();
- ?>
Example 2:
-
Include ('class/class. phpmailer. php ');
-
$ Config = array (
- 'Host' => 'smtp .163.com ',
- 'Port' => '25 ',
- 'User' => '***',
- 'Passwd' => '****',
- 'From' => 'juva _ zz@163.com ',
- 'Fromname' => 'zhengzhou ',
-
);
- $ Subject = 'This is a test mail ';
- $ Body ='
| Test Content |
| This is content |
';
- $ Address = '1970 @ qq.com ';
- $ Username = 'I ';
-
$ Mail = new PHPMailer ();
- $ Mail-> CharSet = 'gb2312 ';
- $ Mail-> IsSMTP ();
- $ Mail-> Host = $ config ['host'];
- $ Mail-> Port = $ config ['port'];
-
$ Mail-> From = $ config ['from'];
- $ Mail-> FromName = $ config ['fromname'];
- $ Mail-> SMTPAuth = true;
-
$ Mail-> Username = $ config ['user'];
- $ Mail-> Password = $ config ['passwd'];
-
$ Mail-> Subject = $ subject;
- $ Mail-> AltBody = "text/html ";
- $ Mail-> MsgHTML ($ body );
-
$ Mail-> AddAddress ($ address, $ username );
-
If (! $ Mail-> Send ())
- {
- Echo "Mail Error:". $ mail-> ErrorInfo;
- } Else
- {
- Echo "Congratulations! ";
- }
|