PHPMailer 使用我的服务器电子邮件发送消息

标签 php mysql email phpmailer

如主题所述,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';
}

    }

最佳答案

您还必须包含 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';
}

    }

更新

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

关于PHPMailer 使用我的服务器电子邮件发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580865/

相关文章:

php - 将函数参数传递给内部函数?

php - 网站无法仅在 IE 中打开

java - 使用 Java Mail API 时如何转换文件名

java - 使用 ColdFusion 从 Outlook .msg 文件中提取附件

c# - SmtpClient无法发送;雷鸟可以

php - 获取登录 wordpress 的用户的电子邮件

php - 无法将 FOR UPDATE 附加到标准推进

php - 无法从 Reckon API 获取 token - 返回 SSL 错误

mysql fulltextsearch 处理短词

php - 我如何创建一个 MySQL 事件来自动添加从我的笔记本电脑中新移动的图像文件?