I’m trying to send email using PHP. I found many posts here and I feel lost. I tried to use many codes but none of them works.
Finally, I read about PEAR and I have the following code, I’m working on Mac OSX 10.7.3 with MAMP having PHP 5.3.6.
What do I need to do to have the code works, it shows an error loading page!? Is there any configurations?
If anyone has a working example with instructions for configurations, please share.
<?php
require_once "Mail.php";
$recipients = 'receiver@live.com';
$headers['From'] = 'sender@gmail.com';
$headers['To'] = 'receiver@live.com';
$headers['Subject'] = 'Test message';
$body = 'Test message';
$params['sendmail_path'] = '/usr/lib/sendmail';
// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);
$mail_object->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
With PHP sending an email couldn’t be simpler:
That’s it. Keep in mind that your server needs to be configured to send email though.
http://php.net/manual/en/function.mail.php