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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?