PHP 邮寄 class 不会用 HTML 填充邮件
PHP Mailing class won't fill message with HTML
我有 PHP 邮件 class。问题是电子邮件消息不会随 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;
}
我认为如果删除以下行:
$message = "";
它应该按预期工作。否则,请提供您的 setMessage 方法代码。
编辑关于 OP 评论:
如果您想删除 html 标签,请查看 http://php.net/manual/en/function.strip-tags.php
$stripedMessage = strip_tags($message);
然后发送 $stripedMessage 而不是 $message;。
我有 PHP 邮件 class。问题是电子邮件消息不会随 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;
}
我认为如果删除以下行:
$message = "";
它应该按预期工作。否则,请提供您的 setMessage 方法代码。
编辑关于 OP 评论:
如果您想删除 html 标签,请查看 http://php.net/manual/en/function.strip-tags.php
$stripedMessage = strip_tags($message);
然后发送 $stripedMessage 而不是 $message;。