【问题标题】:How to send an email using php step by step? [duplicate]如何逐步使用php发送电子邮件? [复制]
【发布时间】: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


【解决方案1】:
<?php
$to = "abc@example.com";
$subject = "This is subject of mail";

    $message = "
    <html>
        <head>
            <title>".$subject."</title>
        </head>
    <body>
        <p>This here is the description of mail</p>
    </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: thesender@example.com' . "\r\n";

    mail($to,$subject,$message,$headers);

    echo "<script> alert('Form Succesfully Submitted'); window.location.href='contact.php'; window.location.href='contact.php'; </script>";

?>

试试这个。 如果您需要抄送:或密件抄送,请将它们包含在 headers 变量之后。

【讨论】:

  • 谢谢 我发现我的主机无法执行任务!我换了主机,问题就解决了!再次感谢
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多