0

我正在尝试通过 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);
4

6 回答 6

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手册

于 2012-11-20T08:39:38.880 回答
1

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

$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
于 2012-11-20T08:40:17.307 回答
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));
于 2012-11-20T08:44:20.943 回答
1

您需要定义正确的标题才能发送 HTML 格式的电子邮件。这可以非常简单地使用:

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

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

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

有关PHPMailer 类,请参见此处

于 2012-11-20T08:44:37.403 回答
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
    }
}

希望这个功能可以帮到你。尝试将此函数用作您的发送邮件,然后在底部尝试编写条件以查看您的邮件是否已提交。您只需要在其中输入即可

于 2012-11-20T08:48:25.333 回答
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";
于 2012-11-20T09:09:15.470 回答