0

Hello everyone!

I've created a mailing list where users can subscribe with a form, and the results are added to a MySQL database. They can also unsubscribe. The subscribe/unsubscribe pages work GREAT, but the page where I send out the email is giving me some trouble. It says that the email was sent, but the users never get it! Any suggestions?

The PHP for the send_email.php page is:

<?php
  $from = 'me@example.com';
  $subject = $_POST['subject'];
  $text = $_POST['body'];
  
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

  $dbc = mysqli_connect('db.server.com', 'my_username', 'my_password', 'my_database')
    or die('Error: Could not connect to database');

  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query)
    or die('Error: Could not connect to database');

  while ($row = mysqli_fetch_array($result)){
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $to = $row['email'];
    $msg = "Hello $first_name!<br /><br />$text<br /><br />This message was sent to $first_name $last_name ($to) because they signed up to my email list<br />Don't want to receive these emails?  Click <a href='http://my-site.com/mailing-list/remove.php?email=$to' title='Unsubscribe'>here</a> to unsubscribe.";
    mail($to, $subject, $msg, $headers, 'From:' . $from);
    echo 'Email sent to: ' . $to . '<br />';
  } 

  mysqli_close($dbc);
?>

Thanks!

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by minitauros
0

Have you tried commenting out the mail function and echo out the emails instead to see if you are actually getting any results from the mysql query? Also echo "Hello World" inside the while loop to see if the code is going in there.

If the query is working ok then try changing the mail function arguments. This is what I use:

$to = $_POST[email];

        $subject = "Website Registration";

        $message = "Welcome to our website ".$name."!\r\nPlease validated your useraccount bla bla.";

        $headers = 'From: noreply@blabla.com' . "\r\n" .

            'Reply-To: noreply@blabla.com' . "\r\n" .

            'X-Mailer: PHP/' . phpversion();

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

Have you tried commenting out the mail function and echo out the emails instead to see if you are actually getting any results from the mysql query? Also echo "Hello World" inside the while loop to see if the code is going in there.

If the query is working ok then try changing the mail function arguments. This is what I use:

$to = $_POST[email];

        $subject = "Website Registration";

        $message = "Welcome to our website ".$name."!\r\nPlease validated your useraccount bla bla.";

        $headers = 'From: noreply@blabla.com' . "\r\n" .

            'Reply-To: noreply@blabla.com' . "\r\n" .

            'X-Mailer: PHP/' . phpversion();

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

Yes, I tried that. It displayed the email as it would be sent to each person

(e.g. Hello Person1! MESSAGE

Hello Person 2! MESSAGE

and so forth.)

Also, your code that you provided does not connect to a MySQL database for emails.

Edited by calebcook: n/a

0

I've found that the problem lies on this line:

mail($to, $subject, $msg, $headers, 'From:' . $from);

I tried

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

and that worked, so for some reason

$headers, 'From:' . $from

is creating the problem.

0

If you put a "From:...." in your $headers, there's no need to add it again in your additional parameters :).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.