使用PHP heredoc功能

时间:2016-05-16 23:30:55

标签: php html email

当用户填写联系表单时,我希望用户在邮件开头处获取带有图像的HTML邮件,并为包含用户姓名和电话的阴影的框提供一些样式 号码显示。

我尝试使用$ mail-> IsHTML(true)和$ mail-> msgHTML($ message)使用PHPmailer,但邮件未发送。

信息如下:

$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');               
    }

3 个答案:

答案 0 :(得分:1)

正如我在评论中所述:

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

最好不要使用任何JS并使用内联CSS方法进行样式化。

gibberish

的评论留下了一个例子
<style>

正如你在评论中所指出的那样:

  

@Fred,感谢您回复。发送邮件服务器对附件大小有限制。上周情况并非如此,所以我不知道。有人改变了它,忘了告诉那些使用它的人。这导致电子邮件无法发送。再次感谢你所有的时间和帖子。 - 克里斯H

  

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

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

答案 1 :(得分:1)

我不得不使用:

dict
除了使用内联CSS之外,

嵌入图像。

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

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

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

答案 2 :(得分:0)

我在几天之前遇到了同样的问题。然后我把内联css 而不是内部css,它工作正常。

你做同样的事情( put inline css ),它会起作用。