Wednesday, April 18

PEAR Mail

So I've been needing to set up something so I can send automated emails from a website.

So... I created a simple PEAR Mail script, just to try it out. The script resulted in success, but I never received any emails. I changed the type to mail, which is just php's mail() function to see what was up and the error I got was SMTP server response: 554 Relay rejected for policy reasons... which makes sense, since I'm off campus working on a development server. I just wonder why the smtp mail type didn't tell me the error too. It would have been nice. Just to make this post look longer than it is, here's the code:

require_once 'Mail.php';

$recipients = 'me@example.com';

$params['host'] = 'smtp.server.com';
$params['auth'] = TRUE;
$params['username'] = 'email@example.com';
$params['password'] = 'passw0rd';

$headers['From'] = 'email@eaxmple.com';
$headers['To'] = 'me@example.com';
$headers['Subject'] = 'SMTP Test';

$body = 'test message';

$mail_object =&Mail::factory('smtp', $params);
$mail = $mail_object->send($recipients, $headers, $body);
if(PEAR::isError($mail)){
echo($mail->getMessage());
}else{
echo("success");
}

No comments: