Mail error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Mail error

Post by Addos »

Hi,
I’m hoping somebody can help me with the following. I had been successfully using the code below to send mail for a short while and now I’m getting the error message
“Failed to connect to mail.******.com:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]Failed to set sender: info@******.com [SMTP: Failed to write to socket: not connected (code: -1, response: )]”

I have tested this account by adding it to my Outlook Express and it worked perfectly. I only have access to my CP panel to setup mail accounts but would not have access to the server where the mail is running.

I’m getting the impression that my host has changed something but I can’t be sure. Can anyone advise as anything I look at on the web seems to talk about changing stuff on the server which I wouldn’t have access to.

Thanks

Code: Select all

<?PHP
 
/* PHP email queue processing script */
require_once "Mail.php";
 
// defile variables
$db_url = "localhost";
$db_name = "*****";
$db_user = "*****";
$db_pass = "*****";
 
$host = "mail.*****.com";//MAIL_SERVER
$username = "info@*****.com";//MAIL_USER
$password = "*****";//MAIL_PASS
 
$max_emails_per_batch = 20; // My host  only allow 35
 
// connect to database
$link = mysql_connect($db_url, $db_user, $db_pass);
if (!$link) {
die('Could not receive connection: ' . mysql_error());
}
if (!mysql_select_db($db_name, $link)) {
die('Could not connect to db: ' . mysql_error());
}
 
// query email_queue for records where success = 0
$sql = "SELECT * FROM email_queue WHERE success = 0 AND max_attempts != attempts LIMIT " . $max_emails_per_batch;
$result = mysql_query($sql, $link);
 
if (!$result) {
echo "DB Error, could not query the databasen";
echo 'MySQL Error: ' . mysql_error();
exit;
}
 
// check if records found
if (mysql_num_rows( $result )) {
 
// prepare mailer
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
 
// loop through records to send emails
while ($queued_mail = mysql_fetch_array($result)) {
// send email
 
$to =        $queued_mail['to_email'];
$subject =   $queued_mail['subject'];
$body =      $queued_mail['message'];
$from =      $queued_mail['from_name'] . ' < ' . $queued_mail['from_email'] . '>';
 
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
 
$mail = $smtp->send($to, $headers, $body);
 
if (PEAR::isError($mail)) {
// else update attempts, last attempt
$sql = "UPDATE email_queue SET " .
"attempts = attempts+1, " .
"last_attempt = now() " .
"WHERE id = '" . $queued_mail['id'] . "'";
mysql_query($sql, $link);
 
echo( $mail->getMessage() );
} else {
// if successful, update attempts, success, last attempt, date_sent
$sql = "UPDATE email_queue SET " .
"attempts = attempts+1, " .
"success = '1', " .
"last_attempt = now(), " .
"date_sent = now() " .
"WHERE id = '" . $queued_mail['id'] . "'";
mysql_query($sql, $link);
 
echo("Message successfully sent!");
}
} // end while (loop through records and sending emails)
} // no rows so quit
 
 
// release resources
mysql_free_result($result);
mysql_close($link);
?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Mail error

Post by Eric! »

Did your outlook express use port 25 too?

You're running this locally, right? Is your firewall blocking your script?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Mail error

Post by VladSun »

I’m getting the impression that my host has changed something but I can’t be sure.
If I were you, I would ask my hosting provider what had been changed :)
There are 10 types of people in this world, those who understand binary and those who don't
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Mail error

Post by Addos »

Thanks guys. I just realised that I needed to change

$host = "mail.****.com";//MAIL_SERVER

To

$host = "localhost";//MAIL_SERVER

and that solved the problem. I’m not fully sure I totally understand this right now but I think it was that I can use localhost as the site is hosted on that actual server and I didn’t need to use the SMTP outgoing server.

Thanks again
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Mail error

Post by VladSun »

Addos wrote:localhost as the site is hosted on that actual server and I didn’t need to use the SMTP outgoing server.
I would stay away from such hosting providers ... mail server and http server on a single machine :( ...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply