如何使用phpmailer发送电子邮件?

时间:2014-06-15 17:22:53

标签: php

帮助请发送电子邮件至。

我使用了一个流行的脚本phpmailerpage上的示例有一个pattern来发送消息。我按如下方式使用某个电子邮件:

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('prozaik81-2@yandex.ru', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer mail() test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//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.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

导致run脚本确实显示已发送的铭文消息! _。但是,这封信没有来到指定的邮箱。问题不明确,因为未显示错误消息。

1 个答案:

答案 0 :(得分:1)

嗯,我不确定你是否真的需要它,但我前段时间做了一个班级,使用PHPMailer轻松发送电子邮件,如果你愿意,可以使用它:

<?php

Namespace Email;
include_once 'class.phpmailer.php';

use PHPMailer;

Class Email {

    private $mail_host = "smtp host";
    private $mail_port = "smtp port";
    private $mail_user = "user";
    private $mail_pass = "pass";

    public function sendMail($fromName, $sendAddress, $cc, $bcc, $reply, $from, $subject, $body)
    {
        $mail = new PHPMailer(true);
        $mail->ClearAddresses();
        $mail->SetLanguage("es", "");
        $mail->CharSet = "UTF-8";
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = $this->mail_host;
        $mail->Port = $this->mail_port;
        $mail->Username = $this->mail_user;
        $mail->Password = $this->mail_pass;
        $mail->IsHTML(true);
        try {
            if ($cc != false) {
                if (is_array($cc)) {
                    foreach($cc as $value) {
                        $mail->AddCC($value);
                    }
                }
                else {
                    $mail->AddCC($cc);
                }
            }
            if ($bcc != false) {
                if (is_array($bcc)) {
                    foreach($bcc as $value) {
                        $mail->AddBCC($value[0]);
                    }
                }
                else {
                    $mail->AddBCC($bcc);
                }
            }
            if (is_array($sendAddress)) {
                foreach($sendAddress as $value) {
                    $mail->AddAddress($value);
                }
            }
            else {
                $mail->AddAddress($sendAddress);
            }

            $mail->AddReplyTo($reply, $fromName);
            $mail->SetFrom($from, $fromName);
            $mail->Subject = $subject;
            $mail->Body = $body;
            $mail->Send();
            return true;
        }

        catch(Exception $e) {
            error_log("exception: " . $mail->ErrorInfo, 0);
            return false;
        }
    }

}