How to use smtp function in php with authentication - php

My code is as given below. I would like to know how to use smtp function in php with authentication in contact form. If I use hotmail id I am not able to recive the inquiry but it works fine for gmail id. It would be great if someone help in me editing my code noted below
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" Name: $name \n Email: $email \n Phone: $phone \n Message: $message";
$to = "abc#hotmail.com";
$subject = "Contact Form";
$mailheader = "From: $name \r\n";
mail($to, $subject, $formcontent, $mailheader) or die("Error!");
echo "<br><br><p style='color:#000'><b>Thank You $name</b></p><p style='color:#000'>We will be in touch as soon as possible.</p>";
echo "<p style='color:#000'>Go to <a href='index.html'><b>Home Page</b></a></p>";
?>

You can use PHPMailer Library to do this.
Example:
$mail = new PHPMailer();
$mail->SMTPSecure = 'tls';
$mail->Username = "abc#hotmail.com";
$mail->Password = "Your password";
$mail->AddAddress("mymail#hotmail.com");
$mail->FromName = "Your Name";
$mail->Subject = "Contact Form";
$mail->Body = "The Body";
$mail->Host = "smtp.live.com";
$mail->Port = 587;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
$mail->Send();

Related

PHP Mailer - Receiving Spam Emails

I have a website with a "book appointment" section which requires all fields to be completed before sending an email. Since the website is relatively popular, we get many requests during the day. I am using PHPMailer to receive e-mails.
Unfortunately, some of the email I receive are in this format:
A client has made a request to book an appointment.
Client Name: 5c3a5beb89e89
Client Email: sbethdddany5996#gmail.com
Client Number:
Message:
The e-mail is a spam email. How can I avoid this? The following is my PHP script.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$msg = $_POST['msg'];
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "***";
$mail->SMTPSecure = "tsl";
$mail->Port = ***;
$mail->SMTPAuth = true;
$mail->Username = '***';
$mail->Password = '***!';
$mail->setFrom($email, $totalName);
$mail->addAddress('***');
$mail->Subject = "Appointment Request";
$mail->Body = "A client has made a request to book an apointment.
\n\nClient Name: $name \n\nClient Email: $email \n\nClient Number: $phone \n\nMessage: $msg";
if ($mail->send()){
header('Location: index.html');
}
else{
echo "Something went wrong, please try again.";
}
?>

PHPMailer error - SMTP Error: Data not accepted

I have done a contact form and obviously I would like to receive a confirmation email when a user fill out this form on my website.
I'm using PHPMailer. When I test the code on my computer as localhost, everything works fine and confirmation message is received properly.
Here you are the PHP script:
<?php
$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if ($Name=='' || $Email=='' || $Message==''){
echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";
}else{
require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $Email;
$mail->FromName = $Name;
$mail->AddAddress("myemail#gmail.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form";
$mail->Body = "Name: $name \n<br />".
"Email: $Email \n<br />".
"Message: $Message\n<br />";
// SMTP Server
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";
}
?>
The problem is when I upload this PHP script on a web hosting (i.e 123-reg.co.uk), it doesn´t work.
Error message:
SMTP Error: Data not accepted
Here you are the PHP script:
<?php
$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if ($Name=='' || $Email=='' || $Message==''){
echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";
}else{
require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $Email;
$mail->FromName = $Name;
$mail->AddAddress("email#mydomainat123-reg.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form";
$mail->Body = "Name: $name \n<br />".
"Email: $Email \n<br />".
"Message: $Message\n<br />";
// SMTP Server
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.123-reg.co.uk";
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "email#mydomainat123-reg.com";
$mail->Password = "password";
if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";
}
?>
I have tried uploading and executing this PHP script from another free web hosting but it was to not avail.
I'm kind of new in PHP, especially in dealing with mail functions.
What can be the reason? Any additional help would be appreciated.
Thanks in advance for your help.
Your server doesn't allow different sender and username you should config: $mail->From like $mail->Username
Moreover...
Most times I've seen this message the email gets successfully sent anyway, but not always. To debug, set:
$mail->SMTPDebug = true;
You can either echo the debug messages or log them using error_log():
// 'echo' or 'error_log'
$mail->Debugoutput = 'echo';
A likely candidate especially on a heavily loaded server are the SMTP timeouts:
// default is 10
$mail->Timeout = 60;
class.smtp.php also has a Timelimit property used for reads from the server!
Hope this information helps! :)

Converting PHP Contact Form to SMTP

had alot fo problems making a functional contact form tonight. After messing for hours i finally found out that i can only use SMTP with my web hoster.
Can anyone please advice me how i can complete my form?
This is my current SMTP PHP Form
$mail = new PHPMailer();
//Your SMTP servers details
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server or localhost
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "enquiries#c(hidden)y.co.uk"; // SMTP username
$mail->Password = "******"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Display Name";
$mail->AddAddress("enquiries#c(hidden)y.co.uk", "chapnolo"); //Email address where you wish to receive/collect those emails.
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $_POST['subject'];
$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :".$_POST['query'];
$mail->Body = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
header("Location: $redirect_url");
}
?>
I need my form to have the following functionality for me to retrieve the requires e-mail message body.
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$web = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
Help will be greatly appreciated
Metexora - So you're not having any issues with the code, you just haven't told it to send you the name contact number, email and message body - just change:
$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :".$_POST['query'];
to
$message = "Name of the requestor :".$name." \r\n <br>Email Adrress :".$email." \r\n <br> Phone number :".$number."\r\n <br> Message: ".$message."\r\n <br> Website: ".$web;
(Note: Be careful, you are re-declaring $message here, that isn't really recommended practise given they refer to different things, the user message and the email message, you should only use the change the contents of a variable if it still refers to the same thing)

Replacing mail() script with SMTP script [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
PHP's mail() function has stopped sending my emails. I talked with my service provider and they said I have to use SMTP however i am not professional php developer. They tried to give a little help and they provided me with a script which sends email to me when a user submit a form, but my old form used to send email to me of the clent data as well as a confirmation email email to the client.
The new script i have manged some how to send a confirmation email for the client, but I don't know how send email with this script, can you tell me what I should add to make it work?
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$state = $_POST['us'];
$details = $_POST['details'];
$reasons = $_POST['re'];
$content = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
//Your SMTP servers details
// set mailer to use SMTP
$mail->IsSMTP();
// specify main and backup server or localhost / your mail server yourmailserver.com
$mail->Host = "xxxxxx.example.com";
// turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = "me#xxxxx.com";
$mail->Password = "**********"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
//$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Admin";
//Email address where you wish to receive/collect those emails.
$mail->AddAddress($visitor_email, "");
$mail->WordWrap = 50;// set word wrap to 50 characters
$mail->IsHTML(true);// set email format to HTML
$mail->Subject = 'You have mail from: www.example.com';
// ** Build the Message
$message = "<h4><b>We received your query</b></h4><br/><br/>";
$message .="Thankyou for contacting example.net.<br/>";
$message .="<br>";
$message .="Regards,<br/>";
$message .="admin";
$mail->Body = $message;
//header("Location: thank-you.html");
if(!$mail->Send())
{
header('Location: thank-you.html');
// echo "Message could not be sent. <p>";
// echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
header('Location: thank-you.html');
}
else{
header('Location: contact-us.html');
}
?>
// ******** Second update
<?
ob_start();
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$state = $_POST['us'];
$details = $_POST['details'];
$reasons = $_POST['re'];
$content = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
//Your SMTP servers details
// set mailer to use SMTP
$mail->IsSMTP();
// specify main and backup server or localhost / your mail server yourmailserver.com
$mail->Host = "xxx.example.net";
// turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = "*****#example.net";
$mail->Password = "********"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
//$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Admin vueproperty";
$mail->AddAddress($visitor_email, ""); //Email address where you wish to receive/collect those emails.
//$mail->AddCC($_POST['email']); // ** Client email
$mail->WordWrap = 50;// set word wrap to 50 characters
$mail->IsHTML(true);// set email format to HTML
$mail->Subject = 'You have mail from: www.exmple.net';
// ** Build the Message
$message = "<h4><b>We received your query</b></h4><br/><br/>";
$message .="Thank you for contacting example.net. A property consultant will be in contact with you asap.<br/>";
$message .="<br>";
$message .="Regards,<br/>";
$message .="The example Team";
$mail->Body = $message;
//header("Location: thank-you.html");
if($mail->Send())
{
$body = "Name: $name<br/>";
$body .= "Phone: $phone<br/>";
$body .= "Email: $visitor_email<br/>";
$mail->AddAddress('xphpxmysql#gmail.com', "MINE");
if($mail->Send()){
header('Location: thank-you.html');
exit;
}
}
}
?>
// ******** Third update
<?
ob_start();
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$state = $_POST['us'];
$details = $_POST['details'];
$reasons = $_POST['re'];
$content = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
//Your SMTP servers details
// set mailer to use SMTP
$mail->IsSMTP();
// specify main and backup server or localhost / your mail server yourmailserver.com
$mail->Host = "xxx.example.net";
// turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = "*****#example.net";
$mail->Password = "********"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
//$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Admin vueproperty";
$mail->AddAddress($visitor_email, ""); //Email address where you wish to receive/collect those emails.
//$mail->AddCC($_POST['email']); // ** Client email
$mail->WordWrap = 50;// set word wrap to 50 characters
$mail->IsHTML(true);// set email format to HTML
$mail->Subject = 'You have mail from: www.exmple.net';
// ** Build the Message
$message = "<h4><b>We received your query</b></h4><br/><br/>";
$message .="Thank you for contacting example.net. A property consultant will be in contact with you asap.<br/>";
$message .="<br>";
$message .="Regards,<br/>";
$message .="The example Team";
$mail->Body = $message;
//header("Location: thank-you.html");
if($mail->Send())
{
$body = "Name: " . $name . "<br/>";
$body .= "Phone: " . $phone . "<br/>";
$body .= "Email: ". $visitor_email . "<br/>";
$mail->body = $body;
$mail->AddAddress('xphpxmysql#gmail.com', "MINE");
if($mail->Send())
{
header('Location: thank-you.html');
exit;
}
}
}
?>
//**** The fifth update
<?
ob_start();
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$state = $_POST['us'];
$details = $_POST['details'];
$reasons = $_POST['re'];
$content = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
//Your SMTP servers details
// set mailer to use SMTP
$mail->IsSMTP();
// specify main and backup server or localhost / your mail server yourmailserver.com
$mail->Host = "xxx.example.net";
// turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = "*****#example.net";
$mail->Password = "********"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
//$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Admin vueproperty";
$mail->AddAddress($visitor_email, ""); //Email address where you wish to receive/collect those emails.
//$mail->AddCC($_POST['email']); // ** Client email
$mail->WordWrap = 50;// set word wrap to 50 characters
$mail->IsHTML(true);// set email format to HTML
$mail->Subject = 'You have mail from: www.exmple.net';
// ** Build the Message
$message = "<h4><b>We received your query</b></h4><br/><br/>";
$message .="Thank you for contacting example.net. A property consultant will be in contact with you asap.<br/>";
$message .="<br>";
$message .="Regards,<br/>";
$message .="The example Team";
$mail->Body = $message;
if($mail->Send())
{
$body = "Name: " . $name . "<br/>";
$body .= "Phone: " . $phone . "<br/>";
$body .= "Email: ". $visitor_email . "<br/>";
$mail->Body = $body;
$mail->AddAddress('xphpxmysql#gmail.com', "MINE");
if($mail->Send())
{
header('Location: thank-you.html');
exit;
}
}
}
?>
//// **** Sixth update NOW it's working it's working
<?
ob_start();
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$state = $_POST['us'];
$details = $_POST['details'];
$reasons = $_POST['re'];
$content = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
//Your SMTP servers details
// set mailer to use SMTP
$mail->IsSMTP();
// specify main and backup server or localhost / your mail server yourmailserver.com
$mail->Host = "xxx.example.net";
// turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = "*****#example.net";
$mail->Password = "********"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
//$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Admin vueproperty";
$mail->AddAddress($visitor_email, ""); //Email address where you wish to receive/collect those emails.
//$mail->AddCC($_POST['email']); // ** Client email
$mail->WordWrap = 50;// set word wrap to 50 characters
$mail->IsHTML(true);// set email format to HTML
$mail->Subject = 'You have mail from: www.exmple.net';
// ** Build the Message
$message = "<h4><b>We received your query</b></h4><br/><br/>";
$message .="Thank you for contacting example.net. A property consultant will be in contact with you asap.<br/>";
$message .="<br>";
$message .="Regards,<br/>";
$message .="The example Team";
$mail->Body = $message;
$mail->Send();
$mail->ClearAllRecipients();
$body = "Name: " . $name . "<br/>";
$body .= "Phone: " . $phone . "<br/>";
$body .= "Email: ". $visitor_email . "<br/>";
$mail->Body = $body;
$mail->AddAddress('xphpxmysql#gmail.com', "");
if($mail->Send())
{
$mail->ClearAllRecipients();
header('Location: thank-you.html');
exit;
}
}
if i understand exactly your question. you were able to send the email to your client but you want to send it also to you.
if this the case then. add another receptionist$mail->AddAddress($visitor_email, "MY_CLIENT");
EDIT:
first get red of the second line.
if($mail->Send()){$message = "ALL REQUIRED DATA YOU WANT TO SEND TO YOURSELF";$mail->AddAddress('YOUREMAIL#EXAMPLE.COM', "MINE");if($mail->Send()){header('Location: thank-you.html');exit;}}

PHP Contact Form Best Practice

I'm setting up a PHP contact form on a site. I'm using the Swift Mailer library to send the mail, and my domain email is through Google Apps. Is it possible to use Google Apps for company email and use sendmail/SMTP on my VPS to send email from the contact page? The problem I'm having is that I can't dynamically generate the from address, Google's servers force that to be the email address that the email is going through. Thanks in advance.
I use PHPMailer with this function...
function email($to, $subject, $body){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->SetFrom("anything#domain.com", "Any Thing");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
$mail->Send();
unset($mail);
}
After doing some reading, I realized that I didn't need a mail library at all. I'm using PHP's mail() function to accomplish exactly what I wanted, sending form mail through sendmail and having Google Apps handle all domain email. Here's the relevant code that's working for me.
// Define message variables
if(get_magic_quotes_gpc()){
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$body = stripslashes($_POST['body']);
}else{
$name = $_POST['name'];
$email = $_POST['email'];
$body = $_POST['body'];
}
$subject = "Website Contact Form";
$recipient = "web#somesite.com";
$content = "NAME: $name, $email\nCOMMENT: $body\n";
$mailheader = "MIME-Version: 1.0\r\n";
$mailheader .= "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Bcc: another.email#address.com" . "\r\n";
mail($recipient, $subject, $content, $mailheader) or die("Failure");
header("Location:/thankyou.php");
}
This is working perfectly for me. Hope it helps someone else.

Categories