【问题标题】:How can I add HTML, CSS, and PHP to emails using PHPMailer?如何使用 PHPMailer 将 HTML、CSS 和 PHP 添加到电子邮件中?
【发布时间】:2017-02-24 22:38:31
【问题描述】:

我可以发送电子邮件,但他们似乎没有收到任何 CSS,即使我已将其添加到我在发送邮件时调用的 .php 文件中。

我设置了$mail->isHTML(true); 并且HTML 可以正常工作,只是CSS 和PHP 都没有使用变量。

这就是它的外观(atm):http://imgur.com/a/SXUqs

这是我要发送的.php 文件:

<!DOCTYPE html>
<html>
<head>
</head>

<body>
<style>
/*custom font*/
@import url('http://fonts.googleapis.com/css?family=Montserrat');

/* Basic Styles */
@import 'https://fonts.googleapis.com/css?family=Roboto:300,400,500';
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,300');


* { margin:0; padding:0; }
html { overflow:auto; height:100%; }
body { background-color: #e9ebee; margin:0; padding:0; font-size:10px; cursor:default; height:100%; }
body, html {font:13px 'open sans',sans-serif;  overflow-x:hidden;}

/* Base Styles
********************************************************************* */
html {
  font-size: 62.5%;
  width: 100%;
  height: 100%;
}
body {
  background: #e9ebee;
  height: 100%;
  font-size: 1.5em;
  line-height: 1.6;
  font-family: 'Open Sans', Helvetica, Arial, sans-serif;
  color: #222;

}

/*form styles*/
#msform {
  min-width: 60%;
  margin: 50px auto;
  text-align: center;
  position: relative;
}
#msform fieldset {
  background: white;
  border: 0 none;
  border-radius: 3px;
  box-shadow: 0 0 15px 1px rgba(0, 0, 0, 0.4);
  padding: 20px 30px;
  box-sizing: border-box;
  margin: 0 10%;

  /*stacking fieldsets above each other*/
  position: relative;
}
/*Hide all except first fieldset*/
#msform fieldset:not(:first-of-type) {
  display: none;
}
/*inputs*/
#msform input, #msform textarea {
  padding: 15px;
  border: 1px solid #ccc;
  border-radius: 3px;
  margin-bottom: 10px;
  width: 100%;
  box-sizing: border-box;
  font-family: montserrat;
  color: #2C3E50;
  font-size: 13px;
  outline: none;
}

#msform input:focus, #msform textarea:focus {
  border:1px solid #2196F3;
}

/*buttons*/
#msform .action-button {
  max-width: 40%;
  width:100px;
  background: #2196F3;
  font-weight: bold;
  color: white;
  border: 0 none;
  border-radius: 1px;
  cursor: pointer;
  padding: 10px 5px;
  margin: 10px 5px;
}
#msform .action-button:hover, #msform .action-button:focus {
  box-shadow: 0 0 0 2px white, 0 0 0 3px #2196F3;
}
/*headings*/
.fs-title {
  font-size: 16px;
  text-transform: uppercase;
  color: #2196F3;
  margin-bottom: 10px;
  border-bottom:1px solid #475f93;
}
.fs-subtitle {
  font-weight: normal;
  font-size: 13px;
  color: #2196F3;
  margin-bottom: 20px;
}
/*progressbar*/
#progressbar {
  margin-bottom: 30px;
  overflow: hidden;
  /*CSS counters to number the steps*/
  counter-reset: step;
}
#progressbar li {
  list-style-type: none;
  color: #666;
  text-transform: uppercase;
  font-size: 9px;
  width: 20%;
  float: left;
  position: relative;
}
#progressbar li:before {
  content: counter(step);
  counter-increment: step;
  width: 20px;
  line-height: 20px;
  display: block;
  font-size: 10px;
  color: #333;
  background: white;
  border-radius: 3px;
  margin: 0 auto 5px auto;
}
/*progressbar connectors*/
#progressbar li:after {
  content: '';
  width: 100%;
  height: 2px;
  background: white;
  position: absolute;
  left: -50%;
  top: 9px;
  z-index: -1; /*put it behind the numbers*/
}
#progressbar li:first-child:after {
  /*connector not needed before the first step*/
  content: none; 
}
/*marking active/completed steps green*/
/*The number of the step and the connector before it = green*/
#progressbar li.active:before,  #progressbar li.active:after{
  background: #2196F3;
  color: white;
}
</style>  

    <!-- multistep form -->
    <form id='msform' action=' method='post'>
        <!-- fieldsets -->
        <fieldset>
          <h2 class='fs-title'>Verify your account</h2>
          <p>Hello <?php echo $_POST['username']; ?>.<br/>
          You have recently signed up to our website at <?php echo $url; ?>, and
          to be able to sign in you will have to verify your account by using the link bellow.<br />
          <br />
          <?php echo "<a href='".$url."/verify/".$verify_link."'>here</a>"; ?>
          </p>
        </fieldset>
    </form>

</body>
</html>

这是我用来发送邮件的 PHPMailer 文件:

<?php
include $_SERVER["DOCUMENT_ROOT"] . '/assets/head.php';
$body = file_get_contents($_SERVER["DOCUMENT_ROOT"] . '../assets/php/mail/mail-body.php');
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require $_SERVER["DOCUMENT_ROOT"] . '/assets/php/phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom($domainemail, $url);
//Set an alternative reply-to address
$mail->addReplyTo($domainemail, $url);
//Set who the message is to be sent to
$mail->addAddress("toemail", "toname");
//Set the subject line
$mail->Subject = 'Verify your account';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents(''), dirname(__FILE__));
//main body of the email
$mail->Body = $body;
//Replace the plain text body with one created manually
$mail->AltBody = 'If you can&apos;t view this email, please switch to a browser that support html emails';
//Attach an image file
//$mail->AddAttachment($body);
//allow html output
$mail->isHTML(true); 


//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

我能够获得正确的$_POST[''],但链接将显示linkname"; &gt;,而实际上并不是链接。

【问题讨论】:

    标签: php html css phpmailer


    【解决方案1】:

    您的代码不起作用,因为

    $body = file_get_contents($_SERVER["DOCUMENT_ROOT"] . '../assets/php/mail/mail-body.php');
    

    不执行php。

    尝试将其替换为:

    ob_start();
    require( $_SERVER["DOCUMENT_ROOT"] . '../assets/php/mail/mail-body.php');
    $body = ob_get_contents();
    ob_end_clean();
    

    【讨论】:

    • 虽然这解决了我的 php 链接的问题,但不幸的是它并没有解决 css 问题:/ 但至少解决了一个问题,谢谢。
    • 不要在电子邮件中使用 css 规则,因为并非所有电子邮件客户端都支持它们。改用内联样式。
    • CSS 导入不起作用。 Gmail 上周才开始支持样式标签中的 css,尽管您的代码限制为 8k。
    • @Synchro 但可以说我正在向 Outlook 发送邮件,那么邮件应该正确显示吗?无论如何,我让内联 css 工作:)
    • Outlook 在 HTML 和 CSS 方面非常糟糕。许多事情不能正常工作 - 例如,您不能在任何东西上设置图像背景。这主要是因为它使用了 Word 的渲染引擎。 Gmail 也很糟糕,但他们最近的变化意味着它不像以前那么糟糕了。
    猜你喜欢
    • 2016-06-10
    • 2011-05-21
    • 2013-10-31
    • 2011-10-02
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多