How to test PHP email functionality

How to test PHP email functionality

You can test PHP SMTP functions with the following two examples.
1. Normally PHP mail function without any authentication:
Copy
<?php

$from = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
$mail_result ="Mail failed";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' .$from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$to = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
$subject = "Mail sending from files";

if (mail($to,$subject,$body,$headers))
echo "mail sent" ;
else
echo "mail not sent" ;

?><
2. SMTP PHP mailer function with authentication email id and password:

We strongly recommend using an SMTP relay that requires authentication. Sending mail through unauthenticated SMTP servers (including the localhost relay on Cloud Sites) can result in delays or undelivered email due to stringent anti-spam filters.

Download the below links,

https://support.hostingraja.in/PHPMailer_5.2.0.zip

and unzip the PHPMailer_5.2.0.zip file, Then provide the proper mail details in Sendingmail.php file.

PHPMailer_5.2.0/Sendingmail.php
<?php
$hostname = "mail.domainname";
$SendingEmail = "Your_Email_ID";
$SendingPwd = "Your_secret_Email_Password";
$To = "To_address";
$Subject = "Subject";
$body = "Body";
$port = "465";
require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = $hostname; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $SendingEmail; // SMTP username
$mail->Password = $SendingPwd; // SMTP password
$mail->Port = $port;
$mail->SMTPDebug = 2;
$mail->From = $SendingEmail;
$mail->FromName = "support";
$mail->AddAddress($To,$Name);
$mail->SMTPSecure = "tls";
$mail->AddAddress($To,$Name);
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(false); // set email format to HTML
$mail->Subject = $Subject;
$mail->Body = $Body;

if(!$mail->Send())
{
echo "Message could not be sent.";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "Message has been sent $To from $SendingEmail \n";
?>
Hostname in SMTP mail function:
You are connecting your domain, just give the mail.domain otherwise your are configuring any other email.
For example, take as Gmail account
$hostname = "ssl://smtp.gmail.com";
$mail->Host = $hostname; // specify main and backup server
Port number in SMTP mail function:
Without an SSL connection,

SMTP server port for outgoing: 25 / 587

With an SSL connection,

SMTP server port for outgoing: 465

$port = "465";
$mail->Port = $port;
HTML or NON-HTML in SMTP mail function:
If you send mail with HTML or without HTML check once, If you're sending normal text mail. But in SMTP configuration your enabling the IsHTML is tue. The mail will reached to spam folder.
$mail->IsHTML(false); // set email format to HTML
So make sure you send mail with HTML tag or NOT.
Kindly provide the proper login details and check it.
Note: Mail.php is a PEAR module and is installed on the server. It is included in the default include_path for PHP, so requiring it here will work by default without any additional effort on your part.