【发布时间】:2015-08-21 21:48:40
【问题描述】:
我想使用 php 发送电子邮件,我使用了以下代码:
<?php
// multiple recipients
$to = 'x@example.com' . ', '; // note the comma
$to .= 'y@example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
我所做的是将这个文件保存为 example.php 文件并将其上传到我的 ftp 中的目录。然后我用浏览器去site.com/directory/example.php
我认为代码可以运行,但无法发送电子邮件。
我应该在 cpanel 或服务器中配置其他任何东西来使用此代码吗?
【问题讨论】:
-
如果您不打算更改
From:、Cc:或Bcc字段,我将完全删除$headers。将顶部的$to内容更改为:$to = 'x@example.com, y@example.com';(将使其更易于阅读,并填写您的信息。如果您取出标题,最后一行将是mail($to, $subject, $message);,然后尝试转到该脚本. 如果它给你自己发送一封电子邮件,你就知道它有效。 -
这两行脚本怎么样!即使这样也行不通? 你确定我不应该在 cpanel 中做任何其他事情吗? @弗兰克Z
-
进入页面时发生了什么。尝试一个简单的
echo 'Email sent';mail之后的行。你看到了吗? -
谢谢我在脚本中添加了一条回显线并将其上传到另一个主机然后它正在工作!
标签: php email cpanel phpmailer