【问题标题】:phpmailer can't send to gmail/yahoo mailphpmailer 无法发送到 gmail/yahoo 邮件
【发布时间】:2023-03-28 08:01:01
【问题描述】:

我有这个功能码

require_once("../includes/classMail/class.smtp.php");
require_once("../includes/classMail/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();  
$mail->Host     = $_SESSION['MailHost']; // SMTP servers

// sender   
$mail->From     = $_SESSION['UserEmail'];
$mail->FromName = $_SESSION['UserName'];

// receiver
unset($ArrExp);
$ArrExp=explode(",",$EmailTo);
for ($i=0;$i<count($ArrExp);$i++) {
    $mail->AddAddress($ArrExp[$i],"");
}   

if($EmailCC!="")
{
// CC to
    unset($ArrExp);
    $ArrExp=explode(",",$EmailCc);
    for ($i=0;$i<count($ArrExp);$i++) {
    $mail->AddCC($ArrExp[$i],"");
    }
}   


// ReplyTo
$mail->AddReplyTo("$_SESSION[UserEmail]","$_SESSION[UserName]");

// attachement File
$TxtAttach=explode("{:}",$TxtAttach);

$mail->WordWrap = 50;    // set word wrap
$mail->AddAttachment("../images/logoletter/$EmailAttach");      // attachment1

for ($i=0;$i<count($TxtAttach);$i++)
{
     $TxtAttach2=explode("{..}",$TxtAttach[$i]);
    // attachment2
     $mail->AddAttachment("../upload/$TxtAttach2[0]");
}
$mail->IsHTML(true);                               // send as HTML

$mail->Subject  =  $EmailSubject;
$mail->Body     =  $EmailBody;
$mail->AltBody  =  "";

if(!$mail->Send())
{
    return $mail->ErrorInfo;    
} else {
    return 1;
}       

此功能非常适合我的公司邮件,但我无法使用此功能发送到 gmail / yahoo 邮件并且没有错误显示

我该如何解决这个问题,以便我可以将邮件发送到 gmail/yahoo 或其他邮件。谢谢

【问题讨论】:

标签: php email phpmailer


【解决方案1】:

我认为您需要定义您的主机,即 $mail->Host = 'smtp.gmail.com' 和端口号。 $mail->Port = 465.或者请在下面找到代码。

<?php 
require 'PHPMailerAutoload.php';

    //Create a new PHPMailer instance
    $mail = new PHPMailer();
    //Tell PHPMailer to use SMTP
    $mail->IsSMTP(); 
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    //$mail->SMTPDebug = 2;

    //Ask for HTML-friendly debug output
    //$mail->Debugoutput = 'html';

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 465;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;

    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "test3@gmail.com";

    //Password to use for SMTP authentication
    $mail->Password = "test3123";

    $mail->setFrom('t3@gmail.com', 'development');  //add sender email address.

    $mail->addAddress('dev@gmail.com', "development");  //Set who the message is to be sent to.
    //Set the subject line
    $mail->Subject = $response->subject;

    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->Body     = 'Name: '.$data['name'].'<br />Location: '.$data['location'].'<br />Email: '.$data['email'].'<br />Phone:'.$data['phone'].'<br />ailment: '.$data['ailment'].'<br />symptoms: '.$data['symptoms'];

    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';

    //Attach an image file
    //$mail->addAttachment('images/phpmailer_mini.gif');
    //$mail->SMTPAuth = true;
    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
?>

【讨论】:

  • 我缺少的是 $mail->SMTPAuth = true; //用于 SMTP 身份验证的用户名 - 使用 gmail 的完整电子邮件地址 $mail->Username = "test3@gmail.com"; //SMTP认证使用的密码 $mail->Password = "test3123";
  • i 使用代码中的 SMPTAuth、用户名和密码。我的 CC 问题现在正在运行,问题出在 $EmailCC 和 $EmailCc (区分大小写问题)中。但感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 2017-11-04
  • 2013-09-06
  • 2014-11-30
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多