I want to use the pear mail extension to send SMTP authenticated emails.
There are a lot of examples in the web… this is my code:
<?php
require_once "Mail.php";
$body = "Testtext";
$subject = "SMTP Testmail";
$mail_to = "valid.adress@test.com";
$mail_from = "valid.sender.adress@othertest.com";
//SMTP Verbindungsdaten
$host = "valid.smtp.com";
$username = "invalidusername@othertest.com";
$password = "validpasswordforotheruser";
$smtp = Mail::factory('smtp',
array (
'host' => $host,
//'auth' => true,
'auth' => true,
'username' => $username,
'password' => $password
));
$headers = array (
'From' => $mail_from,
'To' => $mail_to,
'Subject' => $subject
);
$mail = $smtp->send($mail_to, $headers, $body);
if (PEAR::isError($mail)) {
echo "Error during sending E-Mail : ". $mail->getMessage();
}
?>
The script worked well so I wanted to test the error case of this sample and changed the smtp user to something invalid and it sill worked… I have got my mail.
How can this be?
Greetings
edit:
I have done an other test now and changed the smtp server to be something invalid… documentation say that there must be an “PEAR_MAIL_SMTP_ERROR_CONNECT” error… I’m getting the following output:
“fsockopen() [function.fsockopen]: unable to connect to xx.xx.xx.xxx:25 (Connection refused) in /usr/local/lib/php/Net/Socket.php on line 108″
“Catchable fatal error: Object of class PEAR_Error could not be converted to string in /usr/local/lib/php/Net/SMTP.php on line 222”
Where does this IP coming from when the smtp name is invalid???
Can it be the server is overrules by any config?
This means the SMTP server does not validate the login data. It might not require authentication at all or support SMPT-after-POP (a horribly outdated thing by the way), i.e. if your IP authenticated on the corresponsing POP3 server recently the SMTP server will not require you to authenticate.
Try changing the server to something that is likely to require authentication all the time, e.g. the gmail SMTP.