phpmailer发送gmail邮件实例详解

yipeiwu_com6年前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中引用&的用法分析【变量引用,函数引用,对象引用】

本文实例分析了php中引用&的用法。分享给大家供大家参考,具体如下: php的引用(就是在变量或者函数、对象等前面加上&符号) //最重要就是 删除引用的变量 ,只是引用的变量访问不了,...

PHP-Fcgi下PHP的执行时间设置方法

一般情况下设置PHP脚本执行超时的时间 一、在php.ini里面设置 max_execution_time = 1800; 二、通过PHP的ini_set 函数设置 ini_set("m...

php实现在新浪云中使用imagick生成缩略图并上传的方法

本文实例讲述了php实现在新浪云中使用imagick生成缩略图并上传的方法。分享给大家供大家参考,具体如下: imagick是一款php的图像处理插件了我们可以使用imagick来进行许...

php PDO异常处理详解

异常处理: PHP:默认为直接报错 MYSQL:默认为静默模式,错就错,不报错 PDO:默认为静默模式,错就错,不报错 以前,当PHP碰到错误的时候,会直接报错,错误处理会变得相当麻烦。...

浅析PHP程序设计中的MVC编程思想

PHP的MVC编程思想目前已经被广泛使用于各种大型项目的开发,很多成熟的MVC框架也逐渐被大家所熟知并被广泛应用于各类项目中,比较常见的如ThinkPHP、codeigniter、Sym...