【问题标题】:PHPMailer send the messages with my server emailPHPMailer 使用我的服务器电子邮件发送消息
【发布时间】:2016-02-08 10:20:19
【问题描述】:

正如主题所写,PHPMailer 使用我的服务器电子邮件发送消息,而不是实际发件人的电子邮件。 例如:

来自:发件人姓名 myservername@server.com

发件人电子邮件未显示在部分的电子邮件中

这是我的代码

$result = mysql_query($insert_query, $connection) or die(mysql_error());

    if($result)
    {


    require_once('../se482/class.phpmailer.php');

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

//$mail->IsSMTP(true); 
$mail = new PHPMailer;                                     // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'myemail@gmail.com';                 // SMTP username
$mail->Password = 'mypassowrd';                           // SMTP password
$mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
 $mail->CharSet     = 'UTF-8';
  $mail->Encoding    = '8bit';
$mail->addAddress('myemail@gmail.com');     // Add a recipient
$mail->setFrom($email, $name);
//$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); 

$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  $mail->Send();
  $mail->SmtpClose();
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

    }

【问题讨论】:

  • 删除//$mail->IsSMTP(true); 上的评论以使用您设置的smtp
  • 如果我删除它,我会收到此错误:致命错误:在非对象上调用成员函数 IsSMTP()
  • 把它放在$mail = new PHPMailer; 下面
  • 我收到了这个额外的错误:无法发送邮件。邮件程序错误:SMTP 连接()失败。
  • 嗯。我假设你的usernamepassword 是正确的?

标签: php mysql email phpmailer


【解决方案1】:

$mail->from 设置为 $email,它没有在任何地方定义。

【讨论】:

  • 没有定义,但我没有在这里复制整个代码
【解决方案2】:

您还必须包含class.smtp.php 才能使用smtp。同时取消注释//$mail->isSMTP(true);

如文档中所述,为避免这种情况,您只能包含 PHPMailerAutoload.php 的自动加载器类

更新代码

$result = mysql_query($insert_query, $connection) or die(mysql_error());

    if($result)
    {


    //require_once('../se482/class.phpmailer.php');
    // require_once('../se482/class.smtp.php');
//or
  require_once('../se482/PHPMailerAutoload.php');

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP(true); 
$mail = new PHPMailer;                                     // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'myemail@gmail.com';                 // SMTP username
$mail->Password = 'mypassowrd';                           // SMTP password
$mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
 $mail->CharSet     = 'UTF-8';
  $mail->Encoding    = '8bit';
$mail->addAddress('myemail@gmail.com');     // Add a recipient
$mail->setFrom($email, $name);
//$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); 

$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  $mail->Send();
  $mail->SmtpClose();
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

    }

更新

您还添加了 app 凭据到 get_auth_token.php,如使用 gmail 文档中所述。这是链接https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

【讨论】:

  • 对不起,我怎样才能包含该类并使用 smtp?
  • 仍然出现同样的错误:无法发送邮件。邮件程序错误:SMTP 连接()失败
  • 你在用PHPMailerAutoload.php吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2020-05-09
  • 1970-01-01
相关资源
最近更新 更多