Sending email without authentication
<?php
$to = 'nobody@whateverdomain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@whateverdomain.com' . "\r\n" .
'Reply-To: webmaster@whateverdomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
The correct way of sending email is through a SMTP connection.
Assuming the PEAR Mail package is installed.
<?php
require_once "Mail.php";
$from = "Name Surname <sender@yourdomain.com>";
$to = "Name Whatever <recipient@example.com>";
$subject = "Subject!";
$body = "Hi,\n\nHow are you?";
$host = "mail.yourdomain.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Assuming you have Zend Framework, you can do the same by sending through Zend_Mail over SMTP. The example below uses information on Google SMTP
require_once 'Zend/Loader/Autoloader.php'; //Should be in the include_path
$autoloader = Zend_Loader_Autoloader::getInstance();
$config = array('ssl' => 'tls', 'port' => 587, 'auth' => 'login', 'username' => 'username@gmail.com', 'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
if (strtolower($this->getType()) == 'html')
$mail->setBodyHtml($this->getBody());
}
else {
$mail->setBodyText($this->getBody());
}
$mail
->setFrom($this->getFromEmail(), $this->getFromName())
->addTo($this->getToEmail(), $this->getToName())
->setSubject($this->getSubject());
$mail->send($transport);