Jump to content

Sending e-mail from PHP using an e-mail list from a MySQL table


Freedom-n-Democrazy

Recommended Posts

I'm setting up a newsletter thing for my website.

 

I have a newsletter table in MySQL:

+----------------+------+--------+

| email          | mens | womens |

+----------------+------+--------+

| test2@test.com |    1 |      1 |

| test1@test.com |    1 |      0 |

+----------------+------+--------+

 

I am using a HTML form and this PHP code I learn't from the manual, which sends out e-mail's.

PHP:

if ($_POST['newsletter'] == 'Mens') {

$to = '';

$subject = $_POST['subject'];

$body = $_POST['body'];

$header = 'From: Me Someone <me@someone.com>';

mail($to, $subject, $body, $header);

}

 

What I want to do with the above code is send out an e-mail to all the e-mails in my MySQL database that are tagged '1' under mens.

 

How would I go about doing this? I'm guessing I will have to use a MySQL query in the $to = ''; that goes something like this:

$to = '$query (select from `newsletters` where `email` = 1');

?

Link to comment
Share on other sites

You have to actually execute the query.

$sql = "SELECT `email` FROM `newsletters` WHERE `mens` = 1";
$sql_query = mysql_query($sql);

 

Then its just a matter of iterating over the results and sending out the emails.. Or join the results together and send 1 email to all recipients.

Link to comment
Share on other sites

Your main problem was you weren't calling the mysql_query function, and you needed quotes around your sql string. You also would have gotten a resource id error because you needed to deal with the result accordingly. Try this

 

$sql = 'select email from newsletters where mens = 1';
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
   $to[] = $row['email'];
}
$tostring = implode(';', $to); // had a comma in here should be semi-colon to separate email addresses

 

*Edit*

 

You have to actually execute the query.

$sql = "SELECT `email` FROM `newsletters` WHERE `mens` = 1";
$sql_query = mysql_query($sql);

 

Then its just a matter of iterating over the results and sending out the emails.. Or join the results together and send 1 email to all recipients.

 

Beat me to it. My example joins them together

Link to comment
Share on other sites

Cool guys, thanks.

I'm not using the following code, but its still not working.

 

<?php

$link = mysql_connect('localhost', 'testusr', 'testpw');

mysql_select_db('testdb', $link);

if ($_POST['newsletter'] == 'Mens') {

	$query = "select email from newsletters where mens = 1";
	$result = mysql_query($query);
	while ($row = mysql_fetch_assoc($result)) {
		$to[] = $row['email'];
	}
	$tostring = implode(';', $to);
	$subject = $_POST['subject'];

	$body = $_POST['body'];

	$header = 'From: Me Someone <me@someone.com>';

	mail($to, $subject, $body, $header);

}

echo 'Done.';

mysql_query ($query);

mysql_close($link);

?>

Link to comment
Share on other sites

You are or you arent using that code? Im a little confused.. If you ARE using that code.. you are giving the "to" parameter of the mail function an array..

 

You either need to use the $tostring as defined earlier or change this line

mail($to, $subject, $body, $header);

to

mail(join(',',$to), $subject, $body, $header);

 

DevilsAdvocate: you were right the first time.. Its a comma to separate emails in the mail() function

Link to comment
Share on other sites

Sorry for the slow response, my net is capped at 72kbps until the end of the months, and thats kiloBITS!!!  :'(

 

The code worked fine, thanks guys the help was really appreciated! The only problem I have is that when the mail is received, I can see all the e-mail addresses it was sent to. Is it possible to stop this? I'm guessing with some sort of loop or something. To be honest that is beyond me and just an educated guess, but I am all ears on advice how to go about achieving it. :)

Link to comment
Share on other sites

Wrap a foreach loop around the mail function.

foreach ($to as $email) { 
   mail($email, $subject, $body, $header);
}

 

Or you could do it straight out of the database where the $to array is generated.. Either way.

 

// Version 1 //
while ($row = mysql_fetch_assoc($result)) {
$to[] = $row['email'];
}
$subject = $_POST['subject'];
$body = $_POST['body'];
$header = 'From: Me Someone <me@someone.com>';
foreach ($to as $mail) {
mail($mail, $subject, $body, $header);
}

// Version 2 //
$subject = $_POST['subject'];
$body = $_POST['body'];
$header = 'From: Me Someone <me@someone.com>';
while ($row = mysql_fetch_assoc($result)) {
mail($row['email'], $subject, $body, $header);
}

Link to comment
Share on other sites

Fantastic!

 

Thanks guys so much for your help, especially you Buddski!

 

Wow PHP is such an amazing product. Every day I continue to get blown away how powerful it is!

 

Coming from a bit of a Python background, I use to fear PHP... but now I see it as the most awesome thing to come along since I found Linux!

 

Thanks again guys! I hope you enjoy your days! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.