I am having difficulty with my php code. From my tired eyes, the code is correct, and I've had multiple others look at the code. No one can figure out why it's not working. It must be something quite simple, but I cannot get the contact form to send.
PHP script:
<?php
$from = 'email#example.com';
$sendTo = 'email#example.com';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$htmlHeader = '';
$htmlFooter = '';
$okMessage = 'Contact form succesfully submitted. Thank you, We will get back to you soon!';
$htmlContent = '<h1>New message from contact form</h1>';
use Nette\Mail\Message,
Nette\Mail\SendmailMailer;
require 'php/Nette/nette.phar';
$configurator = new Nette\Configurator;
$configurator->setTempDirectory(__DIR__ . '/php/temp');
$container = $configurator->createContainer();
$httpRequest = $container->getService('httpRequest');
$httpResponse = $container->getService('httpResponse');
$post = $httpRequest->getPost();
if ($httpRequest->isAjax()) {
$htmlContent .= '<table>';
foreach ($post as $key => $value) {
if (isset($fields[$key])) {
$htmlContent .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$htmlContent .= '</table>';
$htmlBody = $htmlHeader . $htmlContent . $htmlFooter;
$mail = new Message;
$mail->setFrom($from)
->addTo($sendTo)
->setSubject($subject)
->setHtmlBody($htmlBody, FALSE);
$mailer = new SendmailMailer;
$mailer->send($mail);
$responseArray = array('type' => 'success', 'message' => $okMessage);
$httpResponse->setCode(200);
$response = new \Nette\Application\Responses\JsonResponse($responseArray);
$response->send($httpRequest, $httpResponse);
}
Contact Form HTML:
<div class="section contact soepa" id="contact" data-animate="bounceIn">
<div class="container">
<div class="col-md-12">
<h2 class="title"><span style="color: #f46b01;">Connect With Us</span></h2>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form id="contact-form" method="post" action="contact.php">
<div class="messages">
</div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" class="form-control" placeholder="Your firstname *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="surname" class="form-control" placeholder="Your lastname *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="email" class="form-control" placeholder="Your email *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="phone" class="form-control" placeholder="Your phone *" required="required">
</div>
<div class="col-md-12">
<textarea name="message" class="form-control" placeholder="Message *" rows="4" required="required"></textarea>
</div>
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary btn-lg" value="Send message">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
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'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>
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>";
}
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
I am doing this first time. I want to ask, can I subscribe using MailChimp through my local server because I am having problem in doing that every time. When I subscribe I do not receive any mail. Here is the code
<?php
// change this path if the class file isn't in the same directory!
include_once 'MailChimp.php';
$alertclass = 'alert-warning';
$msg = '';
$name = '';
$email = '';
if (isset($POST['Submit'])) {
if (empty($_POST['name']) || empty($_POST['email'])) {
$msg = 'Please enter a name and email address.';
} else {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
/*
* Place here your validation and other code you're using to process your contact form.
*/
$mc = new \Drewm\MailChimp('xxxxxxxxx-us11');
$mvars = array('optin_ip'=> $_SERVER['REMOTE_ADDR'], 'FNAME' => $name);
$result = $mc->call('lists/subscribe', array(
'id' => 'f660c6ba5f',
'email' => array('email'=>$email),
'merge_vars' => $mvars,
'double_optin' => true,
'update_existing' => false,
'replace_interests' => false,
'send_welcome' => false
)
);
if (!empty($result['euid'])) {
$msg = 'Thanks, please check your mailbox and confirm the subscription.';
$alertclass = 'alert-success';
} else {
if (isset($result['status'])) {
switch ($result['code']) {
case 214:
$msg = 'You\'re already a member of this list.';
break;
// check the MailChimp API if you like to add more options
default:
$msg = 'An unknown error occurred.';
$alertclass = 'alert-error';
break;
}
}$msg="asdasdad";
}
}
}
?>
I am very new to this.
html code
<form class="form-horizontal"action="contactform-mailchimp.php" method="post">
<?php
//if ($msg != '')
echo '
<div class="alert '.$alertclass.'" role="alert">'.$msg.'</div>';
?>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">First name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" id="inputName">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="email" id="inputEmail">
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" name="message" id="inputMessage"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" name="newsletter"> Subscribe to newsletter
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="Submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>