如何使用php一步一步地发送电子邮件


How to send an email using php step by step?

我想使用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或服务器使用这段代码吗?

<?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>";
?>

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