PHP系統郵件發送函數

爲了自我學習和交流PHP(jquery,linux,lamp,shell,javascript,服務器)等一系列的知識,希望光臨本博客的人可以進來交流。尋求共同發展。搭建平臺。本人博客也有許多的技術文檔,希望可以爲你提供一些幫助。


QQ羣: 191848169   點擊鏈接加入羣【PHP技術交流(總羣)】



/**
 * 功能:系統郵件發送函數
 * @param string $to    接收郵件者郵箱
 * @param string $name  接收郵件者名稱
 * @param string $subject 郵件主題
 * @param string $body    郵件內容
 * @param string $attachment 附件列表
 * @return boolean
 */
function send_mail($to$name$subject ''$body ''$attachment = null, $config '') {
    $config is_array($config) ? $config : C('SYSTEM_EMAIL');
    import('PHPMailer.phpmailer', VENDOR_PATH);         //從PHPMailer目錄導class.phpmailer.php類文件
    $mail new PHPMailer();                           //PHPMailer對象
    $mail->CharSet = 'UTF-8';                         //設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼
    $mail->IsSMTP();                                   // 設定使用SMTP服務
//    $mail->IsHTML(true);
    $mail->SMTPDebug = 0;                             // 關閉SMTP調試功能 1 = errors and messages2 = messages only
    $mail->SMTPAuth = true;                           // 啓用 SMTP 驗證功能
    if ($config['smtp_port'] == 465)
        $mail->SMTPSecure = 'ssl';                    // 使用安全協議
    $mail->Host = $config['smtp_host'];                // SMTP 服務器
    $mail->Port = $config['smtp_port'];                // SMTP服務器的端口號
    $mail->Username = $config['smtp_user'];           // SMTP服務器用戶名
    $mail->Password = $config['smtp_pass'];           // SMTP服務器密碼
    $mail->SetFrom($config['from_email'], $config['from_name']);
    $replyEmail $config['reply_email'] ? $config['reply_email'] : $config['reply_email'];
    $replyName $config['reply_name'] ? $config['reply_name'] : $config['reply_name'];
    $mail->AddReplyTo($replyEmail$replyName);
    $mail->Subject = $subject;
    $mail->MsgHTML($body);
    $mail->AddAddress($to$name);
    if (is_array($attachment)) { // 添加附件
        foreach ($attachment as $file) {
            if (is_array($file)) {
                is_file($file['path']) && $mail->AddAttachment($file['path'], $file['name']);
            else {
                is_file($file) && $mail->AddAttachment($file);
            }
        }
    else {
        is_file($attachment) && $mail->AddAttachment($attachment);
    }
    return $mail->Send() ? true : $mail->ErrorInfo;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章