首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用phpmailer发送电子邮件?

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

Stack Overflow用户
提问于 2014-06-15 17:22:53
回答 1查看 620关注 0票数 0

帮助请发电子邮件到。

我使用了一个流行的脚本phpmailer。在页面上有一个模式来发送消息。我使用的电子邮件如下:

代码语言:javascript
复制
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!";
}

上,脚本确实显示了发送的铭文消息!_。但是,这封信没有到达指定的邮箱。由于没有显示错误消息,所以问题不清楚。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-15 17:32:42

嗯,我不确定你是否真的需要它,但是不久前我上了一门课,用PHPMailer轻松地发送电子邮件,如果你想要的话,你可以用它:

代码语言:javascript
复制
<?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;
        }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24232174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档