dongli8466 2014-06-15 17:22
浏览 37
已采纳

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

help please send an email to.

I use a popular script phpmailer. examples on the page there is a pattern to send a message. I use a certain email as follows:

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

resulting in at run script does displays an inscription Message sent! _. however, the letter did not come to the specified mailbox. the problem is not clear because the error message is not displayed.

  • 写回答

1条回答 默认 最新

  • douye2036 2014-06-15 17:32
    关注

    Well, I am not sure if you really need it, but I did a class some time ago to send emails easily using PHPMailer, you can use it if you want:

    <?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;
            }
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能