css php mail()不发送样式

oipij1gg  于 2023-01-27  发布在  PHP
关注(0)|答案(6)|浏览(173)

我尝试通过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);
f3temu5u

f3temu5u1#

要发送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

fnvucqvd

fnvucqvd2#

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

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

但是,我建议你使用PHPMailer类。这个类将处理所有的头文件和你可能需要的任何东西。你可以很容易地添加附件,嵌入图片,通过SMTP发送等等...
这是一个非常棒的类,更不用说你将学习大量的对象和类,特别是如果你是一个PHP新手:-)
请参阅此处了解PHPMailer Class

yrefmtwq

yrefmtwq3#

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

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

14ifxucb4#

尝试一些类似

<?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));
wooyq4lh

wooyq4lh5#

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,然后在按钮写一个条件来看看你的消息是否被提交了。你只需要在里面给予

7uhlpewt

7uhlpewt6#

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

相关问题