我正在尝试使用phpMailer发送邮件。这是我的代码:
<?php
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->PluginDir = "phpmailer/";
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "xxx@gmail.com";
$mail->Password = "xxxxx";
$mail->SetFrom('yyyya@gmail.com', 'Nasze imie i nazwisko');
$mail->AddAddress("email@anymail.pl"); // ADRESAT
$mail->Subject = 'Test message';
// w zmienną $text_body wpisujemy treść maila
$text_body = "Hello, \n\n";
$text_body .= "Sending succesfully, \n";
$text_body .= "PHPMailer";
$mail->Body = $text_body;
if(!$mail->Send())
echo "There has been a mail error <br>";
echo $mail->ErrorInfo."<br>";
// Clear all addresses and attachments
$mail->ClearAddresses();
$mail->ClearAttachments();
echo "mail sent <br>";
?>
邮件没有发送,在浏览器中我有空页,没有消息。这有什么不对?
祝你好运, 达格纳
答案 0 :(得分:4)
邮件没有发送,在浏览器中我有空白页面,没有消息。 这有什么不对?
尝试设置错误检查(仅限开发):
ini_set('display_errors', true);
error_reporting(1);
将两行放在页面顶部。这应该给你一些消息,例如准确的错误。
我像这样修改了class.smtp.php
文件的Connect
函数,让它为我自己工作:
public function Connect($host, $port = 0, $tval = 30) {
$host = "ssl://smtp.gmail.com";
$port = 465;
// other code
我的电子邮件发送代码是有效的;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxxxxxxxx@gmail.com"; // SMTP username
$mail->Password = "xxxxxxxxx"; // SMTP password
$webmaster_email = "xxxxxx@gmail.com"; //Reply to this email ID
$email = "xxxxxxx@gmail.com"; // Recipients email ID
$name = "Sarfraz"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Local Mail from Sarfraz";
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "I am a local mail !";
$mail->Body = "Hey What's up? Have fun :)"; //HTML Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}