How to send email using PHP script?

For PHP, We provide pear mail using smtp authentication method , here is the code sample:

require_once "c:\php\pear\Mail.php";

$from = "Sandra Sender ";

$to = "Ramona Recipient ";

$subject = "Hi!";

$body = "Hi,\n\nHow are you?";

$host = "mail.example.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("

" . $mail->getMessage() . "

");

} else {

echo("

Message successfully sent!

");

}

?>

Please use your own mail credentials : mail.yourdomain.com as the email host.

Was this answer helpful?

 Print this Article

Also Read

I try to send and email throught Outlook but i got the error message "RELAY not allowed" why?

You have to enabled SMTP Authentication **Important - Always use this format username@domain.com...

I'm new to PHP, where should I start?

Well, try this.The official PHP web site as a lot of useful information:   ...

Why i couldn't fetch my variable from the form?

Register_Globals directive is set to OFF because of potential security issues.  PHP has this...

How can I call a command line executable from within PHP?

Check out the "Program Execution functions" here :...

Setup Email in iPhone or iPad

1.) Go to  Settings >>  Mail, Contacts, Calendars2.) To add a new Email account , choose...