Page 1 of 1

Mail database, is my code correct??

Posted: Mon Nov 25, 2002 1:30 pm
by darthmahon
Hi,

Ok what I am trying to do is email everyone who has registered so far on the site. There are about 10000 records, but only about 30 have registered yet.

Below is my code:

Code: Select all

<?php

$landps     = array("mydb", "localhost", "myuser", "mypass");
$database   = $landps&#1111;0];
$host       = $landps&#1111;1];
$username   = $landps&#1111;2];
$password   = $landps&#1111;3];
$table        = 'users';
$Subject    = trim ($Subject);
$Message    = trim ($Message);
$URL        = 'http://www.mysite.com/admin/';

mysql_connect ($host,$username,$password) or die ("Failed login.");
mysql_select_db ($database) or die ("Failed to access db.");

$Message = nl2br($Message);

$Message = preg_replace("/((http(s?)://)|(<a href="http://www." target="_blank">www.</a>))(&#1111;S.]+)b/i",
"<a href="http$3://$4$5"
target="_blank">$2$4$5</a>", $Message);

$Message = preg_replace("/(&#1111;w.]+)(@)(&#1111;S.]+)b/i",
"<a href="mailto:$0">$0</a>",
$Message);

$From = 'Chris <chris@yduk.net>';

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $From\r\n";

$result = mysql_query("SELECT email FROM users WHERE registered = 1"); 
while ($row = mysql_fetch_array($result)) 
&#123; 
$recipient = $row&#1111;'email'];
mail ($recipient, $Subject, $Message, $headers) or die ("mail failure.");
&#125;

header("Location: $URL");

?>
Now the problem is it seems to do something for a few seconds, but then it says "mail failure"... I know it sends some emails, but not all of them.

Is it timing out or what? I don't understand why it won't work, the code looks fine to me.

Any of you pros got an idea?

Cheers,
Chris

Posted: Mon Nov 25, 2002 2:25 pm
by BigE
Your code looks fine to me. One thing you might want to try is turning track_erros on at the beginning of your script, and then using $php_errmsg to see what error mail() is returning.

Code: Select all

&lt;?php
ini_set('track_errors', 1);
?&gt;
Also, rtrim() the e-mail addresses just to make sure. The manual notes of \n causing a possible error if in the 'to' or 'subject' field. Hope that helps.