This article describes the example of the php mailer class calling the remote SMTP server to send emails. Share it with everyone for your reference, the details are as follows:
php mailer is a very useful PHP email sending module. It can call local SMTP to send emails, and it can also call remote SMTP to send emails. However, you need to pay attention to some things when using it, otherwise it will cause In the case where the sending fails or cannot be called at all, this article will expand on the problems and solutions I encountered when using this class, and briefly explain the usage of php mailer and precautions.
First download the phpmailer class library file, download it here, only one resource point is needed. Download address: http://www.bkjia.com/codes/27188.html
After downloading, place this file, class.phpmailer.php, in a directory of your project, and write this where you need to send emails:
<?php require 'class.phpmailer.php'; try { $mail = new PHPMailer(true); $body = file_get_contents('contents.html'); //邮件的内容写到contents.html页面里了 $body = preg_replace('//////','', $body); //Strip backslashes $mail->IsSMTP(); // tell the class to use SMTP $mail->SMTPAuth = true; // enable SMTP authentication $mail->Port = 25; // set the SMTP server port $mail->Host = "mail.yourdomain.com"; // 远程SMTP服务器 $mail->Username = "yourname@yourdomain.com"; // 远程SMTP 服务器上的用户名 $mail->Password = "yourpassword"; // 你的远程SMTP 服务器上用户对应的密码 //$mail->IsSendmail(); //告诉这个类使用Sendmail组件,使用的时候如果没有sendmail组建就要把这个注释掉,否则会有 $mail->AddReplyTo("yourname@yourdomain.com","First Last"); $mail->From = "fromname@yourdomain.com"; $mail->FromName = "First Last"; $to = "toname@domain.com"; $mail->AddAddress($to); $mail->Subject = "First PHPMailer Message"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->WordWrap = 80; // set word wrap $mail->MsgHTML($body); $mail->IsHTML(true); // send as HTML $mail->Send(); echo 'Message has been sent.'; } catch (phpmailerException $e) { echo $e->errorMessage(); } ?>
Note: The above $mail->IsSendmail(); needs to be commented out, otherwise if there is no sendmail component, the error "Could not execute: /var/qmail/bin/sendmail" will be prompted!
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP XML file operation skills", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "php Summary of String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of PHP Common Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.