我正在尝试向某人发送一封内容.html的邮件,此代码在 XAMPP 上工作正常,但在服务器上不起作用,只显示空屏幕并最终没有发送邮件,我做错了什么?
Gmail.php
<?php
include('/mailer/class.phpmailer.php');
include('/mailer/class.smtp.php'); // optional, gets called from within class.phpmailer.php if not already loaded
$hodemail = strtolower($branch)."hodofsrit@gmail.com";
echo '1';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
echo '2';
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxxxxxx";
$mail->Password = "xxxxxxxx";
$mail->SetFrom("134g1a05a1@srit.ac.in");
$mail->Subject = "Student Feedback ".$branch . " ".$yearandsem;
$mail->Body = "hello, Here's the graph generated";
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AddAddress($hodemail);
if(!$mail->Send())
{
$error = $mail->ErrorInfo;
header('Location:sendmail.php?errormsg=There was an error in sending email '.$error);
}
else
{
function redirect($url)
{
$string = '<script type="text/javascript">';
$string .= 'window.location = "' . $url . '"';
$string .= '</script>';
echo $string;
}
redirect('sendmail.php?msg=Email Successfully sent to corresponding HOD!');
}
?>
send_mail.pgp
<?php
session_start();
if(isset($_SESSION['admin'])){
$branch = $_POST['branch'];
$yearandsem = $_POST['yearandsem'];
include 'displaygraphs.php';
include'gmail.php';
}
else {
header('location:index.php?msg="Login First"');
}
?>
显示图工作正常,经过我用一些echo's
进行测试,我才知道页面在gmail.php的行$mail = new PHPMailer();
行之后停止,请帮我解决这个问题,谢谢!
只需评论$mail->IsSMTP();
..我有同样的问题..在本地主机上它工作,而在实时服务器上不工作。在我对此发表评论$mail->IsSMTP();
后,它工作正常..希望所以可能对你有帮助。
如果你删除$mail->IsSMTP()
,你将不会使用SMTP
!然后,PHPMailer 将使用内置的 mail()
函数,所有SMTP
特定设置都将被忽略!
如果在new PHPMailer()
失败,则可能无法加载库。检查邮件程序目录的路径,该路径显示为:
/mysrit/mailer/class.phpmailer.php
(注意开头/绝对)
或
mailer/class.phpmailer.php
(注意缺少开始/使路径相对)。
我建议使库成为必需的。那是:
require 'mailer/class.phpmailer.php';
或者,如果它可能被加载多次:
require_once 'mailer/class.phpmailer.php';
这样,脚本将在找不到邮件库时立即停止执行。