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添加文字水印或图片水印的水印类完整源代码与使用示例

PHP实现的给图片添加水印功能,可添加文字水印或图片水印,使用文字水印时需要提供字体文件,使用图片水印时需要提供水印图片,水印图片不能比要添加水印的图片大,请使用背景透明的水印图片。 该...

dedecms模板标签代码官方参考

没想到dedecms可以这样DIY,牛啊地址:http://www.dedecms.com/archives/templethelp/help/ 在了解DedeCms的模板代码之前,了解...

解决PHP程序运行时:Fatal error: Maximum execution time of 30 seconds exceeded in的错误提示

解决分析: 这个错误是说你的php 执行时间越过了配置文件中设置的最大执行时间30秒钟,这不是你的程序本身存在的问题,而 是系统的配置文件问题,如果你的网速快的话,可能再执行一次就不会出...

PHP实现冒泡排序的简单实例

1、首先我们必须弄清楚什么是冒泡排序,不理解冒泡排序的原理,我们就无法写出代码。 冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第...

PHP获取一年中每个星期的开始和结束日期的方法

本文实例讲述了PHP获取一年中每个星期的开始和结束日期的方法。分享给大家供大家参考。具体分析如下: 最近项目中需要做个提交周报的功能,需要知道指定周数的开始日期和结束日期,以便处理其他业...