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>

相关文章

Optimizer与Debugger兼容性问题的解决方法

ZendExtensionManager.dll——Zend扩展插件管理器 Zend扩展插件管理器最简安装教程: 1、安装Zend Optimizer 3.3.0a:https://ww...

php+js iframe实现上传头像界面无跳转

上传头像,界面无跳转的方式很多,我用的是加个iframe那种。下面直接上代码。 html: 复制代码 代码如下: //route 为后端接口 //upload/avatar 为上传的头像...

使用WAMP搭建PHP本地开发环境

使用WAMP搭建PHP本地开发环境

写在前面的话 PHP是服务器脚本语言,所以需要在服务器上才能运行。作为新手,搭建服务器可能需要捣腾很久,有可能还搞不定。所以在入门阶段,为了把更多时间用在熟悉编程语言上,使用集成环境是最...

简单实现php上传文件功能

本文实例为大家分享了php上传文件功能的具体代码,供大家参考,具体内容如下 html: <form action="upload_file.php" method="post"...

简单谈谈PHP中的Reload操作

前言 有很多前辈告诫过我们,reload 能保证整个过程的平滑性,所谓平滑性指的是在 reload 的过程中,旧的进程在处理完当前请求前不会提前终止。很多年来,我从来没有质疑过这种说法,...