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>

相关文章

Laravel中log无法写入问题的解决

前言 账号登录报500错误,也没有返回错误信息,没办法只能使用原始方法,到现在一行一行打印。到 Log::info() 后面就无法正常显示了,那么问题就找到了。 导致无法写入日志的问题,...

PHP抽象类基本用法示例

本文实例讲述了PHP抽象类基本用法。分享给大家供大家参考,具体如下: <?php //抽象类像一个模板,供子类扩展(重写),抽象类里有普通方法(有方法体),也有抽象方法...

PHP 裁剪图片成固定大小代码方法

做一个首页调用图像,有时候往往需要获得固定大小的图像,因为首页的图像位置通常由设计人员指定好了,如果是做最新发布图像调用,因为不知道客户会上传什么比例的图像,所以,有时候也就没有办法确定...

php实现图片上传、剪切功能

本文实例为大家详细介绍了php实现图片上传、剪切功能的具体代码,供大家参考,具体内容如下 <?php defined('BASEPATH') OR exit('No d...

mac pecl 安装php7.1扩展教程

鉴于brew突然间的不能使用,简直是好坑的,以前多方便的安装啊,现在却那啥,好尴尬啊 brew search php ==> Searching local taps... p...