PHP uses phpmailer to send emails

墨辰丷
Release: 2023-03-31 12:44:01
Original
2244 people have browsed it

phpmailer is a free php email sending plug-in. We can use phpmailer to log in to our designated email and then use this email to automatically send us emails. This free email address like 163 and Sina all support it. Okay Without further ado, let’s take a look at a phpmainer 163 email to send an email.

There is no local email server. It is a good choice to use a ready-made SMTP server to send emails. The tool used here is phpmailer (Version 5.2.0), and the SMTP servers are gmail and 163.

1. Script to send using gmail

include("class.phpmailer.php");
include("class.smtp.php");
//获取一个外部文件的内容
$mail       = new PHPMailer();
$body       = file_get_contents('contents.html');
$body       = eregi_replace("[\]",'',$body);
//设置smtp参数
$mail->IsSMTP();
$mail->SMTPAuth  = true;
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = "ssl";
$mail->Host    = "smtp.gmail.com";
$mail->Port    = 465;
//填写你的gmail账号和密码
$mail->Username  = "yourname@gmail.com";
$mail->Password  = "password";
//设置发送方,最好不要伪造地址
$mail->From    = "yourname@gmail.com";
$mail->FromName  = "Webmaster";
$mail->Subject  = "This is the subject";
$mail->AltBody  = $body;
$mail->WordWrap  = 50; // set word wrap
$mail->MsgHTML($body);
//设置回复地址
$mail->AddReplyTo("yourname@gmail.com","Webmaster");
//添加附件,此处附件与脚本位于相同目录下
//否则填写完整路径
$mail->AddAttachment("attachment.jpg");
$mail->AddAttachment("attachment.zip");
//设置邮件接收方的邮箱和姓名
$mail->AddAddress("toname@gmail.com","FirstName LastName");
//使用HTML格式发送邮件
$mail->IsHTML(true);
//通过Send方法发送邮件
//根据发送结果做相应处理
if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 echo "Message has been sent";
}
Copy after login

2. Script to send email using 163

You only need to change the SMTP configuration and account password, the SMTP configuration is as follows

//设置smtp参数
//注意这里不需要ssl协议
$mail->IsSMTP();
$mail->SMTPAuth  = true;
$mail->SMTPKeepAlive = true;
$mail->Host    = "smtp.163.com";
$mail->Port    = 25;
Copy after login

To pass the test in the local wampserver environment, you need to enable the php_openssl extension.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php methods to operate MySQL database and session dialogue

Three commonly used methods in php Tree traversal techniques

Definition and usage of the PHPZip class

The above is the detailed content of PHP uses phpmailer to send emails. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!