I have a form in my website, and it works well. It was part of a oakler template and so far I received subject,email,message information back.
I added a telephone field trying to follow the structure of the form but am no php expert and don't know what is not working.
In the email sent I receive the field 'telephone' but it is empty.
Please help.
The website is this: http://ruidematos.co.uk/contact-psychologist-hypnotherapist-london-harley-street
The form html is this:
<form id="contactForm" action="php/contact-form.php" method="POST">
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" placeholder="Subject" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control input-lg" name="subject" id="subject" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<textarea maxlength="5000" placeholder="Message" data-msg-required="Please enter your message." rows="10" class="form-control input-lg" name="message" id="message" required></textarea>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" placeholder="Your Name" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control input-lg" name="name" id="name" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="email" placeholder="Your E-mail" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control input-lg" name="email" id="email" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" placeholder="Your Telephone" value="" data-msg-required="Please enter your telephone." data-msg-email="Please enter a valid number." maxlength="30" class="form-control input-lg" name="phone" id="phone" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary btn-lg mb-xs" data-loading-text="Loading...">
</div>
</div>
</form>
The PHP code is this:
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php');
// Step 1 - Enter your email address below.
$email = 'info#ruidematos.co.uk';
// If the e-mail is not working, change the debug option to 2 | $debug = 2;
$debug = 0;
$subject = $_POST['subject'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
),
3 => array(
'text' => 'Telephone',
'val' => $_POST['phone']
)
);
$message = '';
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug; // Debug Mode
// Step 2 (Optional) - If you don't receive the email, try to configure the parameters below:
//$mail->IsSMTP(); // Set mailer to use SMTP
//$mail->Host = 'mail.yourserver.com'; // Specify main and backup server
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'user#example.com'; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
//$mail->Port = 587; // TCP port to connect to
$mail->AddAddress($email); // Add another recipient
//$mail->AddAddress('person2#domain.com', 'Person 2'); // Add a secondary recipient
//$mail->AddCC('person3#domain.com', 'Person 3'); // Add a "Cc" address.
//$mail->AddBCC('person4#domain.com', 'Person 4'); // Add a "Bcc" address.
$mail->SetFrom($email, $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
$arrResult = array ('response'=>'success');
} catch (phpmailerException $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}
if ($debug == 0) {
echo json_encode($arrResult);
}
You have added the HTML element for phone correctly, but it seems the Javascript handler that submits the form has a specified list of elements it fetches, and not the entire form.
Look in http://ruidematos.co.uk/js/views/view.contact.js , within the function $('#contactForm').validate( on line 26 the ajax call is performed and submits via POST the 4 elements name, email, subject, ,message:
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: {
name: $form.find('#name').val(),
email: $form.find('#email').val(),
subject: $form.find('#subject').val(),
message: $form.find('#message').val()
}
})
After the subject line add one for phone too and it should work.
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: {
name: $form.find('#name').val(),
email: $form.find('#email').val(),
subject: $form.find('#subject').val(),
phone: $form.find('#phone').val(),
message: $form.find('#message').val()
}
})
Your rename message to phone.
name: $form.find('#name').val(),
email: $form.find('#email').val(),
subject: $form.find('#subject').val(),
phone: $form.find('#phone').val(),
message: $form.find('#message').val()
Related
I am writing a contact form to collect data.
My app is on Heroku. It is a PHP application. SSL configured as well.
The PHPMailer is configured correctly as far as I am aware of. Following is the php code which does that. (contact.php)
<?php require 'PHPMailer-master/PHPMailerAutoload.php';
$fromEmail = 'myemail#gmail.com';
$fromName = 'Application Form';
$sendToEmail = 'myotheremmail#gmail.com';
$sendToName = 'Applied';
$subject = 'New message from contact form';
// smtp credentials and server
$smtpHost = 'smtp.gmail.com';
$smtpUsername = 'myemail#gmail.com';
$smtpPassword = 'passwordformyemail';
$fields = array('name' => 'Name', 'linkedin' => 'LinkedIn URL', 'phone' => 'Phone Number', 'email' => 'Email', 'message' => 'Cover Letter', 'notice' => 'Notice Period', 'salary' => 'Salary Expectation');
// message that will be displayed when everything is OK
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
error_reporting(E_ALL & ~E_NOTICE);
try {
if (count($_POST) == 0) {
throw new \Exception('Form is empty');
}
$emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Have a nice day,<br>Best,<br>Me</p>";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $smtpUsername;
//Password to use for SMTP authentication
$mail->Password = $smtpPassword;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName);
$mail->addReplyTo($fromEmail, $fromName);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $emailTextHtml;
$mail->msgHTML($emailTextHtml);
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
if (!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
Here is the contact form code (index.php),
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Name *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your Name *" required="required" data-error="Name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_linkedin">LinkedIn URL</label>
<input id="form_linkedin" type="text" name="linkedin" class="form-control" placeholder="Please enter your LinkedIn profile URL" data-error="LinkedIn URL is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_phone">Phone *</label>
<input id="form_phone" type="tel" name="phone" required="required" class="form-control" placeholder="Please enter your phone number" data-error="Valid Phone number is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Required Notice Period *</label>
<input id="form_email" type="text" name="notice" class="form-control" placeholder="Please enter the required period of notice to previous work place *" required="required" data-error="Notice Period detail is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_salary">Salary Expectation *</label>
<input id="form_salary" type="number" name="salary" class="form-control" placeholder="Please enter your expected salary in Sri Lankan Rupees *" required="required" data-error="Salary expectation is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Cover Letter *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Please write Cover Letter *" rows="4" required="required" data-error="Cover Letter is required"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Submit Application">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
Then I have the JS code (index.js) This is added as an external script in index.php,
$(function () {
$('#contact-form').validator();
// when the form is submitted
$('#contact-form').on('submit', function (e) {
// if the validator does not prevent form submit
if (!e.isDefaultPrevented()) {
var url = "contact.php";
// POST values in the background the the script URL
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#contact-form').find('.messages').html(alertBox);
// empty the form
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
The Heroku application log gives the following when the form is filled and submitted,
2021-07-18T10:47:27.738111+00:00 app[web.1]: 10.181.143.206 - - [18/Jul/2021:10:47:27 +0000] "POST /applications/contact.php HTTP/1.1" 200 2177 "https://www.example.com/applications/" "Mozilla/5.0 (Linux; Android 11; GM1910) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36
As I am using gmail SMTP,
I have switched off two factor authentication
I have switched on less secure access
Nothing on the error logs as well.
I have added the following files for PHPMailer to work,
class.phpmailer.php
class.smtp.php
PHPMailerAutoload.php
I tried following the tutorials available on the web and checking the issue but had no luck.
Is there any silly mistake that I am doing here or have I missed an important point regarding PHPMailer and Heroku which makes this not to work?
Any help is greatly appreciated!
Hope you guys can assist me on this. When someone completes my form I want the form I receive to have a "replyto" that implements the clients email address and not reply to the server.
So basically when I receive the from from the client I want to be able to say reply, where it shows the clients email address
Really hope you can help me on this
<form id="contact-form" method="post" action="contact-2.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="col-md-4">
<div class="form-group">
<label for="form_name"></label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="form_email"></label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="form_subject"></label>
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Please enter your subject*" required="required" data-error="Subject is required">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="form_message"></label>
<textarea id="form_message" name="message" class="form-control" placeholder="Your Message*" rows="9" required data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-4 col-md-offset-4">
<input type="submit" class="btn btn-primary btn-block btn-lg" value="Send message">
</div>
</div>
</form>
<?php
/*THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION*/
require 'PHPMailer-master/PHPMailerAutoload.php';
/**CONFIGURE EVERYTHING HERE*/
// an email address that will be in the From field of the email.
$fromEmail = 'Demo#gmail.com';
$fromName = 'Demo#gmail.com';
// an email address that will receive the email with the output of the form
$sendToEmail = 'Demo#gmail.com';
$sendToName = 'Demo#gmail.com';
// subject of the email
$subject = 'contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*LET'S DO THE SENDING*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Have a nice day,<br>Kind Regards,<br>Felleng Tours</p>";
$mail = new PHPMailer;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
$mail->addReplyTo($from);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy
if(!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e){
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}// else just display the message
else {
echo $responseArray['message'];
}
?>
It's not clear where $from is defined, but you should be able to do:
$mail->addReplyTo($_POST['email']);
That will use your form's email address field as a reply-to address.
You've got a try/catch block, but you've not told PHPMailer to use exceptions, which you can do by passing true to the constructor:
$mail = new PHPMailer(true);
That way you don't need to throw your own exceptions. You're also using an old version of PHPMailer.
I have a questionnaire form on my website that emails me answers submitted by visitors. One of the questions asks users for the best contact time (morning, afternoon, evening) The name of that select class is contact-time.
Currently in the body of the email, it's displayed as Contact-time just like I named it in the html form. I want it to display as Contact Time.
How can I build the body of the email so that it displays contact-time as Contact Time while still being able to fetch the value from select class named contact-time? I've looked through pretty much all questions regarding this and couldn't find any relevant information.
PHP code that builds the email and then sends it
<?php
function constructEmailBody () {
$fields = array("name" => true, "email" => true, "address" => true, "phone" => true, "contact-time" => true);
$message_body = "";
foreach ($fields as $name => $required) {
$postedValue = $_POST[$name];
if ($required && empty($postedValue)) {
errorResponse("$name is empty.");
} else {
$message_body .= ucfirst($name) . ": " . $postedValue . "\n";
}
}
return $message_body;
}
//attempt to send email
$emailBody = constructEmailBody();
require './vender/php_mailer/PHPMailerAutoload.php';
$email = new PHPMailer;
$email->CharSet = 'UTF-8';
$email->isSMTP();
$email->Host = 'smtp.gmail.com';
$email->SMTPAuth = true;
$email->Username = 'email#domain.com';
$email->Password = 'MyPassword';
$email->SMTPSecure = 'tls';
$email->Port = 587;
$email->setFrom($_POST['email'], $_POST['name']);
$email->addAddress('email#domain.com');
$email->Subject = 'Estimate Request Answers';
$email->Body = $emailBody;
//try to send the message
if($email->send()) {
echo json_encode(array('message' => 'Thank you! Your message was successfully submitted.'));
} else {
errorResponse('An unexpected error occured while attempting to send the email: ' . $email->ErrorInfo);
}
?>
HTML form
<form role="form" id="estimateForm" method="POST">
<div class="col-xs-12 contact-information">
<div class="form-group">
<fieldset>
<legend>Contact Information</legend>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<label class="control-label" for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Name">
<label class="control-label" for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email">
<label class="control-label" for="address">Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Enter Address">
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<label class="control-label" for="phone">Phone Number</label>
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Enter Phone Number">
<label class="control-label" for="contact-time">Preferred Contact Time</label>
<select class="selectpicker contact-time" id="contact-time" name="contact-time" title="Preferred Contact Time">
<option value="Morning">Morning (9-11am)</option>
<option value="Afternoon">Afternoon (11-2pm)</option>
<option value="Evening">Evening (2-5pm)</option>
</select>
<label class="control-label" for="contact-method">Preferred Contact Method</label>
<select class="selectpicker contact-method" id="contact-method" name="contact-method" title="Preferred Contact Method">
<option>By Phone</option>
<option>By Email</option>
</select>
</div>
</fieldset>
</div>
</div>
</form>
Thank you for your time in advance and I appreciate any help.
nothing to do with the mail code you can use a simple str_replace() on $name
or
if($name =='whatever'){
$name='something else';
}
or about 10 other options
I have a contact us page in html including a form where a user can send message with that form. I want to send the message with PHP mailer function. But the problem is after i sending the message I want to redirect to the page with message. But instead of loading the it's showing the response in a new page. How do i send the user to the same page again?
Here is my Contact.html
<?php
if (isset($arrResult)) {
if($arrResult['response'] == 'success') {
?>
<div class="alert alert-success" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.
</div>
<?php
} else if($arrResult['response'] == 'error') {
?>
<div class="alert alert-danger" id="contactError">
<strong>Error!</strong> There was an error sending your message. (<?php echo $arrResult['error'];?>)
</div>
<?php
}
}
?>
<h2 class="mb-sm mt-sm"><strong>Contact</strong> Us</h2>
<form id="contactForm" action="php/contact-form.php" method="POST">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name" required>
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message" id="message" required></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" onclick="myFunction()"class="btn btn-primary btn-lg mb-xlg" data-loading-text="Loading...">
</div>
</div>
</form>
</div>
Here is the contact-form.php
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php');
$email = 'myemail#yahoo.co';
$subject = $_POST['subject'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = '';
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug;
$mail->AddAddress($email);
$mail->SetFrom($email, $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
$arrResult = array ('response'=>'success');
} catch (phpmailerException $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}
if ($debug == 0) {
echo json_encode($arrResult);
}
Use header
header('Location: http://www.example.com/')
Header doc
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
This is code of my form.
<form method="post" action="mailer.php" id="contactfrm">
<div class="col-sm-4">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Enter name" title="Please enter your name (at least 2 characters)">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="comments">Message</label>
<textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
</div>
<button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Submit</button>
<div class="result"></div>
</div>
</form>
Here My mailer.php
<?php
$replyemail="my email";
$name = $_POST["name"];
$email = $_POST["email"];
$thesubject = "Project With Me Query";
$themessage = $_POST["message"];
$success_sent_msg='<p align="center"><strong> </strong></p>
<p align="center"><strong>Your message has been successfully sent to My Email<br>
</strong> and I will reply as soon as possible.</p>
<p align="center">A copy of your query has been sent to you.</p>
<p align="center">Thank you for contacting Me.</p>';
$replymessage = "Hi $name
Thank you for your email.
We will endeavour to reply to you shortly.
Please DO NOT reply to this email.
Below is a copy of the message you submitted:
--------------------------------------------------
Subject: $thesubject
Query:
$themessage
--------------------------------------------------
Thank you";
$themessage = "name: $name \nQuery: $themessage";
mail("$replyemail",
"$thesubject",
"$themessage",
"From: $email\nReply-To: $email");
mail("$email",
"Receipt: $thesubject",
"$replymessage",
"From: $replyemail\nReply-To: $replyemail");
echo $success_sent_msg;
echo '<script>setTimeout(function(){location.href="index.php"} , 5000); </script>';
?>
I am unable to figure out what wrong I've done.
whenever i fill out information in for a Success Message displayed. but i didn't get any email of that information.
can someone fix this existing code or provide me a new mailer code?
Your form
<form method="post" action="1.php" id="contactfrm">
<div class="col-sm-4">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Enter name" title="Please enter your name (at least 2 characters)">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="comments">Message</label>
<textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
</div>
<button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Submit</button>
<div class="result"></div>
</div>
</form>
Your php code with smtp
<?php
if(isset($_POST["submit"])){
$replyemail="my email";
$name = $_POST["name"];
$email = $_POST["email"];
$thesubject = "Project With Me Query";
$themessage = $_POST["message"];
$success_sent_msg='<p align="center"><strong> </strong></p>
<p align="center"><strong>Your message has been successfully sent to My Email<br>
</strong> and I will reply as soon as possible.</p>
<p align="center">A copy of your query has been sent to you.</p>
<p align="center">Thank you for contacting Me.</p>';
$replymessage = "Hi $name
Thank you for your email.
We will endeavour to reply to you shortly.
Please DO NOT reply to this email.
Below is a copy of the message you submitted:
--------------------------------------------------
Subject: $thesubject
Query:
$themessage
--------------------------------------------------
Thank you";
$themessage = "name: $name \nQuery: $themessage";
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "yourusername#gmail.com";
$mail->Password = "yourgmailpassword";
$mail->AddReplyTo($replymessage, "Reply name");
$mail->AddAddress($email,'ashu');
$mail->Subject = "SMTP Receivced";
$mail->Body = "<b>Succesfully SMTP Receivced</b>";
$mail->MsgHTML($success_sent_msg);
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'index.php';
$crlf = "\n";
$hdrs = array(
'From' => 'you#yourdomain.com',
'Subject' => 'Test mime message'
);
if($mail->send($hdrs))
{
echo "<script> alert('Successfully Mailed');window.location = '';</script>";
}
else{
echo "Mailed Error: " . $mail->ErrorInfo;
}
}
//echo '<script>setTimeout(function(){location.href="pra-2.php"} , 5000); </script>';
?>