phpmailer发送gmail邮件实例详解

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail             = new PHPMailer();
$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.gmail.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "***@gmail.com";  // GMAIL username
$mail->Password   = "***";            // GMAIL password
$mail->SetFrom('****@gmail.com', 'First Last');
$mail->AddReplyTo("***@gmail.com","First Last");
$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "***@gmail.com";
$mail->AddAddress($address, "John Doe");
$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!";
}
?>
</body>
</html>

相关文章

PHP中使用asort进行中文排序失效的问题处理

PHP中有非常方便的对数组进行重新排序的方法——asort,关于asort的使用方法可以看 这里 。但是asort对含有中文key的数组进行排序时,有时候并不是按照字母顺序。这主要是编码...

微信自定义菜单的创建/查询/取消php示例代码

微信公众帐号 服务号可以使用 自定义菜单功能。之前在创建菜单时一直失败,原因是$data 格式一直没有传正确,后来终于解决了。这里先记录下 顺便封装了一个类,便于自定义菜单的管理。此类仅...

PHP魔术方法__GET、__SET使用实例

__get()  - 读取不可访问属性的值时,__get() 会被调用。 __set()  - 在给不可访问属性赋值时,__set() 会被调用。 复制代码 代码如下: <?...

专为新手写的结合smarty的类第1/3页

专为新手写的结合smarty的类,诚挚邀请大家多提宝贵意见 这个是为新手(也为自己)写的结合smarty一起使用的类,是未完成的,现在放出来的目的不是马上让新手使用,所以也没有把注解写的...

dedecms采集中可以过滤多行代码的正则表达式

过去用dede采集,总是过滤不掉一行以上的代码,只能一行行的过滤,在网上,发现有很多和我一样的菜鸟。随着不断的使用dede采集,对正则有了更进一步的了解,现在偶用一句正则表达式,同样可以...