-1

I would like to send an email using php and I have used this code:

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

What I do is to save this file as a example.php file and upload it to a directory in my ftp. Then I use a browser to go to site.com/directory/example.php and I think the code runs but it can not send the emails.

Should I configure anything else in the cpanel or the server to use this code?

Blue
  • 22,608
  • 7
  • 62
  • 92
Amir
  • 1
  • 4
  • I would remove `$headers` altogether if you're not going to be altering the `From:`, `Cc:`, or `Bcc` fields. Change the `$to` stuff at the top to: `$to = 'x@example.com, y@example.com';` (Will make it a bit easier to read, and fill in your info. Last line would be `mail($to, $subject, $message);` if you take out the headers, and try and goto that script. If it sends an email to yourself, you know it works. – Blue Jun 07 '15 at 09:40
  • How about this two line script! Even this does not work? Are you sure I should not do anything else in cpanel? @FrankerZ – Amir Jun 07 '15 at 09:48
  • What is happening when you goto the page. Try a simple `echo 'Email sent';` the line after `mail`. Are you seeing that? – Blue Jun 07 '15 at 10:06
  • Thanks I added an echo line to the script and uploaded it to another host then It was working! – Amir Jun 07 '15 at 11:37

1 Answers1

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

?>

Try this out. If you need the Cc:, or Bcc include them after in headers variable.

DpEN
  • 4,216
  • 3
  • 17
  • 26
  • Thanks I found out that my host was not able to perform the task! I changed my host and the problem solved! Thanks again – Amir Jun 07 '15 at 11:35