/    Sign up×
Community /Pin to ProfileBookmark

mailing list mailer

Hi Everyone. I have the bare bones of a php script which mails everyone who signs up for the news letter of a club night (held on a mysql database). The code is below. What I am wondering is, when I call it, it displays the first email address as sent then displays the next ones in blue with the error message. Even though it displays an error it does actually send the email (right now I’m testing with 3 variations of my own address so all emails are received. Can anyone help with getting this code displaying sensible feedback rather than nonsense?

Also, Is 10 seconds a bit too long? With just over a thousand members, it might take a long time (2 hours 48 mins exactly if my math is correct.)
Thanks in advance
Graham

[CODE]<?php
session_start();

if (!session_is_registered(“username”)) {
header( ‘Location: login script address’) ;
} else {
$user=”xxx”;
$host=”localhost”;
$password=”xxx”;
$database=”xxx”;

mysql_connect(“$host”, “$user”, “$password”) or die(‘Unable to connect:’ . mysql_error());
mysql_select_db(“$database”) or die(‘Unable to connect:’ . mysql_error());
//this is a bulk emailer
//use carefully!!!!
$sql = mysql_query(“SELECT * FROM users”);
$num_rows = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql)) {

// take weight off the server CPU by waiting 10 seconds between each mailout.
sleep(10);
$headers = “MIME-Version: 1.0rn”;
$headers .= “Content-type: text/html; charset=iso-8859-1rn”;
$headers .= “From: [email protected]”;
if (!mail($row[“email”],
$subject = “UFREAK E-Flyer”,
$message = ”
<html>
<body>
<center><p><img src=’absolute path of flyer’/></p></center>
<body>
</html>”, $headers)) {
echo “<FONT FACE=VERDANA, ARIAL SIZE=2 Color=Blue>E R R O R <FONT Color=Blue> : Mail has not been sent to ” . $row[“email”] . “</FONT><BR>n”;
} else {
echo “<FONT FACE=VERDANA, ARIAL SIZE=2>” . $row[“email”] . “</FONT><BR>n”;
}

}
}

?>[/CODE]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@DJsACSep 13.2006 — 10 seconds is a bit slow, I'd suggest between 1 and 3 seconds, especially as the mail is small.

3 seconds * 1008 members -> &#8776; 50 minutes

1 second *
1008 members -> &#8776; 16 minutes

The script shouldn't show the blue error unless the mail isn't being sent.

Some snippets from the php manual:Notes

<i> </i>Note: The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

<i> </i>Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.

<i> </i>As such, the to parameter should not be an address in the form of "Something &lt;[email protected]&gt;". The mail command may not parse this properly while talking with the MTA.

<i> </i>Note: Email with attachments and special types of content (e.g. HTML) can be sent using this function. This is accomplished via MIME-encoding - for more information, see this Zend article or the PEAR Mime Classes.

<i> </i>Note: 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.

<i> </i>For the sending of large amounts of email, see the PEAR::Mail, and PEAR::Mail_Queue packages.
?
Copy linkTweet thisAlerts:
@ronverdonkSep 13.2006 — For this type of mail I set the time between 0.5 and 1 second. Never had any complaints from my provider.

Ronald ?
Copy linkTweet thisAlerts:
@DoppleauthorSep 13.2006 — I have set it to 0.5 as this will hugely speed things up.

I can't undesrstand why it would display that the mail isn't being sent when it is. I could live with it but it's not for myself. A client requires it so I'll have to find a way around it. I'll may be try switching them around by saying if(mail rather than if(!mail

I'll let you know how I get on.
Copy linkTweet thisAlerts:
@DoppleauthorSep 13.2006 — I'll may be try switching them around by saying if(mail rather than if(!mail

I'll let you know how I get on.[/QUOTE]


Nah. Didn't work. obviously. That's how desperate I am.
Copy linkTweet thisAlerts:
@ronverdonkSep 13.2006 — Must be in the way you assign data to variables within the mail() function. Why don't you try this one, filling the variables outside the mail() function:
[code=php]
$subject = "UFREAK E-Flyer";
$message = "
<html>
<body>
<center><p><img src='absolute path of flyer'/></p></center>
<body>
</html>";
if (!mail($row["email"], $subject,$message, $headers)) {
[/code]


Ronald ?
Copy linkTweet thisAlerts:
@DoppleauthorSep 13.2006 — Argh. Valiant attempt but still no joy. Could it be that it's displaying the feedback before it's had a chance to send it?

I'm just clutching at straws now.
Copy linkTweet thisAlerts:
@ronverdonkSep 13.2006 — By the way just a small hiccup: your <body> statement before the </html> statement should be </body>.

Ronald ?
Copy linkTweet thisAlerts:
@DoppleauthorSep 14.2006 — Whoops. Thanks for that!

SO this definatly shouldn't be displaying that the email has not been sent if it it has? As far as people can see the code is sound? Could it be a server issue?
Copy linkTweet thisAlerts:
@ronverdonkSep 15.2006 — I ran the following code and it sent all emails correctly.
[code=php]
<?php
$user="xxx";
$host="localhost";
$password="yyy";
$database="zzz";
mysql_connect("$host", "$user", "$password") or die('Unable to connect:' . mysql_error());
mysql_select_db("$database") or die('Unable to connect:' . mysql_error());
//this is a bulk emailer
//use carefully!!!!
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: [email protected]";
$subject = "UFREAK E-Flyer";
$message = "<html><body><center><p><img src='absolute path of flyer'/></p></center></body></html>";
$sql = mysql_query("SELECT * FROM users");
$num_rows = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql)) {
// take weight off the server CPU by waiting 10 seconds between each mailout.
sleep(1);
if (!mail($row["email"], $subject,$message, $headers)) {
echo "<FONT FACE=VERDANA, ARIAL SIZE=2 Color=Blue>E R R O R <FONT Color=Blue> : Mail has not been sent to " . $row["email"] . "</FONT><BR>n";
} else {
echo "<FONT FACE=VERDANA, ARIAL SIZE=2>Sent mail to " . $row["email"] . "</FONT><BR>n";
}

}
?>
[/code]


Ronald ?
Copy linkTweet thisAlerts:
@DJsACSep 15.2006 — I ran the following code and it sent all emails correctly.
[code=php]
<?php
$user="XXX";
$host="localhost";
$password="XXX";
$database="XXXX";
mysql_connect("$host", "$user", "$password") or die('Unable to connect:' . mysql_error());
mysql_select_db("$database") or die('Unable to connect:' . mysql_error());
//this is a bulk emailer
//use carefully!!!!
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: [email protected]";
$subject = "UFREAK E-Flyer";
$message = "<html><body><center><p><img src='absolute path of flyer'/></p></center></body></html>";
$sql = mysql_query("SELECT * FROM users");
$num_rows = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql)) {
// take weight off the server CPU by waiting 10 seconds between each mailout.
sleep(1);
if (!mail($row["email"], $subject,$message, $headers)) {
echo "<FONT FACE=VERDANA, ARIAL SIZE=2 Color=Blue>E R R O R <FONT Color=Blue> : Mail has not been sent to " . $row["email"] . "</FONT><BR>n";
} else {
echo "<FONT FACE=VERDANA, ARIAL SIZE=2>Sent mail to " . $row["email"] . "</FONT><BR>n";
}

}
?>
[/code]


Ronald ?[/QUOTE]

Ronald I suggest that you immediately change your MYSQL Username and Password. Posting them here isn't the best thing to do ?
Copy linkTweet thisAlerts:
@ronverdonkSep 15.2006 — Thanks for the warning. I had forgotten to take them out when posting the code. Anyway, this is my 'toy' db on my private laptop.

Ronald ?
Copy linkTweet thisAlerts:
@DoppleauthorSep 15.2006 — Sent mail to [email][email protected][/email]

E R R O R : Mail has not been sent to

Sent mail to [email][email protected][/email]

Sent mail to [email][email protected][/email][/QUOTE]


This is the output I get. I think it must just be an error in the loop as it's just throwing up the "mail has not been sent to" error after saying the first is ok, then the text stays blue and it says sent to....

I'll play around with the loop.

G
Copy linkTweet thisAlerts:
@DoppleauthorSep 15.2006 — Sorry Guys. I am a complete IDIOT. I have 4 users in the users table, and the second doesn't have an email address. That is why iot's displaying like that. Sorry for wasting your time. I'm away to kick myself in the balls now. :eek:
×

Success!

Help @Dopple spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...