简体   繁体   English

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

[英]PHPMailer send the messages with my server email

as written on the subject, PHPMailer send the messages with my server email, not the email of the actual sender.正如主题上所写,PHPMailer 使用我的服务器电子邮件发送消息,而不是实际发件人的电子邮件。 for example:例如:

from: sendername myservername@server.com来自:sendername myservername@server.com

the sender email doesn't show up on the email from section发件人电子邮件未显示在来自部分的电子邮件中

here is my code这是我的代码

$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';
}

    }

You have to include class.smtp.php as well to use smtp .您还必须包含class.smtp.php才能使用smtp Also uncomment //$mail->isSMTP(true);同时取消注释//$mail->isSMTP(true);

As mentioned in documentation to avoid this you can include only autoloader class that is PHPMailerAutoload.php如文档中所述,为避免这种情况,您可以仅包含PHPMailerAutoload.php自动加载器类

updated code更新代码

$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';
}

    }

Update更新

You have also add your app credential in get_auth_token.php as mentioned in documentation for use gmail.您还在get_auth_token.php添加了您的app凭据,如使用 gmail 的文档中所述。 Here is link https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2这是链接https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

$mail->from设置为未在任何地方定义的$email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM