1. 程式人生 > >TP5 傳送郵件程式碼

TP5 傳送郵件程式碼


傳送郵箱郵件方法


/**
 * 系統郵件傳送函式
 * @param string $tomail 接收郵件者郵箱
 * @param string $name 接收郵件者名稱
 * @param string $subject 郵件主題
 * @param string $body 郵件內容
 * @param string $attachment 附件列表
 * @return boolean
 */
function sendEmail($tomail, $name, $subject = '', $body = '', $attachment = null) {
    Vendor('phpmailer.phpmailer');
    $mail = new 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 = "222"; // SMTP伺服器密碼 $mail->SetFrom('[email protected]', '1600875665'); $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; }


呼叫公共方法


public function sendAllEmail(){
        
        //echo "sssssssssssssss";
         $toemail='[email protected]';
        $name='1600875665nnn';
        $subject='QQ郵件傳送測試';
        $content='恭喜你,郵件測試成功。';
        dump($this->sendEmail($toemail,$name,$subject,$content));
    }