1. 程式人生 > >tp5發送email功能: PHPmailer

tp5發送email功能: PHPmailer

boolean err 由於 列表 功能 class charset func host

第一步:使用composer安裝phpmailer

  1. composer require phpmailer/phpmailer

第二步:common.php寫個發送郵件的函數

/**
 * 系統郵件發送函數
 * @param string $tomail 接收郵件者郵箱
 * @param string $name 接收郵件者名稱
 * @param string $subject 郵件主題
 * @param string $body 郵件內容
 * @param string $attachment 附件列表
 * @return boolean
 * @author static7 <[email protected]
/* */> */ function send_mail($tomail, $name, $subject = ‘‘, $body = ‘‘, $attachment = null) { $mail = new \phpmailar\PHPMailer(); //實例化PHPMailer對象 $mail->CharSet = ‘UTF-8‘; //設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼 $mail->IsSMTP(); // 設定使用SMTP服務 $mail
->SMTPDebug = 0; // SMTP調試功能 0=關閉 1 = 錯誤和消息 2 = 消息 $mail->SMTPAuth = true; // 啟用 SMTP 驗證功能 $mail->SMTPSecure = ‘ssl‘; // 使用安全協議 $mail->Host = "smtp.163.com"; // SMTP 服務器 $mail->Port = 465; // SMTP服務器的端口號 $mail->Username = "[email protected]
/* */"; // SMTP服務器用戶名 $mail->Password = ""; // SMTP服務器密碼 $mail->SetFrom([email protected], ‘xxx‘); $replyEmail = ‘‘; //留空則為發件人EMAIL $replyName = ‘‘; //回復名稱(留空則為發件人名稱) $mail->AddReplyTo($replyEmail, $replyName); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->AddAddress($tomail, $name); if (is_array($attachment)) { // 添加附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo; }

註意以上代碼,由於composer安裝的PHPmailer類在phpmailer包中因此實例化時正確路由是 new \phpmailer\PHPmailer()

第三步:控制器方法裏寫發送的內容

public function email() {
        $toemail[email protected];
        $name=‘static7‘;
        $subject=‘QQ郵件發送測試‘;
        $content=‘恭喜你,郵件測試成功。‘;
        send_mail($toemail,$name,$subject,$content);
    }

第四步:測試發送

博客鏈接:https://www.calm7.com/article/5.html

tp5發送email功能: PHPmailer