PHPMailer根本无法正常工作


PHPMailer is not working at all

我正在尝试配置 PHPMailer,我已经上传了 1 个文件,即 class.phpmailer.php并创建了另一个包含以下内容的 php 文件:

<?php
require('class.phpmailer.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$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 = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myemail@gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("the same email address");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("the same email address");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

我什么也得不到,不是成功信息,也不是失败信息。 http://www.mawk3y.net/mailtest/test.php

现在所有的答案都已经过时了。大多数最新版本(截至 2018 年 2 月)不再具有自动加载功能,PHPMailer 应按如下方式初始化:

<?php
  include_once(FCPATH.'PHPMailer/src/PHPMailer.php');
  include_once(FCPATH.'PHPMailer/src/SMTP.php');
  include_once(FCPATH.'PHPMailer/src/Exception.php');
    $msj="My complete message";
    $mail = new PHPMailer'PHPMailer'PHPMailer();
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    //authentication SMTP enabled
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    //indico el puerto que usa Gmail 465 or 587
    $mail->Port = 465; 
    $mail->Username = "xxxxxx";
    $mail->Password = "xxxx";
    $mail->SetFrom("xxxxxx@xxxxx.com","Name");
    $mail->AddReplyTo("xxx@xxx.com","Name Replay");
    $mail->Subject = "Test";
    $mail->MsgHTML($msj);
    $mail->AddAddress("xxxxxx@xxxxx.com");
 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }
?>

问题出在 require 方法上。

您首先必须提取phpMailer存储库的所有文件。

而不是写作

require('class.phpmailer.php');

你需要包括你的PHPMailerAutoload.php文件的路径,extracted.so 你可以用它替换它。

require('path-of-extracted-folder/PHPMailerAutoload.php');

有关更多参考,您可以访问它的GitHub链接

https://github.com/PHPMailer/PHPMailer

我有同样的问题,但我解决了。这就是我编码的方式

<?php 

require 'PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';              // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'abc@gmail.com';                 // SMTP username
$mail->Password = 'abc';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
//Set who the message is to be sent from
$mail->setFrom('abc1@gmail.com', 'First Last');
//Set an alternative reply-to address
//Set who the message is to be sent to
$mail->addAddress('abc@gmail.com', 'Shehan');
//Set the subject line
$mail->Subject = 'Test Mail';
$mail->Body = 'This is Test Mail';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
} 
?>