Simple PHP Contact Form: Submit Button Not Sending Emails - php

I am using a contact form on a site and it is not sending the email, please can anyone give me advise where this code is wrong?
This form was copied from another site that worked, so I cannot understand where it fails. I've checked the syntax and everything seems ok.
<?php
/*
Template Name: Contact Page
*/
get_header();
$contact_show = true;
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$contact_show = false;
$emailTo = get_option('of_email_address');
$body = "Name: $name \r\nEmail: $email \r\nSubject: $subject \r\nComments:\r\n $comments";
$headers = 'From: <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<div id="top">
<h1><?php the_title(); ?></h1>
</div>
</div>
<div id="main_content">
<div id="contact">
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile; // end of the loop. ?>
<?php if ($contact_show == true) { ?>
<?php } ?>
<?php if(isset($hasError)) { //If errors are found ?>
<div class="redbox">
<p><?php _e('Please check if you\'ve filled all the fields with valid information. Thank you.') ?></p>
</div>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<div class="greenbox">
<p>
<?php _e('Your message has been sent!') ?><br />
<?php echo $name; _e(' thank you for emailing us! We will get in touch with you soon.'); ?>
</p>
</div>
<?php } ?>
<form action="" method="post" id="contactForm"><!-- start contact form -->
<fieldset class="contact-fieldset">
<ul>
<li>
<label for="contactname"><?php _e('Name:') ?></label>
<input type="text" name="contactname" id="name" class="contact-input requiredContact" />
</li>
<li>
<label for="email"><?php _e('Email:') ?></label>
<input type="text" id="email" name="email" class="contact-input requiredContact cEmail" />
</li>
<li>
<label for="subject"><?php _e('Subject:') ?></label>
<input type="text" name="subject" id="subject" class="contact-input requiredContact" />
</li>
<li>
<label for="message"><?php _e('Message:') ?></label>
<textarea rows="6" style="overflow: hidden;" cols="40" id="message" name="message" class="contact-textarea
requiredContact"></textarea>
</li>
<li>
<input type="submit" value="<?php _e('send message'); ?>" name="submit" class="contact-submit"
/>
</li>
</ul>
</fieldset>
</form></div>
</div>

If you coppied it from another site that did work, that suggests the PHP code works and that it's in fact your server setup that is wrong as that is what you have changed (and not the PHP code, which i assume shows no errors if it was previously working). Ensure these settings are correct:
http://php.net/manual/en/mail.configuration.php

I check your coding, in that there are no errors and the reason for not working is the function calling may be.
In your coding there are several function calls are present. Check that all the functions are present and if any errors in that functions.

Again, PHP looks good; check your php.ini file and ensure that mail functions are configured to either give PHP a path to sendmail or an SMTP connection; also, be sure that your firewall settings are not blocking port 25 (or your configuration's SMTP relay port)

Related

When contact form button is clicked, site redirects to a custom post enrty

I've got this wordpress site with a custom post type, called reviews, loop and a contact form on a page. When I click on the submit button on the contact page, reguardless if the form contents matches the regex, it redirects the page to /reviewa/random name
I've removed the "reviews" part of the website and the form works.
What I would like to happen is to have the form send the email, without redirecting the site to /reviews/randomName
$(function() {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
$('#contact-form').submit(function(e) {
$clientName = $("#contactName").val();
var valid = true;
// Remove any confirmation messages
$('p.confirmation').remove();
// Validate all fields that have a class of .required-field
$('.required-field').each(function() {
// Remove error class from all inputs and textareas and delete error messages before checking form again
$(this).removeClass('error').next('p').remove();
if ($(this).val().trim() === '' && $(this).attr('id') != 'email') {
$(this).addClass('error').parent().append('<p class="error">Please enter a ' + $(this).prev().html().toLowerCase() + '</p>');
valid = valid && false;
} else if ($(this).attr('id') === 'email') {
if (!$(this).val().trim().match(re)) {
$(this).addClass('error').parent().append('<p class="error">Please enter a valid email address</p>');
valid = valid && false;
}
}
});
if (valid) {
var formInput = $(this).serialize();
$.post($(this).attr('action'), formInput, function(data) {
$('#contact-form').before('<p class="confirmation">Thanks ' + $clientName + ', your email was successfully sent. We will be in touch soon.</p>');
_gaq.push(['_trackEvent', 'Contact form', 'Contact form submitted']);
});
}
e.preventDefault();
});
});
<div id="reviews">
<h2>Our reviews</h2>
<div id="slider" class="flex-container">
<div class="flexslider">
<ul class="slides">
<?php $new = new WP_Query('post_type=reviews& posts_per_page=-1');
while ($new->have_posts()) : $new->the_post(); ?>
<li>
<?php echo the_post_thumbnail('portfolio-thumb');?>
<span class="review"> <?php the_content(); ?></span>
<span class="name"> <?php the_title(); ?></span>
</li>
<?php endwhile;?>
</ul>
</div>
</div>
</div>
<div id="contact">
<h2>To contact the instructor, plese enter your details below</h2>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submitted'])){
//Check name
if(trim($_POST['contactName']) === '') {
$nameError = true;
$hasError = true;
}else{
$name = trim($_POST['contactName']);
}
//Check company name
if(trim($_POST['companyName']) === '') {
$companyNameError = true;
$companyNameError = true;
}else{
$companyName = trim($_POST['companyName']);
}
//Check address
if(trim($_POST['address']) === '') {
$addressError = true;
$addressError = true;
}else{
$address = trim($_POST['address']);
}
// Check email address exists and is valid
if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
}else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+.[A-Z]{2,4}$",trim($_POST['email']))){
$emailError = 'You entered an invalid email address.';
$hasError = true;
}else{
$email = trim($_POST['email']);
}
if(trim($_POST['telNumber']) === '') {
$telError = true;
$hasError = true;
}else{
$telephone = trim($_POST['telNumber']);
}
//Check message
if(trim($_POST['message']) === ''){
$messageError = true;
$hasError = true;
}else{
$message = stripslashes(trim($_POST['message']));
}
//If there is no error, send the email
if(!isset($hasError)){
$emailTo = 'randomEmail#mail.com';
$subject = 'Contact from the Contact Us page';
$body = "Name: $name \n Email: $email \n Telephone: $telephone \n Poxtcode: $address . \n Message:\n$message";
$headers = 'From: randomWebsite.com, <'.$emailTo.'>' . "\n" . "\n" .'Reply-To: ' . $email;
ssmtp($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php if($emailSent == true) { ?>
<p class="confirmation">Thanks,
<?php echo $name;?>, your email was successfully sent. We will be in touch soon.</p>
<?php } ?>
<form action="<?php the_permalink(); ?>" id="contact-form" method="post" novalidate>
<ul>
<li>
<input type="text" placeholder="Name (required)" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])){echo $_POST['contactName'];} ?>" class="required-field" />
<?php if($nameError) { ?>
<p class="error">Please enter your name.</p>
<?php } ?>
</li>
<li>
<input type="text" placeholder="Postcode (required)" name="address" id="address" value="<?php if(isset($_POST['address'])){echo $_POST['address'];} ?>" class="required-field" />
<?php if($addressError) { ?>
<p class="error">Please enter your postcode.</p>
<?php } ?>
</li>
<li>
<input type="tel" placeholder="Mobile number (required)" name="telNumber" id="telNumber" value="<?php if(isset($_POST['telNumber'])){echo $_POST['telNumber'];} ?>" />
<?php if($telError) { ?>
<p class="error">Please enter your mobile number.</p>
<?php } ?>
</li>
<li>
<input type="email" placeholder="Email (required)" name="email" id="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>" class="required-field" />
<?php if($emailError) { ?>
<p class="error">
<?php echo $emailError;?>
</p>
<?php } ?>
</li>
<li class="textarea">
<textarea name="message" placeholder="Message (required)" id="message" rows="20" cols="30" class="required-field"><?php if(isset($_POST['message'])){echo stripslashes($_POST['message']);} ?></textarea>
<?php if($messageError) { ?>
<p class="error">Please enter a message.</p>
<?php } ?>
</li>
<li>
<input type="hidden" id="submitted" name="submitted" />
<button class="button" ">Send</button>
</li>
</ul>
</form>
</div>

Custom Page Template is not shown as page section in Wordpress twentyseventeen theme?

I created a page template and put it as the theme for a page. After that i have chosen this page as a page section in the twenty seventeen theme options but the content of this page is not shown just if you access the page directly and not as a page section.
<?php
/*
Template Name: Contact
*/
?>
<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['comments']) === '') {
$commentError = 'Please enter a message.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = '[PHP Snippets] From '.$name;
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?>
<?php get_header(); ?>
<div id="container">
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error">Sorry, an error occured.<p>
<?php } ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ul class="contactform">
<li>
<label for="contactName">Name:</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>
<li>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>
<li><label for="commentsText">Message:</label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if($commentError != '') { ?>
<span class="error"><?=$commentError;?></span>
<?php } ?>
</li>
<li>
<input type="submit">Send email</input>
</li>
</ul>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<?php } ?>
</div><!-- .entry-content -->
</div><!-- .post -->
<?php endwhile; endif; ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I hope you can help me guys. Thanks in advance! :)
Edit:
The website with the custom page template is displayed when you access the page directly like for example http://example.com/testing.
direct link
But if you want to set it as a page section in the twentyseventeen theme under the theme options the space, where the custom template should appear, is blank. This is the case if you access it via http://example.com. The site is a onepager and the testing page with the custom page template is a section of it.
front page
2nd Edit:
I set the custom page as a page section by just setting it in the theme options.
theme options of twentyseventeen theme
You're trying to use wordpress functions out of wordpress. Place this code require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); directly to next line of opening php tag <?php at 8th line of your code.
This may not work if your wordpress installation is on some folder of your main server directory. Then you can try this one:
//place this at the same position, as I mentioned above
$needPath = realpath(__DIR__ . '/../../..');
require_once($needPath . '/wp-load.php');
Used solution from here to go several levels up in the filesystem. Because we need to go to main wordpress installation folder from {wp-main-folder}/wp-content/themes/twentyseventeen/{your-file}, we should to go 3 levels up for reaching wp-load.php file.
EDIT
Also, it will be useful to check, if your page is called from/out of your wordpress installation. So, you can use this solution for it:
//place this to the same place as described above
if(!defined(ABSPATH)) {
$needPath = realpath(__DIR__ . '/../../..');
require_once($needPath . '/wp-load.php');
}
Tested and working
Edit: more detailed
<?php
/*
Template Name: Contact
*/
if(!defined(ABSPATH)) {
$needPath = realpath(__DIR__ . '/../../..');
require_once($needPath . '/wp-load.php');
}
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['comments']) === '') {
$commentError = 'Please enter a message.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = '[PHP Snippets] From '.$name;
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?>
<?php get_header(); ?>
<div id="container">
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error">Sorry, an error occured.<p>
<?php } ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ul class="contactform">
<li>
<label for="contactName">Name:</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>
<li>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>
<li><label for="commentsText">Message:</label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if($commentError != '') { ?>
<span class="error"><?=$commentError;?></span>
<?php } ?>
</li>
<li>
<input type="submit">Send email</input>
</li>
</ul>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<?php } ?>
</div><!-- .entry-content -->
</div><!-- .post -->
<?php endwhile; endif; ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
For calling this template from wordpress. you need to call it like( if your website url is http://example.com ):
Create page from wordpress dashboard and choose from the list template on the right side of create page section instead of Default Template the one you want to use:
After it( dont forget to add page title ) save page and go to the url, provided there. If page don't have parent, or permalinks are as ?p=123 then you should access that page like here: http://example.com/your-page-title.
If you're going to access that template directly, you should write url as http://example.com/wp-content/themes/twentyseventeen/your-template-filename.php. This example provided as your template is directly in your twentyseventeen theme folder.

Form submitting to a different url as opposed to within the same page

I have a form that submits within the same url (eg. form.co.uk is where the form is and upon successfully submitting rather than going to a confirmation page with a different URL it stays on form.co.uk and gives the 'thanks for contacting us' confirmation on the same page.
However I need to change this so upon submitting it goes to a different url (just a thank you page)
How would I go about this?
html
<div class="width-inner">
<?if ($c->submitted && count($c->errors) == 0): ?>
<div class="thankyou-box">
<h2>Thank you for your enquiry</h2>
<h3>We'll get back to you as soon as we can.</h3>
</div>
<script type="text/javascript">
_gaq.push(['_trackPageview', "/thanks"]);
</script>
<? else: ?>
<div class="contact-box left" style="width:650px;">
<form action="form.php" method="post">
<div class="heading">
<h2>Contact Us</h2>
<h3>Get in touch now.</h3>
</div>
<? if(count($c->errors) > 0): ?>
<div class="error">
There was a problem with your enquiry
</div>
<? endif; ?>
<div class="vspace20"></div>
<label>Your Name:</label>
<input type="text" name="name" class="<?= $c- >requestEmpty('name') ? 'invalid' : '' ?>" value="<?= #$_REQUEST['name'] ?>"/>
<div class="required">Required field</div>
<div class="clear"></div>
<label>Email Address:</label>
<input type="email" name="email" class="<?= $c- >requestEmpty('email') ? 'invalid' : '' ?>" value="<?= #$_REQUEST['email'] ?>"/>
<div class="required">Required field</div>
<div class="clear"></div>
<hr/>
<label>Enquiry:</label>
<textarea name="enquiry" class="<?= $c->requestEmpty('enquiry') ? 'invalid' : '' ?>">I would like to contact you.</textarea>
<div class="clear"></div>
<hr/>
<input type="hidden" name="extra" value="<?= #$_REQUEST['extra'] ?>"/>
<div class="notice">Your details will not be used for marketing purposes</div> <br>
<input type="submit" class="btn-submit right" value="Claim Now" name="submit"/>
<div class="clear"></div>
</form>
</div>
</div>
code for the handler
<?php
class Contact {
var $submitted = false;
var $errors = array();
var $template = "Name: %s\r\nEmail Address: %s\r\nEnquiry: %s";
public function __construct() {
if (!empty($_REQUEST['submit'])) {
$this->submitted = true;
} else {
return;
}
$requiredFields = array(
'name', 'email'
);
foreach($requiredFields as $key) {
if ($this->requestEmpty($key)) {
$this->errors[] = $key;
}
}
if (count($this->errors) == 0) {
$this->_sendForm();
}
}
private function _sendForm() {
$body = sprintf($this->template,
$_REQUEST['name'],
$_REQUEST['email'],
$_REQUEST['enquiry'] . "\r\n" . $_REQUEST['extra']);
$headers = 'From: auto#magnetikmedia.co.uk' . "\r\n" .
'Reply-To: auto#magnetikmedia.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail("johnfarrell2012#gmail.com", "New contact form", $body, $headers, "- fauto#magnetikmedia.co.uk");
header("Location: thank_you.php");
exit;
}
public function requestEmpty($key) {
if (empty($_REQUEST['submit'])) {
return false;
}
if (!isset($_REQUEST[$key])) {
return true;
}
if (trim($_REQUEST[$key]) == "") {
return true;
}
return false;
}
}
?>
I guess it must be something to do with the top if else statement ie. I want to say 'if successful and no errors then go to www.whatever.com' but how would I write this?
At the end of your form PHP page, add this, altered for your URL:
$success = "success.html";
header("Location: " . $success);
Maybe you just need to change your HTML,
<form action='page.php' method='post'>
or send a header after your PHP is handled on that same page, like
header('LOCATION:page.php'); die();
. Make sure you use die(); to kill the current page.
After seeing your edit, try this then:
Quick opening note: You could also set header to an external URL such as:
header("Location: http://www.example.com/");
and the http:// must be included.
Redirection after mail examples:
mail("johnfarrell2012#gmail.com", "New contact form", $body, $headers, "- fauto#magnetikmedia.co.uk");
header("Location: thank_you.php");
exit;
}
And to check if mail was sent successfully:
(We simply add an if/else condition.)
if(mail("johnfarrell2012#gmail.com", "New contact form", $body, $headers, "- fauto#magnetikmedia.co.uk"));
header("Location: thank_you.php");
exit;
}
else {
echo "Mail not sent";
exit;
}
More on the header function can be viewed on the PHP.net Website.
http://php.net/manual/en/function.header.php

Strange letters in mail sent from mail-form in Wordpress

I need to change the charset of the mail form used on this side: http://www.erik-dalsgaard.dk/kontakt/
I need to include these letters: Æ, æ, Ø, ø, Å, å
Right now the letters it output when it sends a mail is Æ, Ø,à instead of the letters above.
The php for the mailform is:
<?php
/*
Template Name: Contact
*/
get_header(); ?>
<?php
//If the form is submitted
if(isset($_POST['submitted'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactName']) === '') {
$nameError = 'You forgot to enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) === '') {
$emailError = 'You forgot to enter your email address.';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter your comments.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = get_option_tree('pr_contact_email');
$subject = 'Henvendelse fra hjemmeside fra '.$name;
$msubject = trim($_POST['subject']);
$body = "Navn: $name \n\nE-Mail: $email \n\nEmne: $msubject \n\nBesked: $comments";
$headers = 'From: Besked fra hjemmeside <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="inner custom_content">
<div class="content <?php global_template(content); ?>">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if(the_content()){ ?>
<div class="divider"></div>
<?php } ?>
<?php endwhile; endif; ?>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="form-success">
<?php echo get_option_tree('pr_form_success'); ?>
</div>
<?php } else { ?>
<div class="form-success">
<?php echo get_option_tree('pr_form_success'); ?>
</div>
<form action="<?php the_permalink(); ?>" id="contactForm" class="big_form" method="post" accept-charset="UTF-8">
<ul class="forms">
<li>
<label for="contactName">Navn: *</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField <?php if($nameError != '') { ?>hightlight<?php } ?>" />
</li>
<li><label for="email"><?php tr_translate(email); ?>: *</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email <?php if($emailError != '') { ?>hightlight<?php } ?>" />
</li>
<li><label for="subject">Emne:</label>
<input type="text" name="subject" id="subject" value="<?php if(isset($_POST['subject'])) echo $_POST['subject'];?>" />
</li>
<li class="textarea"><label for="commentsText">Besked: *</label>
<textarea name="comments" id="commentsText" rows="8" cols="60" class="requiredField <?php if($commentError != '') { ?>hightlight<?php } ?>"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
</li>
<li class="buttons">
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit" class="button light"><?php tr_translate(submit_contact); ?></button>
<div class="loading"></div>
</li>
</ul>
</form>
</div><!-- .content End -->
<!-- Content End -->
<?php } ?>
<?php global_template(sidebar); ?>
<?php get_footer(); ?>
There are more places where this issue might come but fist verify if:1. MySQL 5 doesn't support full UTF-8 characters2. The e-mail client/host doesn't support full UTF-8 charactersIt is easy to verify this, check your website/your database if you detect this issues there also then you might try to use this plugin or search for similar ones.The bad part is that the issue might come from the e-mail client (ex: thunderbird or outlook) or even from the e-mail host.
I encontered an issue with some language specific characters that showed just fine in both yahoo and gmail webmail but not in any roundcube hosts. I ended up replaceing my characters with "normal" ones.(I haven't tryed the ubove plugin).Check the plugin it says it recodes the characters so it should do the trick.Regards.

PHP mail issues using javascript validation

I am trying to send an email from the same PHP page after Javascript validation but have some issues sending email. Can someone help me out with this?
Here is the JavaScript code:
<!-- -->
<script type="text/javascript">
function checkCheckBoxes() {
if (document.form.inforequired.checked == true)
{
if (document.form.option1.checked == false && document.form.option2.checked == false && document.form.option3.checked == false && document.form.option4.checked == false && document.form.option5.checked == false && document.form.option6.checked == false && document.form.option7.checked == false && document.form.option8.checked == false && document.form.option9.checked == false && document.form.option10.checked == false)
{
document.getElementById('error').innerHTML = "Please choose your options.";
return false;
}
if (document.form.option8.checked == true || document.form.option9.checked == true || document.form.option10.checked == true)
{
if (document.form.address.value == "" )
{
document.getElementById('erroraddress').innerHTML = "Please enter your address.";
return false;
}
}
else
{
document.forms["form"].submit();
return true;
}
}
else
{
document.forms["form"].submit();
return true;
}
}
</script>
<!-- Email -->
Here is the PHP code to send email in the else part.
<div id="content">
<h1 id="formSpacingHeading"> Visitor Information </h1>
<?if( !isset($_POST['submit'])):?>
<form id="form" name="form" onsubmit="return checkCheckBoxes();" action="" method="POST">
<p id="formSpacing"><label for="username" class="iconic user" > Name <span class="required">*</span></label> <input type="text" name="username" id="username" required="required" placeholder="Enter your name" /></p>
<p id="formSpacing"><label for="usermail" class="iconic mail-alt"> E-mail address <span class="required">*</span></label> <input type="email" name="usermail" id="usermail" placeholder="Enter your E-mail ID" required="required" /> </p>
<p id="formSpacing"><label for="contactno" class="iconic link"> Contact number <span class="required">*</span></label> <input type="text" pattern="^\d\d\d\d\d\d\d\d\d\d$" name="contact" id="contactno" placeholder="Enter your contact number" required="required" /></p>
<input type="submit" name="submit" value="Submit Form" />
</form>
<?php else :
$name=$_POST['username'];
$mail=$_POST['usermail'];
$contact=$_POST['contact'];
$to = "admin#xxx.com";
$subject = "New Visitor Information";
$message = $name;
$from = $mail;
$headers = "From:" . $from;
$email = mail($to, $subject, $message, $headers);
if($email){
echo "Thank you. We will keep you updated with the latest information.";
}
else{
echo "Error processing your request. Please try again later.";
}
?>
<? endif ?>
I'm not a PHP guru, but I have programmed a few basic things in it.
I think you have your if statement all wrong.
Change..
<?if( !isset($_POST['submit'])):?>
to
<?if( !isset($_POST['submit'])){?>
Change
<?php else :
To
<?php } else {
Change
?>
<? endif ?>
to
}?>

Categories