Knowledgebase

How to use PHP mail using SMTP authentication code?

How to use PHP mail using SMTP authentication code?



How to use PHP mail using SMTP authentication code?


If you are using PHP, then you can make use of PHP PEAR mail package authentication. You need to use the below mentioned code. 

require_once "Mail.php";

$from = "sender@example.com"; 
$to = "recipient@example.com"; 
$subject = "Hi";
$body = "Hi, How are you?";

$host = "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() . "</p>"); 
} else { 
echo("

Message successfully sent!

"); 


* In above mentioned code, you need to assign proper values for text starting with "$". 
  • 0 Users Found This Useful
Was this answer helpful?