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 验证身份证是否合法的函数

话不多说,请看代码: function is_idcard( $id ) { $id = strtoupper($id); $regx = "/(^\d{15}$)|(^\d...

PHP实现UTF8二进制及明文字符串的转化功能示例

本文实例讲述了PHP实现UTF8二进制及明文字符串的转化功能。分享给大家供大家参考,具体如下: <?php /***********本程序由云客编写。有空的时候承接ph...

php中的一些数组排序方法分享

A.内部排序(直接加载到内存进行排序):包括交换式排序(冒泡和快速法)、选择式排序、插入式排序 B.外部排序(因数据量大,需借助外部存储进行排序):包括合并排序、直接合并排序 【冒泡排序...

PHP求最大子序列和的算法实现

复制代码 代码如下: <?php //作者:遥远的期待 //QQ:15624575 //算法分析:1、必须是整数序列、2、如果整个序列不全是负数,最大子序列的第一项必须是正数,否则...

PHP版微信第三方实现一键登录及获取用户信息的方法

本文实例讲述了PHP版微信第三方实现一键登录及获取用户信息的方法。分享给大家供大家参考,具体如下: 注意,要使用微信在第三方网页登录是需要“服务号”才可以哦,所以必须到官方申请。 一开始...