phpmailer发送gmail邮件实例详解
程序员文章站
2022-10-18 09:31:29
复制代码 代码如下:phpmailer - smtp (gmail) basic test<...
复制代码 代码如下:
<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>