TP5 發送郵件代碼


發送郵箱郵件方法php

/**
 * 系統郵件發送函數
 * @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 = "15854827610@163.com";    // SMTP服務器用戶名
    $mail->Password = "222";     // SMTP服務器密碼
    $mail->SetFrom('15854827610@163.com', '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='1176962486@qq.com';
        $name='1600875665nnn';
        $subject='QQ郵件發送測試';
        $content='恭喜你,郵件測試成功。';
        dump($this->sendEmail($toemail,$name,$subject,$content));
    }
相關文章
相關標籤/搜索