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

时间:2015-11-07 08:44:26

标签: php mysql email phpmailer

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

来自:sendername 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';
}

    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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