PHP Mailing类不会用HTML填充消息

时间:2015-04-15 19:18:09

标签: php email message send

我有一个PHP邮件类。问题是电子邮件消息不会附带HTML它只是纯文本..我打赌任何其他程序员都看到我的错误..希望你们中的一些人可以!

调用我的函数的代码:

    GhostMailer::setHTML(true);
    GhostMailer::setSender(Input::get('emailfrom'));
    GhostMailer::addRecipient(Input::get('emailto'));
    GhostMailer::setSubject(Input::get('subject'));
    GhostMailer::setReturnAddress(Input::get('emailfrom'));
    GhostMailer::setMessage(Input::get('email'));
    GhostMailer::getHeaders();
    GhostMailer::send();

send()功能:

public static function send ()
{

    $message = self::$message;
    $head    = "";
    foreach ( self::$header as $key => $value ) { $head.= $key . ': ' . $value . self::EOL; }

    // If attachments given
    if ( count ( self::$attachments ) > 0 )
    {

        $separator = md5 ( time() );
        self::setHeaders ( 'Content-Type', 'multipart/mixed; boundary="' . $separator . '"' );

        $head = "";
        foreach ( self::$header as $key => $value ) { $head.= $key . ': ' . $value . self::EOL; }
        $head.= "Content-Transfer-Encoding: 7bit" . self::EOL;
        $head.= "This is a MIME encoded message." . self::EOL . self::EOL;

        // Preparing the message with proper formatting, charsets, content-types and encoding.
        $head .= "--" . $separator . self::EOL;
        $head .= "Content-Type: text/" . ( self::$isHTML ? 'html' : 'plain' ) . "; charset=\"iso-8859-1\"" . self::EOL;
        $head .= "Content-Transfer-Encoding: 8bit" . self::EOL . self::EOL;
        $head .= $message . self::EOL . self::EOL;
        $head .= "--" . $separator . self::EOL;

        $message = "";

        // Attach all given attachments to the mail
        foreach ( self::$attachments as $attached )
        {

            $tmp      = explode ( "/", $attached );
            $filename = end ( $tmp );

            $file_size = filesize ( $attached );


            try // Try to open the file
            {

                $handle  = fopen ( $attached, "r" );
                $content = fread ( $handle, $file_size );
                fclose ( $handle );

            }
            catch ( Exception $e )
            {

                die ( "[GhostMailer] FATAL ERROR IN ATTACHMENTS: Could not open file; Stacktrace: " . $e->getMessage () );

            }

            $content = chunk_split ( base64_encode ( $content ) );

            // attachment
            $head .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . self::EOL;
            $head .= "Content-Transfer-Encoding: base64" . self::EOL;
            $head .= "Content-Disposition: attachment" . self::EOL . self::EOL;
            $head .= $content . self::EOL . self::EOL;
            $head .= "--" . $separator . self::EOL;

        }

    }

    foreach ( self::$recipients as $recipient )
    {

        if ( ! mail (
            $recipient,
            self::$subject ,
            $message ,
            $head
        )
        ) {
            return false;
        }

    }

    return true;

}

1 个答案:

答案 0 :(得分:1)

我认为如果删除以下行:

$message = "";

它应该按预期工作。否则,请提供您的setMessage方法代码。

关于OP评论的编辑:

如果要删除html标签,请查看http://php.net/manual/en/function.strip-tags.php

$stripedMessage = strip_tags($message);

然后发送$ stripedMessage而不是$ message;。