[英]send html in an email via php, can't seem to do it
我有一个要向其发送HTML类型的电子邮件(基本上是一封新闻信)的订阅者列表。 但是目前,它只是发送清楚,我似乎无法弄清楚如何使其看起来更好。
我搜索了其他问题,但似乎无法将答案应用于自己的代码,所以有人可以帮忙吗? 代码如下所示:
<?php
$user = "example";
$password = "example";
$host = "example";
$dbase = "example";
$table = "example";
$from= 'example';//specify here the address that you want email to be sent from
$subject= $_POST['subject'];
$body= $_POST['body'];
// Connection to DBase
$dbc= mysqli_connect($host,$user,$password, $dbase)
or die("Unable to select database");
$query= "SELECT * FROM $table";
$result= mysqli_query ($dbc, $query)
or die ('Error querying database.');
while ($row = mysqli_fetch_array($result)) {
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email= $row['email'];
$msg= "Dear $firstname $lastname,\n$body";
mail($email, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}
mysqli_close($dbc);
?>
因此,我有一个消息框,可以在其中键入消息,但是如何使该消息框为html样式? 那么我可以添加H2等标签吗? 或者只是使电子邮件像html新闻一样
为此,您需要将标题设置为text/html
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
并将其传递给您的mail()
函数。
像这样
mail($to, $subject, $msg, $headers);
$msg= "<h3>Dear $firstname $lastname</h3>,<br><br><b>$body</b>"; //<-- Added some basic formatting
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Yourname <'.$from.'>' . "\r\n";
mail($to, $subject, $msg, $headers);
参考 : PHP Manual
您需要发送标有html的标头:example
$to = 'person@example.com';
$subject = 'Your subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: you@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = // your html code here
mail($to, $subject, $message, $headers);
使用PHP Mailer类而不是mail()
$mail = new PHPMailer();// defaults to using php "mail()"
$mail->SetFrom("frommail");
$mailAdmin->AddAddress("tomail");
$mailAdmin->Subject = $subject;
$mailAdmin->MsgHTML($message);
$mailAdmin->IsHTML(true);
$mailAdmin->Send();
您只需要包含一个类文件phpmailer,即可在线下载类文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.