1

我正在尝试使用 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>";
?>

邮件没有发送,在浏览器中我有空白页面,没有消息。这里有什么问题?

最好的问候, 达格纳

4

1 回答 1

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";
}
于 2012-06-15T08:40:28.447 回答