I have a php form with <meta charset="utf-8" /> (I also tried with <meta charset="ISO-8859-1" /> which give me the same result)
When I test my form with the characters " and ', the email I receive transform those characters into their ASCII characters :
Subject: \"\'
Message: \"\'
Sent by: \"\'
(Somehow, the subject field only adds a \ before the characters)
I have also noticed that anything in between < and > are stripped out in $formAuthor and $formContent but not in $formSubject
Also if the from is submitted with a error in the captcha, the form keep in memory what was written in the field but anything after " in the $formAuthor and $formSubject is stripped out but not in $formContent (it's weird because it does follow the same logic than the one of the email issue)
It's not a server issue because I only have this problem with this form, not with the others once I have.
Thanks a lot for your help !
here is the some part of the php form :
// if all the fields have been entered correctly and there are no recaptcha errors build an email message
if (($resp->is_valid) && (!isset($hasError))) {
$emailTo = 'contact#website.com'; // here you must enter the email address you want the email sent to
$subject = 'Message from: ' . $formAuthor . ' | ' . $formSubject; // This is how the subject of the email will look like
$body = "Email: $formEmail \n\nSubject: $formSubject \n\nMessage: $formContent \n\nSent by: $formAuthor";// This is the body of the email
$headers = 'From: <'.$formEmail.'>' . "\r\n" . 'Reply-To: ' . $formEmail . "\r\n" . 'Return-Path: ' . $formEmail; // Email headers
//send email
mail($emailTo, $subject, $body, $headers);
// set a variable that confirms that an email has been sent
$emailSent = true;
}
$emailSent = true;
}
// if there are errors in captcha fields set an error variable
if (!($resp->is_valid)){
$captchaErrorMsg = true;
}
}
} ?>
here is the HTML :
<div id="singleParagraphInputs">
<div>
<label for="formAuthor">Name</label>
<input class="requiredField <?php if($authorError) { echo 'formError'; } ?>" type="text" name="formAuthor" id="formAuthor" value="<?php if(isset($_POST['formAuthor'])) echo $_POST['formAuthor'];?>" size="40" />
</div>
<div>
<label for="formEmail">Email</label>
<input class="requiredField <?php if($emailError) { echo 'formError'; } ?>" type="text" name="formEmail" id="formEmail" value="<?php if(isset($_POST['formEmail'])) echo $_POST['formEmail'];?>" size="40" />
</div>
<div>
<label for="formSubject">Subject</label>
<input type="text" name="formSubject" id="formSubject" value="<?php if(isset($_POST['formSubject'])) echo $_POST['formSubject'];?>" size="40" />
</div>
<div id="commentTxt">
<label for="formContent">Message</label>
<textarea class="requiredField <?php if($commentError) { echo 'formError'; } ?>" id="formContent" name="formContent" cols="40" rows="5"><?php if(isset($_POST['formContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['formContent']); } else { echo $_POST['formContent']; } } ?></textarea>
</div>
Try this
$emailTo = 'contact#website.com';
$subject = "Message from: " . ' <'. $formEmail .'> ' . "|" . ' <'. $formSubject .'> ';
$body = "Email: " . ' <'. $formEmail .'> ' . "\n\n Subject: " . ' <'. $formSubject .'> ' . "\n\n Message: " . ' <'. $formContent .'> ' . "\n\n Sent by: " . ' <'. $formAuthor .'> ';
// Body alternative $body = "Email: " . ' <'. $formEmail .'> ' . "\n\n" . " Subject: " . ' <'. $formSubject .'> ' . "\n\n" . "Message: " . ' <'. $formContent .'> ' . "\n\n" . "Sent by: " . ' <'. $formAuthor .'> ';
$headers = "From: " . ' <'. $formEmail .'> ' . "\r\n";
$headers .= "Reply-To: " . ' <'. $formEmail .'> ' . "\r\n";
$headers .= "Return-Path: " . ' <'. $formEmail .'> ' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$mail = mail($emailTo, $subject, $body, $headers);
if($mail) {
// If success
}
else {
// If fail
}
Related
I have made a contact form with the following fields: Name, Email, Message. It all worked fine - messages were sent to my email - until I added the attachments option to the form.
I've tried validating the attachment fields by searching up tutorials, but nothing seems to work. I guess I'm just not sure how to implement it to my already existing code.. Any help here?
Here is the form:
<?php include 'contact-form.php'; ?>
<form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<h3>Contact Us</h3>
<fieldset>
<input placeholder="Nimi" type="text" tabindex="1" name="thename" value="<?= $thename ?>" autofocus>
<div class="error"><span><?= $name_error ?></span></div>
</fieldset>
<fieldset>
<input placeholder="Email" type="text" tabindex="2" name="email" value="<?= $email ?>">
<div class="error"><span><?= $email_error ?></span></div>
</fieldset>
<fieldset>
<textarea placeholder="Sisesta sõnum siia.." type="text" tabindex="3" name="message"></textarea>
<div class="error"><span><?= $message_error ?></span></div>
</fieldset>
<fieldset>
<label for="attachment1">File:</label> <input type="file" id="attachment1" name="attachment[]" size="35">
<label for="attachment2">File:</label> <input type="file" id="attachment2" name="attachment[]" size="35">
<div class="error"><span><?= $attachment_error ?></span></div>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Saatmine">Saada</button>
</fieldset>
<div class="success"><?= $success; ?></div>
<div class="error"><?= $error; ?></div>
</form>
Here is PHP validation code contact-form.php:
<?php
$name_error = $email_error = $message_error = $attachment_error = "";
$thename = $email = $message = $success = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["thename"])) {
$name_error = "Palun sisesta nimi";
} else {
$thename = test_input($_POST["thename"]);
// check if name only contains letters, whitespace and hyphen
if (!preg_match("/^[a-zA-Z -]*$/",$thename)) {
$name_error = "Sisestada saab ainult tähti, tühikuid ja sidekriipse";
}
}
if (empty($_POST["email"])) {
$email_error = "Palun sisesta email";
} else {
$email = test_input($_POST["email"]);
// email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Sisesta email korrektselt";
}
}
if (empty($_POST["message"])) {
$message_error = "Palun sisesta sõnum";
} else {
$message = test_input($_POST["message"]);
}
if (empty($_FILES["attachment"])) {
$attachment_error = "Palun sisesta enda eluloo fail";
}
if ($name_error == '' and $email_error == '' and $message_error == '' ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
$to = 'myemail#gmail.com';
$subject = 'Eesti Elulood';
$message = "Sulle saadeti kiri Rannu koguduse kodulehelt.\n\nSaatja nimi: $thename\n\nSaatja email: $email\n\nSõnum: $message";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (isset($_FILES['attachment']['name'])) {
$semi_rand = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . '=?UTF-8?B?' . base64_encode($thename) . '?=' . "
<$email>" . PHP_EOL;
$headers .= "Reply-To: " . '=?UTF-8?B?' . base64_encode($thename) . '?=' .
" <$email>" . PHP_EOL;
$headers .= "Return-Path: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed;" . PHP_EOL;
$headers .= " Boundary=\"{$mime_boundary}\"";
$datamsg = "This is a multi-part message in MIME format." . PHP_EOL .
PHP_EOL;
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: text/plain; Charset=\"UTF-8\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$datamsg .= $message . PHP_EOL . PHP_EOL;
for ($index = 0; $index < count($_FILES['attachment']['name']); $index++)
{
if ($_FILES['attachment']['name'][$index] != "") {
$file_name = $_FILES['attachment']['name'][$index];
$data_file =
chunk_split(base64_encode(file_get_contents($_FILES['attachment']
['tmp_name'][$index])));
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: application/octet-stream; Name=\"
{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Disposition: attachment; Filename=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: base64" . PHP_EOL . PHP_EOL .
$data_file . PHP_EOL . PHP_EOL;
}
}
$datamsg .= "--{$mime_boundary}--";
}
if (#mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=',
$datamsg, $headers, "-f$email")){
$success = "Thankyou, message sent!.";
} else {
$error = "Sorry but the email could not be sent. Please try again!";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
After hitting the submit button, It just takes me to the index.php page..
Any help is appreciated!
1) You have no mail attachments code into your php code except for the html markup, so you cannot send your mail attachments.
2) You have to encode the attachments using chunk_split(base64_encode()) and then you have to import them into your message part using the correct way.
3) You forgot to enter the correct headers, that's the other reason why you can't send your mails.
4) You have to consider that if you use GMail there may be a limit to the type of file you can send and so read this: https://support.google.com/mail/answer/6590?hl=en
5) I suggest you to use the long php tag instead the short tag:
Instead of writing <?= $_SERVER['PHP_SELF']; ?>, write <?php echo $_SERVER['PHP_SELF']; ?>
6) You have a serious error into your php and this is the reason why pressing submit you are in the home instead of your contact form:
<?= $SERVER['PHP_SELF']; ?> is wrong!
<?= $_SERVER['PHP_SELF']; ?> is correct!
See you point 5)
Here is an example of correct html markup for attachments:
<label for="attachment1">File:</label> <input type="file" id="attachment1" name="attachment[]" size="35">
<label for="attachment2">File:</label> <input type="file" id="attachment2" name="attachment[]" size="35">
<label for="attachment3">File:</label> <input type="file" id="attachment3" name="attachment[]" size="35">
Here is an example of correct php mail code for attachments:
if (isset($_FILES['attachment']['name'])) {
$semi_rand = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . '=?UTF-8?B?' . base64_encode($sender_name) . '?=' . " <$from_email>" . PHP_EOL;
$headers .= "Reply-To: " . '=?UTF-8?B?' . base64_encode($sender_name) . '?=' . " <$from_email>" . PHP_EOL;
$headers .= "Return-Path: $from_email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed;" . PHP_EOL;
$headers .= " Boundary=\"{$mime_boundary}\"";
$datamsg = "This is a multi-part message in MIME format." . PHP_EOL . PHP_EOL;
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: text/plain; Charset=\"UTF-8\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$datamsg .= $message . PHP_EOL . PHP_EOL;
for ($index = 0; $index < count($_FILES['attachment']['name']); $index++) {
if ($_FILES['attachment']['name'][$index] != "") {
$file_name = $_FILES['attachment']['name'][$index];
$data_file = chunk_split(base64_encode(file_get_contents($_FILES['attachment']['tmp_name'][$index])));
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: application/octet-stream; Name=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Disposition: attachment; Filename=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: base64" . PHP_EOL . PHP_EOL . $data_file . PHP_EOL . PHP_EOL;
}
}
$datamsg .= "--{$mime_boundary}--";
}
if (#mail($recipient_email, '=?UTF-8?B?' . base64_encode($subject) . '?=', $datamsg, $headers, "-f$from_email")) {
exit("Files Sent Successfully");
} else {
exit("Sorry but the email could not be sent. Please go back and try again!");
}
Where $sender_name is the name of sender, $from_email is the email of sender, $recipient_email is the recipient of your email.
You can take an example from my code and integrate it into your project, I wrote only the essential parts concerning the sending of attachments.
I hope this helps.
I have my form setup and working perfect, however I need to add attachment functionality (only) to this. I've reviewed several threads and tried implementing but with no luck.
I'm trying to add id='file-7'
HTML code:
<fieldset class="clean">
<textarea name="comments" id="comments" placeholder="Message*"></textarea>
</fieldset>
<fieldset class="percent-one-full">
<div class="box">
<input type="file" name="file-7[]" id="file-7" class="inputfile inputfile-6" data-multiple-caption="{count} files selected" multiple />
<label for="file-7"><span></span> <strong><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg> Upload file…</strong></label>
<input name="send" type="submit" class="button red" id="submit" onClick="MM_validateForm('name','','R','bandname','','R', 'email','','RisEmail','comments','','R');return document.MM_returnValue" value="SEND"/>
JS code:
jQuery(document).ready(function(){
$('#cform').submit(function(){
var action = $(this).attr('action');
$("#message").slideUp(750,function() {
$('#message').hide();
$('#submit')
.after('<img src="images/nivo-preloader.gif" class="contact-loader" />')
.attr('disabled','disabled');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
bandname: $('#bandname').val(),
osflow: $('#osflow').val(),
hiddenDiv: $('#hiddenDiv').val(),
hiddenDivv: $('#hiddenDivv').val(),
hiddenDivvv: $('#hiddenDivvv').val(),
hiddenDivvvv: $('#hiddenDivvvv').val(),
hiddenDivvvvv: $('#hiddenDivvvvv').val(),
comments: $('#comments').val(),
verify: $('#verify').val()
},
function(data){
document.getElementById('message').innerHTML = data;
$('#message').slideDown('slow');
$('#cform img.contact-loader').fadeOut('slow',function(){$(this).remove()});
$('#submit').removeAttr('disabled');
if(data.match('success') != null) $('#cform').slideUp('slow');
}
);
});
return false;
});
});
PHP code:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
print_r($_POST);
$name = $_POST['name'];
$email = $_POST['email'];
$_POST['bandname'];
$_POST['osflow'];
$_POST['hiddenDiv'];
$_POST['hiddenDivv'];
$_POST['hiddenDivvv'];
$_POST['hiddenDivvvv'];
$_POST['hiddenDivvvvv'];
$comments = $_POST['comments'];
//$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Please enter your message.</div>';
exit();
//} else if(!isset($verify) || trim($verify) == '') {
// echo '<div class="error_message">Please enter the verification number. </div>';
// exit();
//} else if(trim($verify) != '4') {
// echo '<div class="error_message">The verification number you entered is incorrect.</div>';
// exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
//$address = "example#example.net";
$address = "info#example.com";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Decal Inquiry from ' . $name . '';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name. The additional message is as follows." . PHP_EOL . PHP_EOL;
$e_body = "Name: $name" . PHP_EOL . "\r\n";
$e_body2 = "Email: $email" . PHP_EOL . "\r\n";
$e_custom = "Band Name: $bandname" . PHP_EOL . "\r\n";
$e_custom2 = "Interest In: $osflow" . PHP_EOL . "\r\n";
$e_custom3 = "Drum Size: $hiddenDiv" . PHP_EOL . "\r\n";
$e_custom3 = "Drum Head Color: $hiddenDivv" . PHP_EOL . "\r\n";
$e_custom4 = "Mic Port Position: $hiddenDivvv" . PHP_EOL . "\r\n";
$e_custom5 = "Banner Color: $hiddenDivvvv" . PHP_EOL . "\r\n";
$e_custom6 = "Banner Size: $hiddenDivvvvv" . PHP_EOL . "\r\n";
$e_content .="Content-Disposition: attachment; filename=$file-7" . "\r\n";
$e_content = "Comments: $comments" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email at $email.";
$msg = wordwrap( $e_body . $e_body2 . $e_custom . $e_custom2 . $e_custom3 . $e_custom4 . $e_custom5 . $e_custom6 . $e_custom7 . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$name</strong>, we've received your inquiry and will be in touch shortly.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
Any help would be much appreciated!
I would like to know why can't I receive the check-box value in my PHP email sender?
Here is the html:
<form method="post" action="send.php" name="cform" id="cform" class="form-signin" role="form">
<input name="name" id="name" type="text" class="form-control" placeholder="Your name and surname..." >
<input name="email" id="email" type="email" class="form-control" placeholder="Your email address..." >
<textarea name="comments" id="comments" class="form-control" placeholder="Your request..."></textarea>
<p>
<input type="checkbox" name="newsletter" id="newsletter" value="Yes" checked>
<span>I would like to receive the newsletter</span>
</p>
<input type="submit" id="submit" name="submit" class="lightButton" value="Submit >">
<div id="simple-msg"></div>
</form>
And here's the relevant part of the PHP code I found on the Internet ( don't know if here or in a blog, I'm kind of new to PHP so I can't do it from scratch ) and modified to my needs.
<?php
if(!$_POST) exit;
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$x_newsletter = $_POST['newsletter'];
$address = "xxx#gmail.com";
$e_subject = 'Richiesta contatto da ' . $name . '.';
$e_body = "Richiesta contatto da parte di $name:" . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_checkNl = "Inviare newsletter: $x_newsletter" . PHP_EOL . PHP_EOL;
$e_reply = "$name\n$email";
$msg = wordwrap( $e_body . $e_content . $e_checkNl . $x_newsletter . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
?>
I put the $x_newsletter variable to catch the check-box "Yes" value (or at least something like 0 or 1), and later in the code a $e_checkNl that would write the value in the mail.
Fact is that it is a week that I'm searching in stack-overflow for and answer and troubleshooting this PHP trying to figure this thing out but every email I receive from the PHP sender lacks of this important information.
I don't really know, it could be a typo, or some sort of syntax that I can't manage as a newbie, but it's driving me crazy.
Can you please help me?
I'm here should you need any other kind of information.
Thank you,
Nico
EDIT:
here is the full PHP page. Every "if" i put I always get a "NO" option that is totally weird...
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
print_r($_POST);
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$x_newsletter = $_POST['newsletter'];
if(trim($name) == '') {
echo '<div class="error_message">Your name is required</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Insert valid email address</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Invalid email address. Please retry. </div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Insert your messagge.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "xxx#gmail.com";
$e_subject = 'Richiesta contatto da ' . $name . '.';
$e_body = "Richiesta contatto da parte di $name:" . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
//checkbox
$e_checkNl = "Inviare newsletter: $x_newsletter" . PHP_EOL . PHP_EOL;
$e_reply = "$name\n$email";
$msg = wordwrap( $e_body . $e_content . $e_checkNl . $x_newsletter . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h3>Request sent successfully</h3>";
echo "<p>Dear <strong>$name</strong>, Your request has been received, we will contact you as soon as possible.</p><p>Grazie,<br/>Top Italy Travel.</p>";
echo "<p>$e_checkNl</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERRORE!';
}
But the last part makes the form show a div on page for successful or error request, while the first part is a simple form check.
I doubt those cause this issue.
This will give you your desired result:
Replace $x_newsletter = $_POST['newsletter']; with:
$x_newsletter = (isset($_POST['newsletter'])) ? $_POST['newsletter'] : 'No';
I purchased a template from Template Monster and I can't figure out how to make the form work.
Here's the form code:
<form id="form">
<div class="success_wrapper">
<div class="success-message">Το μήνυμά σας εστάλη.</div>
</div>
<label class="name">
<input type="text" placeholder="Όνομα*:" data-constraints="#Required #JustLetters" />
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το όνομα δεν είναι έγκυρο.</span>
</label>
<label class="email">
<input type="text" placeholder="Email*:" data-constraints="#Required #Email" />
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το email δεν είναι έγκυρο.</span>
</label>
<label class="phone">
<input type="text" placeholder="Τηλέφωνο:" data-constraints=" #JustNumbers"/>
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το τηλέφωνο δεν είναι έγκυρο.</span>
</label>
<label class="message">
<textarea placeholder="Μήνυμα:" data-constraints=' #Length(min=20,max=999999)'></textarea>
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το μήνυμα είναι πολύ μικρό.</span>
</label>
<div>
<div class="clear"></div>
<div class="btns">
Καθαρισμος
Αποστολη</div>
</div>
</form>
There is also a MailHandler.php in a folder named "bat" with the following code:
<?php
//SMTP server settings
$host = "smtp.host.com";
$port = "587";
$username = "";
$password = "";
$messageBody = "";
if($_POST['name']!='false'){
$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['email']!='false'){
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}else{
$headers = '';
}
if($_POST['state']!='false'){
$messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='false'){
$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['fax']!='false'){
$messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['message']!='false'){
$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}
if($_POST["stripHTML"] == 'true'){
$messageBody = strip_tags($messageBody);
}
if($host=="" or $username=="" or $password==""){
$owner_email = $_POST["owner_email"];
$headers = 'From:' . $_POST["email"] . "\r\n" . 'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$subject = 'A message from your site visitor ' . $_POST["name"];
try{
if(!mail($owner_email, $subject, $messageBody, $headers)){
throw new Exception('mail failed');
}else{
echo 'mail sent';
}
}catch(Exception $e){
echo $e->getMessage() ."\n";
}
}else{
require_once 'Mail.php';
$to = $_POST["owner_email"];
$subject = 'A message from your site visitor ' . $_POST["name"];
$headers = array (
'From' => 'From:' . $_POST["email"] . "\r\n" . 'Content-Type: text/plain; charset=UTF-8' . "\r\n",
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory(
'smtp',
array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $messageBody);
try{
if(PEAR::isError($mail)){
echo $mail->getMessage();
}else{
echo 'mail sent';
}
}catch(Exception $mail){
echo $mail->getMessage() ."\n";
}
}
?>
Any help will be truly appreciated!
Ok I figured this one out... You have to get into the forms javascript file it will look like TMForms.js or something like that in the file named JS, under the very top portion of the code that looks like this
okClass:'ok'
,emptyClass:'empty'
,invalidClass:'invalid'
,successClass:'success'
,onceVerifiedClass:'once-verified'
,mailHandlerURL:'http://www.yourwebsitename.com/bat/MailHandler.php
,successShowDelay:'4000'
,stripHTML:true
MAKE SURE TO PUT THE FULL URL PATH TO THE BAT FOLDER LIKE I HAVE in bold, this will fix your issue.
Replace yourwebsite name with your actual URL and you are good to go...
You must contact the support from where your purchased the template, they can help you
echo '<div id="email">
<form action="#" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal .'';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
Sending the email is working fine, it just wont let catch the inserted email.
I can't get the value of <input .... name="mail1"> (me#me.nl) into the "FROM:" section.
What do i wrong OR what is the thing that i don't do in this case ?
Whenever using $headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n"; it works perfectly.
Try
echo '<div id="email">
<form action="a.php" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
if (isset($_POST['submitemail'])) {
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal . '';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
}
the a.php is the name of ur php file