php mail()不发送样式

时间:2012-11-20 08:37:47

标签: php css email styles

我正在尝试通过PHP mail()函数发送样式信息和电子邮件。不幸的是,即使我的邮件客户端设置为接受样式化的电子邮件,它仍然只是呈现为纯HTML文本。

$to = $email;
    $subject = "Subject Details";
    $message='<html><body><table width="600" height="840" border="0" cellpadding="5" cellspacing="5">';
    $message.='<tr><td height="110"><img src="https://user.co.uk/images/logo.jpg" alt="SignDox" width="300" height="100" /></td></tr>';
    $message.='<tr><td height="29" bgcolor="#5C1561">&nbsp;</td></tr>';
    $message.='<tr><td height="537" valign="top">';
    $message.="Message Goes Here\n\n";
    $message.="Username: ".$new_agent_id."\n";
    $message.="Password: ".$pass1."\n\n";
    $message.="To sign in to your user panel follow this link: \n";
    $message.="Inside your admin section you will be able to change your username and password.\n";
    $message.='</td></tr>';
    $message.='<tr><td height="140" bgcolor="#5C1561">&nbsp;</td></tr></table></body></html>';
    $from = "no-reply@sender.co.uk";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

6 个答案:

答案 0 :(得分:4)

要发送HTML邮件,必须设置Content-type标头

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

注意:

1)如果打算发送HTML或其他复杂邮件,建议使用PEAR包»PEAR :: Mail_Mime。

2)值得注意的是mail()函数不适合循环中的大量电子邮件。此功能打开和关闭每个电子邮件的SMTP套接字,效率不高。

请详细阅读:PHP Manual

答案 1 :(得分:1)

根据需要设置内容类型和字符集。您还应该使用\r\n终止您的标题。

$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

答案 2 :(得分:1)

尝试类似

的内容
<?php
$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From:" . $from;
/*
$headers[] = "Bcc: JJ Chong <bcc@domain2.com>";
$headers[] = "Subject: {$subject}";  */ optional

mail($to, $subject, $message, implode("\r\n", $headers));

答案 3 :(得分:1)

您需要定义正确的标头才能发送HTML格式的电子邮件。这可以通过以下方式非常简单地完成:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

但是,我建议您使用PHPMailer类。该类将处理所有标题以及您可能需要的任何其他内容。您可以轻松添加附件,嵌入图像,通过SMTP等发送...

这是一个很棒的课程,更不用说你将学习对象和课程的数量,特别是如果你是PHP的新手: - )

请参阅此处查看PHPMailer Class

答案 4 :(得分:1)

function send_mail($from,$fromName,$to,$object,$bodyText,$bodyHtml){
    $site = "mywebsite.ca";
    $from = $fromName." <".$from.">";
    $limite = "_----------=_parties_".md5(uniqid (rand()));

    $header  = "Reply-to: ".$from."\n";
    $header .= "From: ".$from."\n";
    $header .= "X-Sender: <".$site.">\n";
    $header .= "X-Mailer: PHP\n";
    $header .= "X-auth-smtp-user: ".$from." \n";    
    $header .= "X-abuse-contact: ".$from." \n"; 
    $header .= "Date: ".date("D, j M Y G:i:s O")."\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: multipart/alternative; boundary=\"".$limite."\"";

    $message = "";
    $message .= "--".$limite."\n";
    $message .= "Content-Type: text/plain\n";
    $message .= "charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: 8bit\n\n";
    $message .= $bodyText;

    $message .= "\n\n--".$limite."\n";
    $message .= "Content-Type: text/html; ";
    $message .= "charset=\"iso-8859-1\"; ";
    $message .= "Content-Transfer-Encoding: 8bit;\n\n";
    $message .= $bodyHtml;

    $message .= "\n--".$limite."--";
    if(mail($to,$object,$message,$header)){
      //echo all
    }
    else{
      //echo mssage not submit
    }
}
希望这个功能可以帮到你。尝试使用此函数作为您的send_mail,然后在buttom尝试 写一个条件,看看你的消息是否已经提交。你只需要输入信息

答案 5 :(得分:0)

//your header should be like this
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";