如何发送HTML电子邮件(PHP)

时间:2016-01-20 17:10:39

标签: php email

我有一个php博客系统,用户可以在注册后在我们的博客上公开发布,现在我试图在发布新帖子后发布新邮件名称“标题示例”已发布的所有用户发送电子邮件。

 $send = mysqli_query($con, "SELECT * FROM users");
 while($em = mysqli_fetch_array($send)) {

 phpmailer stuff here

 to = ("$em['email']"); here I'm fetching every email of user
  } while loop is closed here

我想知道这是向所有用户发送电子邮件的正确方法吗?这会导致服务器上的任何负载吗?

1 个答案:

答案 0 :(得分:0)

这样的事情会让你开始。

$headers = "";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: your_from_email_address@yourdomain.com\n";
$headers .= "Organization: yourdomain.com\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Mailer: PHP". phpversion() ."\n" . PHP_EOL;

$msg_start = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>';
$subj = 'Subject of email';
$msg = $msg_start . '<div style="color:red">This is teh email message right here</div>';

$all_users = mysqli_query($con, "SELECT * FROM users");
while($user = mysqli_fetch_array($all_users)) {
    $e = $user['email'];
    $result = mail($e, $subj, $msg, $headers);

} 

请注意,对于电子邮件中的HTML,所有样式必须为内嵌。很遗憾,您无法添加<style>div{background:wheat;}</style>等HTML标记。