PHP A simple way to send emails using PHPMailer

  • 2020-11-18 06:08:48
  • OfStack

Recently, we need to use the email function, which was originally sent using mail() function that comes with PHP. php mail() this method is very simple, convenient and easy to use. However, except netease mailbox, QQ mailbox, GMAIL mailbox and other commonly used mailboxes can be received, HOTMAIL, TOM, LIVE and other mailboxes can not receive this kind of mail after testing. So I turned to PHPMailer, the powerful mail-sending class.
Use some examples that come with the official version. Some will quote Mailer Error: Could not instantiate mail function. This is a mistake. After referring to some of the information, or write their own method. The code is simple enough to explain.

function mailto($nickname, $address, $id, $activation_code)
{
 date_default_timezone_set('PRC'); 
 include_once("class.phpmailer.php");

 $mail = new PHPMailer(); // defaults to using php "mail()"
 $mail->IsSMTP();
 $mail->Host = "smtp.163.com";   // SMTP  The server   
 $mail->SMTPAuth = true;              //  Open the SMTP  certification   
 $mail->Username = "nowamagic@163.com";  //  The user name 
 $mail->Password = "yourpassword";          //  password   

 //$body = file_get_contents('application/views/nmra/register.html');
 //$body = preg_replace('/\\\\/','', $body); //Strip backslashes
 $body = '<p><body style="margin: 10px;"></p>';
 $body .= '<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; ">';
 $body .= '<div align="center"><img src="images/phpmailer.gif" style="height: 90px; width: 340px"></div>';
 $body .= '<p>'.$nickname.' Hello,. </p>';
 $body .= '<p> Congratulations on becoming the first member of the Concise Society for The Study of Modern Magic '.$id.' Members. </p>';
 $body .= '<p> Association for the Study of Modern Magic ( NowaMagic Research Association ) is 1 A community of programming apes, siege lions, design lions and developers for technical exchange and topic discussion. I hope you can find interesting topics and like-minded friends here. </p>';
 $body .= ' Please click the following link to verify your email address, please note that the domain name is nowamagic.net : <a href="http://www.nowamagic.net/librarys/accounts/activation/?code="'.$activation_code.'" target="_blank">http://www.nowamagic.net/librarys/accounts/activation/?code='.$activation_code.'</a>';
 $body .= '<p> I wish work and study happy, life comfortable. </p>';
 $body .= '</div></body>';
 //echo $body;
 $mail->AddReplyTo("nowamagic@163.com","Gonn");
 $mail->SetFrom('nowamagic@163.com', 'Gonn');
 $mail->AddReplyTo("nowamagic@163.com","Gonn");
 $address = "252211974@qq.com";
 //$address = "nowamagic@gmail.com";
 $mail->AddAddress($address, $nickname);

 $subject = " Receive an email from Concise Modern Magic ";
 $mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";
 // optional, comment out and test
 $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
 $mail->MsgHTML($body);

 //$mail->AddAttachment("images/phpmailer.gif");      // attachment
 //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

 if(!$mail->Send()) {
  //echo "Mailer Error: " . $mail->ErrorInfo;
 } 
 else {
  //echo "Message sent!";
 }
}

To use OK, just introduce two PHP classes and write your own methods. The two classes are small and send emails quickly.
PHPMailer is a powerful mail class. Its main features are:
Support for message s/mime encrypted digital signature
Supports multiple TOs, CCs, BCCs and ES33en-ES34en emails
It can work on any server platform, so you don't have to worry about the WIN platform being unable to send emails
Support for text /HTML messages
image images can be embedded
Support for mail clients that do not support HTML reading
Powerful send mail debugging function debug
Custom mail header
Redundant SMTP server support
Support 8bit, base64, binary, and ES54en-ES55en codes
Text wraps automatically
Support multi-attachment sending function
Support for SMTP server validation
Successfully tested on Sendmail, qmail, Postfix, Gmail, Imail, Exchange and other platforms
Provided in the download file, including a detailed description of the content of the document and show

Related articles: