phpmailer无法发送到gmail / yahoo邮件

时间:2014-09-29 05:00:15

标签: php email phpmailer

我有这个功能代码

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或其他邮件。感谢

1 个答案:

答案 0 :(得分:0)

我认为你需要定义你的主机,即$ mail-&gt; Host =&#39; smtp.gmail.com&#39;和港口号。 $ mail-&gt; Port = 465.or请找到下面的代码。

<?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!";
    }
?>