简体   繁体   English

如何使用 PHPMailer 将 HTML、CSS 和 PHP 添加到电子邮件中?

[英]How can I add HTML, CSS, and PHP to emails using PHPMailer?

I am able to send emails, but they don't seem to be getting any CSS even though I have added it to the .php file I'm calling to when sending mail.我可以发送电子邮件,但他们似乎没有收到任何 CSS,即使我已将其添加到我在发送邮件时调用的.php文件中。

I have set $mail->isHTML(true);我已经设置了$mail->isHTML(true); and HTML works, just not the CSS, nor the PHP using variables.和 HTML 工作,只是不是 CSS,也不是 PHP 使用变量。

This is how it's supposed to look (atm): http://imgur.com/a/SXUqs .这就是它的外观(atm): http : //imgur.com/a/SXUqs

This is the .php file I want to send:这是我要发送的.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>

This is the PHPMailer file I use to send my mail:这是我用来发送邮件的 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!";
}

?>

I am able to get the correct $_POST[''] but links will display linkname"; > without actually being a link.我能够获得正确的$_POST['']但链接将显示linkname"; >实际上不是链接。

Your code doesn't work because您的代码不起作用,因为

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

doesn't execute php.不执行php。

Try to replace it with:尝试将其替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM