PHP Mailer - Receiving Spam Emails - php

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.";
}
?>

Related

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! :)

Website Email Form (PHP) Issues

I know this is one of those things that gets asked a lot, but I'm not the most savvy web designer when it comes to more technical issues. The problem is this: on my website, www.imago-graphics.com, the email from the contact section doesn't go anywhere: neeither to my iPage inbox, nor my Google Apps email. I know it's probably an issue with the mail.php, but I'm not sure what it is. Here's the php (I've left the actual email address I want it to go to in the script (mariano#imago-graphics.com), but taken out the password; also, that address is a Google Apps email address, which I think might be part of the issue:
<?
require("class.phpmailer.php");
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mariano#imago-graphics.com"; // SMTP username
$mail->Password = "xxxxx"; // Password
$mail->From = "mariano#imago-graphics.com"; // SMTP username again
$mail->AddAddress("mariano#imago-graphics.com"); // Your Adress
$mail->Subject = "New mail from IMAGO Graphics";
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Body = "<p>You have recieved a new message from the enquiries form on your website.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Subject: </strong> {$subject} </p>
<p><strong>Message: </strong> {$message} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
if(!$mail->Send())
{
echo "Mail Not Sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Mail Sent";
?>
There are a few more params that you need to set to be able to send from a Gmail account. Please check their documentation on most up to date SMTP settings that you need to use.
These are couple things you have to have:
$mail->Username = "mariano#imago-graphics.com";
$mail->Password = "xxxxx";
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
During testing enable debug mode so you can see if the errors if there are any:
$mail->SMTPDebug = 2;

Will not redirect after submitting form - Form works, still get error

I have written this form, and it works perfectly. It sends the email fine. But after it sends, it errors. So people submitting the form think its broken, but it actually sends the form. All I am trying to do is redirect after the form submits to thanks.asp after the mail sends.
Any help is appreciated.
<?php
$name = trim($_POST['name']);
$company = $_POST['company'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$site_owners_email = 'support#macnx.com'; // Replace this with your own email address
$site_owners_name = 'Landin'; // replace with your name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address";
}
if (strlen($comments) < 3) {
$error['comments'] = "Please leave a comment.";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Website Contact Form";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $email . $company . $comments;
// GMAIL STUFF
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "inquiry#getgearhead.com"; // SMTP username
$mail->Password = "..."; // SMTP password
$mail->Send();
header("Location: http://www.macnx.com/noflash/thanks.asp");
}
?>
I believe the problem is that you use header(); incorrectly.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
PHP Documentation - header
i advice you to use javascript for redirection
just change this :
header("Location: http://www.macnx.com/noflash/thanks.asp");
to this :
echo '<script>
window.location.href = "http://www.macnx.com/noflash/thanks.asp";
</script>';

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 Mailer shows no errors but doesn't send the email

I'm creating a booking form as a favour but have decided to send mail via the domain, not the server. This is mainly for better security and less limits in responses and data transfers.
I've been working on this for the last few days and trying to teach myself how to get it to work. I now have a very simple working example which can be seen here below.
This is the simple booking form:
<form method="post" name="process.php" action="process.php">
<p>Name:</p><br><input type="text" name="name"><br><br>
<p>Email Address:</p><br><input type="email" name="email"><br><br>
<br>
<input type="submit" name="submit" value="Send Email">
Then in process.php I have this working code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['submit']))
{
// Values need to be santiised
$name = $_POST['name']; //Name of the person requesting a booking
$email = $_POST['email']; //Email of the person requesting a booking
require '../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'test#handler.net';
$mail->Password = '[PASSWORD]';
$mail->setFrom('test#handler.net'); // All emails would be sent from the handler.net domain to the bookings email of the other domain. (Using test to test, will be noreply)
$mail->addAddress('bookings#salon.com'); // Recipient of the email should be the bookings address, this won't change.
$mail->addReplyTo($email); // The reply to address will be the email address of the user who submitted the booking enquiry.
$mail->addBCC('outbox#handler.net'); // This is to keep a record of all emails that have been sent. Responses will also be saved to a CSV file.
$mail->Subject = 'Booking Request'; // Subject of the email sent to bookings#salon.com that the form responses will be contained within.
$mail->isHTML(TRUE);
$mail->Body = 'Booking request from '.$name.' with email '.$email; // Shows the salon the form response via the email and when they reply a new thread should start in order to compose a new email to reply to the email of the form submitter.
if(!$mail->send()) { // Send the email.
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
}
?>
The code above works and sends the emails from the right address, to the right address. That was on a site in public_html/testing and has been moved to a different site in public_html/booking so the relative paths will be the same. The only files within this directory are index.php (the form) and send.php (the process file with confirmation message)
For some reason this new code with all of the form values will not send. I'm honestly not too sure as to why it won't work now so any pointers at all would be massively appreciated.
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['submit']))
{
// Values need to be santiised
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];
require '../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply#handler.net';
$mail->Password = '[PASSWORD]';
$mail->setFrom('Handler | Bookings'); // Emails sent via Noreply.
$mail->addAddress('bookings#salon.com'); // Email form responses sent to bookings#salon.com
$mail->addReplyTo($email); // Reply to the user who submitted the form.
$mail->addBCC('outbox#handler.net'); // Store record of all emails sent via the system.
$mail->Subject = 'Booking Request'; // Subject of the email sent to bookings#salon.com that the form responses will be contained within.
$mail->isHTML(TRUE);
$mail->Body = '
Booking request from '.$forename.' with email '.$email;'
Test Values: $forename $surname $email $phone $service $date $time
if(!$mail->send()) { // Send the email.
echo '';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '';
}
}
?>
I don't need the echo statements at the end, I just need the email to send following this sort of format:
<img src="https://via.placeholder.com/300x150" width="15%">
<p><b>Name:</b> $forename $surname</p>
<p><b>Email:</b> $email</p>
<p><b>Phone:</b> $phone</p>
<p><b>Service:</b> $service</p>
<p><b>Date:</b> $date</p>
<p><b>Time:</b> $time</p>
I'm just not sure why the emails now will not send when they have done before. Any pointers would be appreciated.
UPDATE
Here's the updated code with the progress made with thanks to Mr Perfect
<?php
mail("bookings#salon.com", "test", "message");
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['submit']))
{
// Values need to be santiised
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];
$message = <<<DELIMETER
<img src="https://via.placeholder.com/300x150" width="15%">
<p><b>Name:</b> {$forename} {$surname}</p>
<p><b>Email:</b> {$email}</p>
<p><b>Phone:</b> {$phone}</p>
<p><b>Service:</b> {$service}</p>
<p><b>Date:</b> {$date}</p>
<p><b>Time:</b> {$time}</p>
DELIMETER;
require '../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply#handler.com';
$mail->Password = '[PASSWORD]';
$mail->setFrom('Handler | Bookings'); // Emails sent via Noreply.
$mail->addAddress('bookings#salon.com','ADMIN'); // Email form responses sent to bookings#salon.com.
$mail->addReplyTo($email); // Reply to the user who submitted the form.
// $mail->addBCC('outbox#handler.net'); // Store record of all emails sent via the system.
$mail->Subject = 'Booking Request | SUBMISSION'; // Subject of the email sent to bookings#salon.com that the form responses will be contained within.
$mail->isHTML(TRUE);
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) { // Send the email.
echo '';
echo '' . $mail->ErrorInfo; // I don't need to echo any errors because the submission page has the text above already.
} else {
echo '';
}
}
?>
You should look at your $mail->addAdress, $mail->addBCC and $mail->addReplyTo fields and follow the correct syntax for those fields.
Test the code below.
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['submit']))
{
// Values need to be santiised
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];
require '../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'test#handler.net';
$mail->Password = '[PASSWORD]';
$mail->setFrom('test#handler.net','Bookings'); // Emails sent via Noreply.
$mail->addAddress('bookings#salon.com',''); // Email form responses sent to bookings#salon.com
$mail->addReplyTo($email,$forename.' '.$surname); // Reply to the user who submitted the form.
$mail->addBCC('outbox#handler.net',''); // Store record of all emails sent via the system.
$mail->Subject = 'Booking Request'; // Subject of the email sent to admin#handler.net that the form responses will be contained within.
$mail->isHTML(TRUE);
$mail->Body = <<<EOD
Booking request from {$forename} with email {$email}.<br />
Contact details: <br />
Full name: {$forename} {$surname}<br />
Email: {$email} <br />
Phone number: {$phone} <br />
Service: {$service} <br />
Date: {$date} {$time}
EOD;
if(!$mail->send()) { // Send the email.
echo '';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '';
}
}
?>
This block of code is wrong, some quotes are missing
$mail->Body = '
Booking request from '.$forename.' with email '.$email;'
Test Values: $forename $surname $email $phone $service $date $time
To enable variable replacement on a string use double quotes, and you won't need to concatenate variables with dot(.), also this make possible to use escaped caracters like \n, try to adjust your code like this:
$mail->Body = "Booking request from $forename with email $email\n" .
"Test Values: $forename $surname $email $phone $service $date $time";
Firstly we have to view the error so therefore you have to set
$mail->SMTPDebug = 0;
into
$mail->SMTPDebug = 3;
So you can get that error to post against that error.

Categories