且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:使用php邮件发送工具发送邮件

更新时间:2023-10-27 09:35:28

尝试添加内容类型为"multipart/alternative"的内容.从有效的我的功能(使用PEAR的Mail)中复制:

Try adding a content-type of "multipart/alternative". Copying from a function of mine that has worked (it uses PEAR's Mail):

protected function eF_mail_multipart($sender, $recipient, $subject, $textbody, $calendarbody, $onlyText = false, $bcc = false) {

        $hdrs = array('From'    => $sender,
                'Subject' => $subject,
                //'To'      => $recipient,
                'Date' => date("r"));
        if ($bcc) {
            //$hdrs['To'] = '';
        }

        $params = array("text_charset" => "UTF-8",
                "html_charset" => "UTF-8",
                "head_charset" => "UTF-8",
                "head_encoding" => "base64");


        $textparams = array(
                'charset'       => 'utf-8',
                'content_type'  => 'text/plain',
                'encoding'      => 'base64',
        );

        $calendarparams = array(
                'charset'       => 'utf-8',
                'content_type'  => 'text/calendar;method=REQUEST',
                'encoding'      => 'base64',
        );


        $email = new Mail_mimePart('', array('content_type' => 'multipart/alternative'));

        $textmime = $email->addSubPart($textbody, $textparams);
        $htmlmime = $email->addSubPart($calendarbody, $calendarparams);


        $final = $email->encode();
        $final['headers'] = array_merge($final['headers'], $hdrs);

        $smtp = Mail::factory('smtp', array('auth'      => $GLOBALS['configuration']['smtp_auth'] ? true : false,
                'host'      => $GLOBALS['configuration']['smtp_host'],
                'password'  => $GLOBALS['configuration']['smtp_pass'],
                'port'      => $GLOBALS['configuration']['smtp_port'],
                'username'  => $GLOBALS['configuration']['smtp_user'],
                'timeout'   => $GLOBALS['configuration']['smtp_timeout'],
                'localhost' => $_SERVER["HTTP_HOST"]));

        $result = $smtp -> send($recipient, $final['headers'], $final['body']);

        return $result;
    }