Getting no errors in error_log or on page. Error log definitely working as if I change
require 'PHPMailer.php'; to require 'PHPFailer.php'; I get PHP Fatal error: require(): Failed
display_errors set to true
error_reporting set to E_ALL & ~E_NOTICE
PHPMailer 6.1.7 - PHP 7.2
Both PHPMailer.php and Exception.php are in the root directory. Have commented out the redirects as I just get sent to /php/form_error.php.
Any ideas would be much appreciated, thanks.
contact_post.php
<?php
/*
THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer.php';
require 'Exception.php';
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$fromEmail = 'info#domain.co.uk';
$fromName = 'Info domain';
// an email address that will receive the email with the output of the form
$sendToEmail = 'info#domain.co.uk';
$sendToName = 'Info domain';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', '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);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
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>";
$mail = new PHPMailer(true);
$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 ($responseArray['type'] == 'success') {
// success redirect
//header('Location: /php/form_complete.php');
}
else {
//error redirect
//header('Location: /php/form_error.php');
}
form on contact.html
<form id="contact-form" method="post" action="/contact_post.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">First name*</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your first name *" required="required" data-error="First name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Last name*</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your last name *" required="required" data-error="Last name 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_need">Please specify your need*</label>
<select id="form_need" name="need" class="form-control" required="required" data-error="Please specify your need.">
<option value=""></option>
<option value="Request quotation">Request quotation</option>
<option value="Request order status">Request order status</option>
<option value="Request copy of an invoice">Request copy of an invoice</option>
<option value="Other">Other</option>
</select>
<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">Message*</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Please enter message here*" rows="4" required="required" data-error="Please, leave us a message."></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="Send message">
</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>
htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Options -Indexes
Further to my comment, you're not doing anything when something goes wrong, so there's nothing to see.
To see what's actually going wrong, output something useful when it does, for example:
catch (\Exception $e)
{
$responseArray = ['type' => 'danger', 'message' => $e->getMessage()];
var_dump($responseArray, $mail->ErrorInfo);
}
One small thing:
if(!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
There's no need to do this – PHPMailer is already set to throw exceptions, so this should just be:
$mail->send();
Related
I use this contact form by Boostrapious and works well, but I can't find solution to add some checkbox inputs and send values by email. I added HTML inputs, but I need help with php code. Can anyone help me?
This is a link to this tutorial and code:
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
<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">Firstname *</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-6">
<div class="form-group">
<label for="form_lastname">Lastname *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname 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 checkbox">
<label for="checkboxoptions1">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions1" value="checkboxoptions1">
Option1
</label>
<label for="checkboxoptions2">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions2" value="checkboxoptions2">
Option2
</label>
<label for="checkboxoptions3">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions3" value="checkboxoptions3">
Option3
</label>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></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="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required. Contact form template by
Bootstrapious.</p>
</div>
</div>
</div>
</form>
and php code here
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'Demo contact form <demo#domain.com>';
// an email address that will receive the email with the output of the form
$sendTo = 'Demo contact form <demo#domain.com>';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('checkboxoptions' => 'My options','name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', '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');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the necessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $_POST['email'],
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// 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'];
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!
I'm trying to do contact form so my clients can send me a message, but when I press send, everything ok, it says contact form sent, but I don't receive anything in my mail. I use gmail, I can use own domain mail too. Can anyone help me fix it? Thanks.
I have server running with linux ubuntu 18.04, with apache web server.
This is the contact.php file. I see everything ok.
Contact.php:
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'Contact form';
// an email address that will receive the email with the output of the form
$sendTo = 'mymail#gmail.com';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', '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');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// 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'];
}
This code is in index.html, one section.
Contact.html:
<section class="contact-1">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-6 col-md-12">
<img class="img-center" src="images/banner/06.png" alt="">
</div>
<div class="col-lg-6 col-md-12 md-mt-5">
<div class="section-title">
<div class="title-effect title-effect-2">
<div class="ellipse"></div> <i class="la la-info"></i>
</div>
<h2>Свяжитесь с нами</h2>
<p>Свяжитесь с нами и расскажите, чем мы можем вам помочь. Заполните все поля, и мы скоро свяжемся с вами.</p>
</div>
<form id="contact-form" method="post" action="php/contact.php">
<div class="messages"></div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Имя" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Фамилия" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Электронная почта" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Телефон" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Сообщение" rows="4" required="required" data-error="Заполните это поле"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12 mt-2">
<button class="btn btn-theme btn-circle" data-text="Send"><span>О</span><span>т</span><span>п</span><span>р</span><span>а</span><span>в</span><span>и</span><span>т</span><span>ь</span>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
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 this problem. I made a bootstrap form with a php included and instead of displaying a message if the form is filled and sent successfuly i need it to redirect to a different page (.html).
This is the PHP page for the form:
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'email#email.com';
// an email address that will receive the email with the output of the form
$sendTo = 'email#mail.cz';
// subject of the email
$subject = 'Nová zpráva z Vašeho webu';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' =>
'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Děkuji! Vaše zpráva byla úspěšně odeslána. ';
// If something goes wrong, we will display this message.
$errorMessage = 'Omlouvám se, ale došlo k nečekaně chybě. Zkuste to prosím
později.';
/*
* 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');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// 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'];
}
And here is the HTML for the form:
<form id="contact-form" method="post" action="contact.php">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Jméno *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Vaše křestní jméno *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Příjmení *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Vaše příjmení *" required="required" data-error="Lastname 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="Váš platný 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">Telefon</label>
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Váš telefon">
<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">Vaše zpráva *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Vaše zpráva *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
<br>
</div>
<div class="col-md-12">
<input type="submit" class="odeslat" value="Odeslat">
</div>
</div>
<div class="row">
<div class="col-md-12">
<br><p class="text-muted"><strong>*</strong> Tyto pole jsou povinná.</p>
</div>
</div>
</div>
</form>
can u pls help me fix it so it doesn't display only the message?
Thank you
You can write in the end a if sentence that shows this echo in the true case and doesn't show in a false case, some like that ->
if(!$ejecutar)
{
echo "whatever you want";
}
else
{
echo "Whatever you want<br><a href='index.php'>Volver</a>";
}