You need to use smtp rather than php's native mail function. This example uses the PEAR package Mail.
PHP Code:
<?php
require_once ('Mail.php');
// details of options can be seen at http://pear.php.net/manual/en/package.mail.mail.factory.php
$options = array(
'host' => 'mail.yoursmtpserver.com',
'port' => 25 // or what ever yours is set at,
'persist' => true, // good for sending multiple emails
);
//If you need smtp authentication then add your type (detection howto below) and credentials
$options = array_merge($options, array(
'auth' => 'LOGIN',
'username' => 'yourusername',
'password' => 'yourpassword'
));
$mailer =& Mail::factory('smtp', $options);
if(PEAR::isError($mailer)) {
die('Could not create mailer: ' . $mailer->getMessage());
}
$recipients = 'who@where.com';
$headers = array(
'From' => 'you@where.com',
'To' => 'who@where.com',
'Subject' => 'Test Message',
'Date' => date('r')
);
$body = 'email body';
$error = $mailer->send($recipients, $headers, $body);
if(PEAR::isError($error)) {
die('Failed to send email: ' . $error->getMessage());
}
?>
To detect the supported auth types from your mailer server do, server responses in bold
Code:
telnet mail.yoursmtpserver.com 25
220 mail.yoursmtpserver.com NO UCE ESMTP
ehlo localhost
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-PIPELINING
250 8BITMIME
The supported auth types are after the 250-AUTH response, LOGIN, PLAIN etc.
You can of course expand on this further by sending multipart email, text/plain and text/html so that more users can see your email.
PHP Code:
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
$options = array(
'host' => 'mail.yoursmtpserver.com',
'port' => 25 // or what ever yours is set at,
'persist' => true, // good for sending multiple emails
'auth' => 'LOGIN',
'username' => 'yourusername',
'password' => 'yourpassword'
));
$mailer =& Mail::factory('smtp', $options);
if(PEAR::isError($mailer)) {
die('Could not create mailer: ' . $mailer->getMessage());
}
$recipients = 'who@where.com';
$headers = array(
'From' => 'you@where.com',
'To' => 'who@where.com',
'Subject' => 'Test Message',
'Date' => date('r')
);
$txtbody = 'email body';
$htmlbody = '<p>email body</p>';
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($txtbody);
$mime->setHTMLBody($htmlbody);
$body = $mime->get();
$headers = $mime->headers($headers);
$error = $mailer->send($recipients, $headers, $body);
if(PEAR::isError($error)) {
die('Failed to send email: ' . $error->getMessage());
}
?>
enjoy
