Send Email from PHP Script with SMTP Authentication


This article demonstrates the method for using PHP and SMTP Authentication to send mail. Below is example code.

Make sure you change the following variables:

  • from: the email address from which you want the message to be sent.
  • to: the recipient's email address and name. host: your outgoing SMTP server name.</li/>
  • username: the SMTP user name (typically the same as the user name used to retrieve mail).
  • password: the password for SMTP authentication.

Sending Mail from PHP Using SMTP Authentication - Example

<?php 
 require_once "check_the_correct_path_below/Mail.php";
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $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("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?

Sending Mail from PHP Using SMTP Authentication and SSL Encryption - Example

<?php
 require_once "check_the_correct_path_above/Mail.php";
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 $host = "ssl://mail.example.com";
 $port = "465";
 $username = "smtp_username";
 $password = "smtp_password";
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 $mail = $smtp->send($to, $headers, $body);
 if (PEAR::isError($mail)) {
   echo("
" . $mail->getMessage() . "
");
  } else {
   echo("

Message successfully sent!
");
  }
 ?
  • email tutorial, php
  • 5 Els usuaris han Trobat Això Útil
Ha estat útil la resposta?

Related Articles

Video: Create Email Address in cPanel

This video tutorial demonstrates how to create an email address using cPanel Shared Hosting...

How to Disable Simplified Account Creation in Outlook 2016

Summary When you configure an Office 365 account in Outlook 2016, you see the...

Video: Add MX Entry in cPanel

This video tutorial provides Step-by-Step instructions for adding an MX entry in cPanel Hosting...

Video: How to Enable Spam Protection in cPanel

This video tutorial provides Step-by-Step instructions for enabling spam protection in cPanel...

Configure Domain DNS for Use with GSuite Apps

This article provides an outline for configuring your Rad Web Hosting domains for use with G...