使用PHP heredoc功能通过PHPMailer发送带有HTML标签的电子邮件


Sending a email message having HTML tags via PHPMailer using PHP heredoc feature

当用户填写联系人表单时,我希望用户获得一条HTML消息,消息开头有一个图像,并为一个带阴影的框设置一些样式,其中包含用户的姓名和电话数字显示。

我尝试使用PHPmailer使用$mail->IsHTML(true)和$mail->msgHTML($message),但消息没有发送。

消息如下:

$message = <<<STARTMESSAGE
     <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'></script>
     <link href='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css' rel='stylesheet'>    
     <style type='text/css'>
        .box {
                margin: 40px;

                -moz-box-shadow: 5px 5px 5px rgba(68,68,68,0.6);
                -webkit-box-shadow: 5px 5px 5px rgba(68,68,68,0.6);
                box-shadow: 5px 5px 5px rgba(68,68,68,0.6);

                display: block;
        }
        .content {
            position:relative;
            padding:100px;
            background-color:FFF;
        }
        .h3 {
            text-align:center;
            color:#008800;
        }
     </style>

        <img src='./img/logo.png' class='img-responsive center-block' alt='My website'>
    <h3> Contact Form Submission </h3>
     <div class='box'>
        <div class='content'>
    The following information was submitted: <br/> <br/>

    <strong>Full Name:</strong> $firstname $lastname <br/> <br/>
    <strong>Phone number:</strong> $phone  <br/> <br/>
     </div>
       </div>
STARTMESSAGE;

编辑:

$mail = new PHPMailer;
//Set who the message is to be sent from
$mail->setFrom($from , "Contact Form");
$mail->addAddress('myemail@example.org', "John Doe");
$mail->Subject = "Contact Form submission ".$name;

$mail->IsHTML(true);
$mail->Body = $message;
//$mail->msgHTML($message);
$mailsent = $mail->send();
if (!$mailsent)
    {
            echo "Mailer Error: " . $mail->ErrorInfo;
            error_log("Mail not sent for $firstname $lastname $mail->ErrorInfo 'n", 1, "myemail@example.org");
            error_log("Message text is  $message 'n", 1, "myemail@example.org");
            error_log("Value of variables from POST method $post_variables 'n ", 1, "myemail@example.org");
            error_log("'n Value of variables from POST method $post_variables 'n", 3, "logfile.txt");
            $logfile = fopen("errorlog.log","w+") or die("cannot open log file");
            fwrite($logfile,"Mail not sent for $firstname $lastname");
            $backtrace = debug_print_backtrace();
            fwrite($logfile,"Back trace is 'n");
            fwrite($logfile,$backtrace);
    }
    else
    {
            echo "Message sent!";
            error_log("'n Mail sent for $firstname $lastname at $date 'n", 3, "logfile.txt");
            error_log("'n Value of variables from POST method $post_variables 'n", 3, "logfile.txt");                               
            header('Location: http://www.example.org/index.php?success=yes');               
    }

我不得不使用:

$mail->AddEmbeddedImage("img/logo.png", "logo", "img/logo.png");
$mail->Body = 'My Logo: <img alt="Logo" src="cid:logo"> 

除了使用内联CSS之外,还可以嵌入图像。

然而,我在发送邮件服务器上有附件大小限制,这阻止了电子邮件的发送。上周的情况并非如此,所以我之前并不知道。有人更改了它,却忘记告诉使用它的人。这阻碍了电子邮件的发送。如果有人使用PHPMailer发送HTML电子邮件,请确保您使用的是

$mail->IsHTML(true);
$mail->Body = $message;

其中$message包含您的邮件内容。此外,正如Fred所说,大多数电子邮件客户端都会删除脚本,因此需要内联CSS样式。

正如我在评论中所说:

大多数邮件客户端将拒绝/忽略样式表/<style>和JS链接文件。

最好不要使用任何JS,而是使用内联CSS方法进行样式设置。

胡言乱语的在评论中留下了一个例子

<h1 style="color:red;font-size:28px;">M E M O</h1>

正如您在评论中所指出的:

@Fred,谢谢你的回复。发送邮件服务器对附件大小有限制。上周不是这样,所以我不知道。有人更改了它,忘记告诉使用它的人。这导致电子邮件无法发送。再次感谢您的时间和帖子Chris H

@Fred,另外,使用img src作为完整的http调用对我来说不起作用。我不得不使用:$mail->AddEmbeddedImage("img/logo.png","logo","img/logo.png")$mail->Body='我的徽标:–Chris H

这是为未来读者提出的问题。

几天前我遇到了同样的问题。然后我把内联css而不是内部css放进去,效果很好。

如果你也这样做(放入内联css),它就会起作用。