Using SMTP with PHP Mail from Template File

295 Views Asked by At

I have an HTML template with a contact form. The problem is it used to send emails without a problem on our hosting but the client's hosting wants SMTP to be added. Below I have the code from template mail.php file but I need a help regarding how to add SMTP function.

<?php
error_reporting(0);
$mainemail = "[email protected]";//Change this mail address.

$name       = $_POST['name'];
$email      = $_POST['email'];
$subject    = $_POST['subject'];
$message    = $_POST['message'];

$mailinfo  = "MIME-Version: 1.0\r\n";
$mailinfo .= "Content-type: text/html; charset=utf-8\n";
$mailinfo .= "From: [email protected] \r\n";//Change this mail address.
$mailinfo .= "Reply-To: $name <$email>\r\n";
$sms  = "Name : ".$name."<br />E-Mail : ".$email."<br />Subject : ".$subject."<br />Message : ";
$sms .= $subject;

$mail = mail($mainemail, $subject ,stripslashes($sms), $mailinfo);
if($mail){
echo "<span>Your mail has been send.</span>";
}
else{
echo "<span>Error! Please try again.</span>";
}
?>

Could you guys please help me with that issue?

1

There are 1 best solutions below

0
On

If you need to send mails by smtp I suggest you to do it with a library like https://github.com/Synchro/PHPMailer

or including the class https://github.com/Synchro/PHPMailer/blob/master/class.smtp.php

in your project