Php Mail Script

Associate
Joined
13 Nov 2003
Posts
1,567
Location
Manchester
Hi All

I am working on a php based mail out script.

Im having all sorts of problems lol, and wondered if you guys could steer me on the right path

Here is the code
Code:
<?php require_once('../Connections/essential.php');

mysql_select_db($database_essential, $essential);
$eventsquery = "SELECT email FROM newsletter_users";
$result = mysql_query($eventsquery);
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++){

$arr[$i] = mysql_fetch_array($result);


@extract($_POST);
$subject = stripslashes($subject);
$fromemail = stripslashes($fromemail);
$content = ($content);
foreach ($arr[$i] as $email) {
$mailto = $email;
$mailsubject = 'Feedback from Essential Manchester.com';
$mailheaders .= "From: $name <$email>\r\n";
$mailheaders .= "Reply-To: $email\r\n";
$mailheaders .= "Return-Path: $email\n";
$message .= " 
$content //n
//n
To unsubscribe from the Essential Newsletter please click 
";
mail($mailto,$mailsubject,$message,$mailheaders);

}




 } ?>

The first problem is that it seems to be sending each name in the database mutliple emails, it should only send the email to each person once.
Secondly, the mail headers are being included in the email when I send it to one of my test accounts.

Also, I need to be able to send the emails as html, how can I do that with this script

Thanks Guys
Aaron
 
Holy vulnerable to spam abuse batman!

Don't use extract($_POST) !!111oneone

Use each indice explicitly and sanitise them of carriage return and line feeds.e.g.
Code:
$to = str_replace(array("\n", "\r"), "", $_POST['to']);

--

The reason everyone will receive multiple mails is because you are appending the headers, never re-defining them, thus the nth mail will contain nth x header info. (e.g. 2nd mail will contain 2x header info.)
 
I cant get it to work lol. If anyone could rewrite that little block of code to do what I need then it would hugely appreciated and I would owe you one lol

Aaron
 
Start by changing

Code:
$mailheaders .= "From: $name <$email>\r\n";
$mailheaders .= "Reply-To: $email\r\n";
$mailheaders .= "Return-Path: $email\n";

to

Code:
$mailheaders = "From: $name <$email>\r\n";
$mailheaders .= "Reply-To: $email\r\n";
$mailheaders .= "Return-Path: $email\n";
 
Getting there lol

Still getting duplicate messages, and duplicate content in the second message it sends to each person.

Also, why would the headers be appearing in the body of the email on one email account?

Thanks Guys
Aaron
 
You're not setting a $name anywhere and I have no idea what the user is supposed to click to un-subscribe, but they're the least of your problems.

I tried to tidy it a little bit:

PHP:
<?php

function mail_sanitise( $string ) {
	return str_replace(array("\n", "\r"), '', $string);
}

require_once '../Connections/essential.php';

mysql_select_db($database_essential, $essential);

$subject = mail_sanitise($_POST['subject']);
$fromemail = mail_sanitise($_POST['fromemail']);

$subject = 'Feedback from Essential Manchester.com';

$content = $_POST['content'] . "\nTo unsubscribe from the Essential Newsletter please click";

$result = mysql_query('SELECT email FROM newsletter_users');

while ( $row = mysql_fetch_assoc($result) ) {
	$headers = "From: $name <{$row['email']}>\r\n";
	$headers .= "Reply-To: {$row['email']}\r\n";
	$headers .= "Return-Path: {$row['email']}\n";
	mail($row['email'], $subject, $message, $headers);
}

?>

Please tell me you're doing some kind of user authentication higher up!

Also, how many email addresses are you planning on using this one? It would be much, much better to stagger things if you're going to be sending a lot of mails.
 
Hi Rob

I will be adding a link to an unsubdcribe page when its finished that removes them from the DB

The page is authenticated higher up and its also in a protected dir.

I think it will need staggering as the list contains 6800 unique emails at the moment.

I was going to use a ready made script, but none that I tried had the option to send email, and I also need to be able to send a test message to myself first.

Thanks for the help guys, lifesavers as ever lol
Aaron
 
Hey Rob

That script is almost perfect, thanks. The only problem I am having is that the headers are being sent out more than once to some users, so they are appearing in the body of the email.

Any ideas?
Thanks
Aaron
 
Back
Top Bottom