Forum Moderators: coopster

Message Too Old, No Replies

PHP Mail from mysql

Using a mysql database to get emails to send

         

leescotland

5:12 pm on Jan 3, 2012 (gmt 0)

10+ Year Member



Have put together a small script to open all rows on a mysql database where the date = todays date.

In the email it will show a long email but for testing i have shortened it to show 'email from this row'.

I need the script to get the email from the row in question but everytime it errors out.

Any thoughts ?


<?php
// Make a MySQL Connection
mysql_connect("localhost", "web168-customer", "1rangers1") or die(mysql_error());
mysql_select_db("web168-customer") or die(mysql_error());

//Get Date
$eltiempo=time();
$lafecha=date("Y-m-d",$eltiempo);

// Get all the data from the "domainrenewal" table
$result = mysql_query("SELECT * FROM domainrenewal WHERE warningdate='$lafecha'")
or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Email


$to = $row['email'];

// Send
mail($to, 'My Subject', $row['email']);

}

?>

leescotland

6:02 pm on Jan 3, 2012 (gmt 0)

10+ Year Member



Not sure why I can get it to send the "$row['email']" as the message but not as the "To".

This comes out with a Server Error

rocknbil

5:07 pm on Jan 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First verify all the values are being populated properly. in your while loop, comment out the mail and do

while($row = mysql_fetch_array( $result )) {
$to = $row['email'];
echo "Sends to: $to <br>\n";

//mail($to, 'My Subject', $row['email']);
}

The syntax is

mail([TO],[SUBJECT],[MESSAGE],[HEADERS]);

There is a fifth parameter but it's seldom needed, specific to the mail program used on the server.You should try some of the examples in the manual [php.net]. The mail header "from" is often required by many mail servers to send a mail and it goes in the headers parameter, which may be why you're getting a 500.


Also error trapping is critical to debugging. Once you've verified the proper values are being trapped,

if (! mail($to, $subject, $message,$headers)) {
echo "<p>Could not send mail to $to</p>";
exit;
}

Last, from the manual (since you're stepping through a database,)

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

1888software

8:06 pm on Jan 4, 2012 (gmt 0)

10+ Year Member



Note that some host server configurations REQUIRE that the sending email address be of the same DOMAIN or ACCOUNT that is used to send the email:

example
server mail() is sent on: 123domain.com
your sending address: sender@123domain.com - passes
your sending address: webmaster@someotherdomain.com - could fail

rocknbil is correct when saying:

if (! mail($to, $subject, $message,$headers)) {
echo "<p>Could not send mail to $to</p>";
exit;
}

I would leave mail() out of the equation entirely and deal with the actual error. The php mail() function is extremely reliable and works in 'almost' all cases.

Also, depending on the size of your dataset, it is inefficient to make variable assignments inside your data retrieval loop when you can easily move them above your call to the db:

//Get Date
$eltiempo=time();
$lafecha=date("Y-m-d",$eltiempo);

leescotland

9:10 pm on Jan 5, 2012 (gmt 0)

10+ Year Member



The script is only going to send a maximum of 5 emails per day so it does not matter how efficient it is.

I have now set it send from my domain to make sure this was not the issue.

If i use the mail code :
mail('some@domain.com, 'My Subject', $row['email']);

This works and on each email it has a message of the email on that row. It is only when I try to have that rows email address as the *To* that I come accross errors

rocknbil

5:32 pm on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mail header "from" is often required by many mail servers


Compose a $header variable and make the from in it something like

no-reply@yourdomain.com

combining all the notes above.