/    Sign up×
Community /Pin to ProfileBookmark

Hi,

Still fairly new to PHP, so a lot of my (working) scripts, is a wild combination of googling, this forum, and i guess, luck.

Im working on, for me anyways, quite a big project, where one of the ‘elements’ is people being able to register, and signup for a newsletter.

I’ve got it working, i’m just curious to find out if the way im doing it, is a proper way, and as i’m currently testing with only 3 receipients, if it will work when more/plenty/alot of mails have to be send

i get the email adresses from a mysql database and have mail() function in a while loop, reading out that database:

[code=php]
while($row = mysql_fetch_assoc($result)){;
$mail = $row[’email’];

$onderwerp = $_GET[‘onderwerp’];
$content = $_GET[‘content’];
$content = stripslashes($content);
$to = $mail;
$subject = $onderwerp;

$message = ”
<html>
<head>

</head>
<body>
$content
</body>
</html>
“;

$headers = “MIME-Version: 1.0” . “rn”;
$headers .= “Content-type:text/html;charset=iso-8859-1” . “rn”;
$headers .= ‘From: <[email protected]>’ . “rn”;
mail($to,$subject,$message,$headers);
}
[/code]

As said, this works, but im far from sure this is thé way of doing it??

Also, is it possible to check if the mail function worked? I’m not desperately after knowing if te receipient received the e-mail, but knowing if the mail() function did it’s work would be nice, so i can echo a message, informing the ‘sender’ if it went ok or not.

Any input is highly appreciated!

Thanks,

peace

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@UAL225Aug 17.2009 — the mail function is pretty slow from what I know in large amounts. I know some where in this forum this is discussed, my recommendation is that on your server you should be able to create a automatic email sender that pulls the emails from a database, I forgot what its called but it should be under your email tab in your sever cpanel.
Copy linkTweet thisAlerts:
@MindzaiAug 17.2009 — For a few recipients calling mail in a loop will work, but for much more I tend to use PEAR's Mail_Queue class and run the script via a cron job.

As for checking if the mail function worked, the function returns a boolean value, true on success and false on failure. Of course this is no guarantee that the mail reached the recipient, but it does give you an idea about whether the mail sent successfully or not.

BTW your script is currently open to header injection attacks, you will probably want to fix that. Also there is no need for all the variable re-assignment you are doing, and populating the $content etc variables can be done outside of the loop for greater efficiency.

A more efficient and secure approach (untested):

[code=php]
$subject = preg_replace("([rn])", "", $_GET['onderwerp'];
$message = "<html>n<head>n</head>n<body>n" . stripslashes($_GET['content']) . "n</body>n</html>n";
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
$headers .= 'From: <[email protected]>' . "rn";
$pattern = "/(bcc:|cc:|content-type:|to:|mime-version:|multipart/mixed|content-transfer-encoding:)/i";
if (preg_match($match, $subject) || preg_match($match, $message)) {
// possible header injection
} else {
$results = array();
while($row = mysql_fetch_assoc($result)) {
$results[] = (int) mail($row['email'], $subject, $message, $headers);
}
$result = array_count_values($results);
echo $result[1] . " of " . sizeof($results) . " mails sent successfully";
}[/code]
Copy linkTweet thisAlerts:
@PeaceFrogauthorAug 18.2009 — Hi,

Thanks a lot to both for your input.


I'll look into both suggestions, and will let you know my findings.

Again, thanks!



peace
×

Success!

Help @PeaceFrog 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.19,
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,
)...