当前位置: 移动技术网 > IT编程>开发语言>PHP > phpmailer发送gmail邮件实例详解

phpmailer发送gmail邮件实例详解

2019年04月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网