【发布时间】:2016-04-29 10:13:42
【问题描述】:
我正在处理一个 php 项目,并且我到了应该向用户发送邮件的部分。我决定使用 PHP 函数mail()。每当我尝试下面的代码时,它都不成功:
// Sending the confirmation mail
$confirmation_link = "http://localhost/africafashion_newversion/confirm.php?id=$user_id&token=$token";
$to = $_POST['email'];
$subject = "Confirmation de votre compte";
$message = '
<html>
<head>
<title>'.$subject.'</title>
</head>
<body>
<div>
<table cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin:0;padding:0;font-family:Arial,Helvetica,sans-serif;width:100%!important;height:100%!important">
<tbody>
<tr>
<td align="center" style="padding-bottom:20px;border-collapse:collapse">
<img src="logo.png" alt="Stack Exchange" style="width:180px;min-height:43px; padding:30px 0; text-align: center;outline:none;" />
<div style="max-width:400px; margin: 0 auto; border:1px solid #eee;border-radius:3px;background:#ffffff; padding: 25px 10px;">
<p>You are almost done!</p>
<p><a href="'.$confirmation_link.'" style="color:#15c; text-decoration: none;">Click here to complete your registration</a></p>
</div>
<div style="font-size:95%; color:#999;font-family: Helvetica,Arial,sans-serif">
<p>
Questions? Comments? Let us know on our <a href="#" style="color:#15c; text-decoration: none;">feedback site</a>.
</p>
<p>
Stack Exchange Inc. 110 William Street, 28th floor, NY NY 10038
</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <no-reply@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
if (mail($to,$subject,$message,$headers)) {
$_SESSION['flash']['success'] = 'A mail has been successfully sent.';
header('Location: login.php');
exit();
}else{
$_SESSION['flash']['danger'] = "Confirmation mail not sent";
$req = $pdo->prepare('DELETE FROM users WHERE UserID = ?');
$req->execute([$user_id]);
header('Location: register.php');
exit();
}
}
请帮我解决这个问题。
【问题讨论】:
-
您是否进行了任何配置设置以启用发送邮件? (因为这在 Windows 机器上不是给定的。)用户 cmets 有关于如何实现这一点的提示,f.e. php.net/manual/en/function.mail.php#118210
-
我在 8 小时前收到了邮件,在实施 HTML 格式邮件后,我收到了该错误。不要认为错误来自
sendmail
标签: php email html-email sendmail