【问题标题】:Adding html properties in a php在 php 中添加 html 属性
【发布时间】:2015-04-06 17:00:37
【问题描述】:

简而言之,一旦用户使用 php 提交了联系表单,我将尝试向他们写一条消息。问题是我试图通过添加像粗体这样的风格元素来设置消息的样式,并用<br> 隔开某些行。它似乎确实适用。

下面是代码。

$message = "Welcome". $full_name . "to Company!" . "<br>". "<br>". "<b>". " Company is 
    the mailbox for the 21st century that evolves per your need. " . "</b>". "<br>". "<br>". "
    Letting you interact with you mail by viewing and deciding what to do with it as it is received at a 
    Company location throughout North America. ". "<b>". "Please use the below information to send a mail/package to your selected address:". "</b>". "<br>". $full_name. "<br>". $address. "<br>". "To login to your dashboard, please visit the  following link"."<br>". "<a href=''http://company.com/userinterfaceSelect.php'>Your Dashboard</a>" ;

其中有&lt;br&gt;, &lt;b&gt;, 和一个链接,似乎没有一个链接。

更新;

<?php 
if(isset($_POST['submit'])){
    $to = $_POST['email'];// this is your Email address
    $from = "info@company.com"; // this is the sender's Email address
    $full_name = $_POST['fullName'];
        $address = $_POST['address'];

    $subject = "Welcome to company";
    $subject2 = "Copy: New Registration";
    $message2 = "New User". "<br>" .$full_name  . "\n\n" ;
    $message = "Welcome". $full_name . "to company!" . "<br>". "<br>". "<b>". " company is 
    the mailbox for the 21st century that evolves per your need. " . "</b>". "<br>". "<br>". "
    Letting you interact with you mail by viewing and deciding what to do with it as it is received at a 
    Company location throughout North America. ". "<b>". "Please use the below information to send a mail/package to your selected address:". "</b>". "<br>". $full_name. "<br>". $address. "<br>". "To login to your dashboard, please visit the  following link"."<br>". "<a href=''http://company/userinterfaceSelect.php'>Your Dashboard</a>" ;

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
 echo "Your mail has been sent successfuly ! Thank you for your feedback";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

【问题讨论】:

  • 输出如何?在给用户的电子邮件中?在页面上?定义“似乎适用”。等等。
  • in a php... stylistic element ... It does seem to apply.... ( ͡° ͜ʖ ͡°)
  • 在电子邮件中输出给用户
  • 如果您将消息发送到浏览器但它只是文本,则 HTML 格式将不会按预期显示。您需要确保内容以 HTML 格式发送。但是,这是一个不同的问题。
  • 我已经更新了代码。如果你们能在我努力学习的过程中更加慷慨,我也将不胜感激

标签: php html css


【解决方案1】:

你已经很接近让它工作了。

你需要添加一个header来表明内容是HTML:

Content-Type: text/html; charset=ISO-8859-1

例如,您的标题可能如下构成:

$headers = "From: you@yoursite.com\r\n";
$headers .= "Reply-To: me@example.com\r\n"; 
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

请参阅下面的参考资料中的详细信息。

参考资料:

http://php.net/manual/en/function.mail.php

http://css-tricks.com/sending-nice-html-email-with-php/

【讨论】:

  • 这个答案是正确的,但我的建议是将 HTML 放在外部文件中,将变量以特定格式放入其中(例如 ##fullname##)。然后当您需要发送邮件时,将文件加载到字符串中并用值替换变量。我认为它更干净,这使您可以将 HTML 模板集中在某个地方。如果需要,它还可以简化翻译过程。
  • 我同意,OP 应该使用模板方法,甚至是某种类型的 PEAR 包。正如他所说,他正在开始。
  • @Jon220 通过将内容类型设置为 HTML/text,接收电子邮件的电子邮件客户端会将内容解释为 HTML,并根据您嵌入在内容中的任何样式(例如内联 CSS)呈现它.如何为您的消息设置样式是一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 2016-09-02
  • 2018-01-22
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多