php code send mail using smtp code example

Example 1: smtp in php mail function

<?php

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

require_once "Mail.php";

$host = "ssl://smtp.dreamhost.com";
$username = "[email protected]";
$password = "your email password";
$port = "465";
$to = "[email protected]";
$email_from = "[email protected]";
$email_subject = "Subject Line Here:" ;
$email_body = "whatever you like" ;
$email_address = "[email protected]";

$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

Example 2: php mail function smtp settings

//Setup the SMTP server settings !
//INI Setup not needed on Linux when sendmail or postfix is installed
//mail DOES not support AUTH - USE ONLY WITH OPEN RELAY
ini_set("SMTP", "smtp.example.com");
ini_set("smtp_port", 25);
ini_set("sendmail_from", "[email protected]");

//DO NOT WASTE TIME SETTING THESE BELOW FOR AUTH - JUST USE PHP MAILER
/*
auth_username = username 
auth_password = password 
OR
username = username 
password = password 
*/


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= 'From:No Reply<[email protected]>' . "\r\n";
$headers .= 'Reply-To:Info<[email protected]>' . "\r\n";
$headers .= 'Cc:Carbon<[email protected]>' . "\r\n";


mail("[email protected]", "Subject", "Hello World!", $headers);

Tags:

Php Example