使用PHPMailer进行SMTP的问题

时间:2011-09-23 07:51:23

标签: php smtp phpmailer

我已将PHPMailer用于SMTP,并且发送邮件时出现问题“Mailer Error:以下发件人地址失败:no-reply@mydomain.org.uk”

我的代码如下:

        $mail = new PHPMailer();

        $mail->IsSMTP();                                   // send via SMTP

        $mail->Host = "localhost;"; // SMTP servers

        $mail->SMTPAuth = true;     // turn on SMTP authentication

        $mail->Username = "";  // SMTP username

        $mail->Password = ""; // SMTP password



        $mail->From = $email_address;

        $mail->FromName = $email_address;

        $mail->AddAddress($arrStudent[0]["email"]);

        $mail->WordWrap = 50;                              // set word wrap

        $mail->IsHTML(true);                               // send as HTML



        $mail->Subject = "Subject";

        $theData = str_replace("\n", "<BR>", $stuff);

        $mail->Body = $theData; // "This is the <b>HTML body</b>";

        $mail->AltBody = $stuff;




        if (!$mail->Send()) {

            $sent = 0;

            echo "Mailer Error: " . $mail->ErrorInfo;

            exit;

        }

我研究了一切,当我在class.smtp.php中调试时发现错误函数“get_lines()”返回错误值“550 Authentication failed”

之前的代码工作正常,我想知道这个问题是如何突然出现的。 不顾一切地帮忙。

谢谢, Biplab

3 个答案:

答案 0 :(得分:1)

public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
    require_once('mailer.class.php');
    $mailer = new PHPMailer();
    //do we use SMTP?
    if ( USE_SMTP ) {
        $mailer->IsSMTP();
        $mailer->SMTPAuth = true;
        $mailer->Host = SMTP_HOST;
        $mailer->Port = SMTP_PORT;
        $mailer->Password = '';
        $mailer->Username = '';
        if(USE_SSL)
            $mailer->SMTPSecure = "ssl";
    }

    $mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
    $mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );

    $mailer->AddAddress($to);
    $mailer->Subject = $subject;
    //$mailer->WordWrap = 100;
    $mailer->IsHTML ( TRUE );
    $mailer->MsgHTML($body);

    require_once('util.class.php');
    $mailer->AltBody  =  Util::html2text ( $body );

    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

    if ( ! $mailer->Send() ) {
        return FALSE;
    }
    else {
        $mailer->ClearAllRecipients ();
        $mailer->ClearReplyTos ();
        return TRUE;
    }
}

我已经习惯了......应该使用SetFrom代替From ...那是你的错误伙伴... :))

答案 1 :(得分:0)

尝试将下一行添加到php.ini

extension=php_openssl.dll

重新启动并重试

答案 2 :(得分:0)

我正在使用YII的Mailer和PHPMailer,这对我有用:

$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username           = $this->SMTP_USERNAME;  // SMTP username
$mail->Password           = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth           = true;
$mail->From               = $this->fromAddress;
$mail->Host               = $this->SMTP_SERVER_ADDRESS;
$mail->FromName           = $this->fromName;
$mail->CharSet            = 'UTF-8';
$mail->Subject            = Yii::t('mailer', $this->subject);
$mail->Body               = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();

希望有帮助吗?