search
Homephp教程php手册PHP借助phpmailer发送邮件,phpmailer发送邮件

PHP借助phpmailer发送邮件,phpmailer发送邮件

本地没有发邮件的服务器,借助现成的SMTP服务器发送邮件是个不错的选择,这里使用到的工具是phpmailer ( Version 5.2.0),SMTP服务器就选gmail和163。

1. 使用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";
}

2.使用163发送邮件的脚本

只需要更改SMTP配置和账户密码即可,SMTP配置如下

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

在本地wampserver环境下测试通过,需要开启php_openssl 扩展。

以上所述就是本文的全部内容了,希望大家能够喜欢。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool