How to Send PHP Mail with Authentication

 

You can download a fully functional PHP Contact Form for your Website at: Contact Form with PHP Email to Send Email from your Website

Utiware's SMTP Email Servers require authentication before emails can be sent.  Please use the following PHP emailing script to see how to include SMTP authentication in your PHP code. Replace "username@yourdomain.com"with your valid email address that is hosted on our server, and use its corresponding password for authentication.

Our Email Servers will not send emails on behalf of random people, so this is why the Sender of the email must match EXACTLY the email address that is used for SMTP Authentication.

For the $host variable, please replace "smtp.YourDomain.com" with your own domain's smtp address.

 <?php

require_once "Mail.php"//Mail.php is already installed on the Web Server. You do not need to upload this file

$from = "Matha Sender <username@yourdomain.com>";
$to = "James Recipient <receiver@gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you doing?";

$host = "smtp.YourDomain.com"; //replace this with your domain's SMTP address
$username = "username@yourdomain.com";
$password = "Put your username@yourdomain.com password here";

$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!");
}

?>

 

How to send HTML mails using PEAR Mail

To send HTML Mail, please add the following line to your PHP code to include mime features.

require_once "Mail/mime.php"; //mime.php is already installed on the Web Server. You do not need to upload this file

Please see below for an example code of how to send HTML emails using PEAR Mail

<?php

require_once ("Mail.php");
require_once ("Mail/mime.php"); //mime.php is already installed on the Web Server. You do not need to upload this file

$from = "Matha Sender <username@yourdomain.com>";
$to = "James Recipient <receiver@gmail.com>";
$subject = "Hi! This is the HTML Mail example";

$host = "smtp.YourDomain.com"; //replace this with your domain's SMTP address
$port = '25';
$username = "username@yourdomain.com";
$password = "Put your username@yourdomain.com password here";

$text = "Hello, How are you doing?";
$html_message = "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>HTML Email Test Page</title>
</head>
<body>
<p>Hello, How are you doing?</p>
</body>
</html>";

$headers['From'] = $from;
$headers['To'] = $to;
$headers['Subject'] = $subject;
$headers['Content-Type'] = 'text/html; charset=UTF-8';
$headers['Content-Transfer-Encoding']= '8bit';

$mime = new Mail_mime;
$mime->setTXTBody($text);
$mime->setHTMLBody($html_message);

$mimeparams=array();
$mimeparams['text_encoding']='8bit';
$mimeparams['text_charset']='UTF-8';
$mimeparams['html_charset']='UTF-8';
$mimeparams['head_charset']='UTF-8';
//$mimeparams['debug'] = 'True'; //Uncomment this if you want to view Debug information

$body = $mime->get($mimeparams);
$headers = $mime->headers($headers);

$smtpinfo['host'] = $host;
$smtpinfo['port'] = $port;
$smtpinfo['auth'] = true;
$smtpinfo['username'] = $username;
$smtpinfo['password'] = $password;
//$smtpinfo['debug'] = 'True'; //Uncomment this if you want to view Debug information

$mail=& Mail::factory('smtp', $smtpinfo);
$mail->send($to, $headers, $body);

?>



Please visit the following link for full documentation and examples of how to send HTML Emails using the installed Pear Mail_Mine.

Pear Mail Mime Documentation and Examples
http://pear.php.net/package/Mail_Mime/docs

Was this answer helpful?

 Print this Article

Also Read

SMTP Authentication is Required for ASP.NET

Dear Sir, Your sender of the Email address must be an email address that is hosted on our...

How to view the content of PHP's phpinfo() fuction

The phpinfo() function is turned off by default due to security reasons.  Information...

How to set "Write Permissions" to Files and Folders

By default and for security reasons, all the folders in your web hosting space are read-only. For...

How to Connect your ASP.Net Application to MySQL Database

You can connect your  ASP.Net Application directly to your MySQL Database by using "MySQL...

How to Send Emails using PHP Contact Form

Click on the following link to download a fully functional PHP Contact Form that you can upload...