PHP mail form won't send with spaces in email field - php

My Problem:
I have a Contact Form that gets sent to an email address. It normally works fine, but the form will not send if spaces are included in the 'Email' Text Field (where the person sending the mail would enter their email). If any other form has spaces the form will still send.
I know spaces aren't allowed in email addresses, but if the message is sent with spaces in the email field, users are still taken to a different page stating the message has been sent, when it actually hasn't. So if someone accidentally puts a space while entering the email, the website will not send the form but it will say it did.
Here's how I have it set up:
On the form the email field is automatically populated by what the user has stored as their User Account email. This variable is then passed using POST to a different page 'wl_process.php' where the fields are sent to an email address. However, even though it automatically fills the email field in for The User, they may want to have their response sent back to a different email.
What I'm trying to do, and not to do:
I'm not sure why including spaces would stop the mail from being sent. I want it to send the form even if spaces are included. I'm looking for a PHP tag or method where all spaces are stripped from the text field. I'm not sure if that exists. I think another solution may be to check for spaces in the email, and if spaces are there have something pop-up and say "Invalid Email Entered, please try again." But ideally, I just want it to send the email. And also, if anyone knows why it's doing this, that would be very beneficial for future use.
Additional Info
Keep in mind that the form works perfectly until spaces are entered into the Email field. If spaces are entered in any of the other fields, the form will still send!
Also: $var_email is pulled from a <? include?> that is located at the top of the page. The include has some $_SESSION information, and also the MYSQLI call that grabs the email out of the databse and turns into a PHP variable. I doubt this has anything to do with the problem though. My problem is the spaces will stop the mail from being sent.
What Doesn't Matter
My naming conventions. Don't get thrown off and think my variable named $email is ever treated like an Email Address in any manner. It could be renamed anything. It is only a variable and text-field here, being posted then Emailed.
My Code
Page 1 (Where the form is)
<div class="form_box">
...
<div class="field clearfix">
<label>Email <span>*</span></label>
<input id="element_1_email" name="element_1_email" value="<? echo $var_email;?>" size="30" class="validate[optional]" type="text" onClick="(this).value=''"/>
</div>
Page 2 (Where the data is sent and mailed out)
<?
$name = $_POST['element_0_name'];
$email = $_POST['element_1_email'];
$company = $_POST['element_2_company'];
$date = $_POST['element_3_date'];
$comment = $_POST['element_4_comment'];
$list = $_POST['element_5_list'];
$email_to = "contact#website.com";
$email_subject = "Online Form";
$email_message = "\n\n";
function clean_string($string) {
$bad = array ("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "",$string);
}
$email_message .= '<div style="font-family:Arial, Helvetica, sans-serif;padding-left: 90px;font-weight: 100;font-size: 14px;color: #2a2a2a;"><table width="1070px" height="685px;"border="0" cellspacing="0" background="http://www.website/image.jpg">
<tr>
<td style="vertical-align:top;padding-left:88px;">
<h2 style="padding-top:150px;padding-left:90px;">Form Information:</h2>';
$email_message .= "Name: ".clean_string($name)."<br>";
$email_message .= "Email: " .clean_string($email). "<br>";
$email_message .= "Company: " .clean_string($company). "<br>";
$email_message .= "Date Required: " .clean_string($date). "<br>";
$email_message .= "Comment: " .clean_string($comment). "<br>";
$email_message .= '<u>List Of Things:<style> tr:nth-child(2n) {background-color:#d6d6d6;}</style> </u><br><table cellspacing="0" style="margin-top:10px;min-width:390px;border:1px solid #cccccc;">' .clean_string($list). '</table>';
$email_message .= "</div>";
$headers = 'From: '.$email."\r\n".
'Reply-To: '. $email."\r\n" .
'X-Mailer: PHP/' .phpversion();
$headers .= "MIME-Version:1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
#mail($email_to, $email_subject, $email_message, $headers);
//
?>

Why not just strip out the spaces from the $_POST variable on Page 2?
Change this
$email = $_POST['element_1_email'];
to this
$email = str_replace(' ','',$_POST['element_1_email']);
----------- UPDATE -----------
Your message is failing because of this code in your $headers variable:
$headers = 'From: '.$email."\r\n".'Reply-To: '. $email."\r\n" .
You are using $email here which did not go through your clean_string function (where you could also remove the spaces by adding a space to the $bad array), so essentially you are trying to send the email to whatever was received in $_POST['element_1_email'] without any sort of clean up or email validation.

Related

Accept and record condition value from received HTML email form

I'm trying to figure out with email confirmation function via email, received by user.
If I have <input type='submit' value='Accept' id='btn1'>, (which is correct way shown by ADyson, instead double quotes: <input type="submit" value="Accept" id="btn1">), then how do I use PHP $_POST['acceptval'] for confirmation from this HTML, which must record value to MySQL database from received message form by included ID, then to pass this condition from the website.
So, if I will include require 'connect.php'; to code below, and will get data for to, message, subject, I will receive message to my userreceiveremail#gmail.com, then I want allow user press button, and record value to his record field, but I can't figure out, how it works:
<?php
$to = "userreceiveremail#gmail.com";
$subject = "Confirmation notification";
$message = "
<html>
<head>
<title>Accept registration</title>
</head>
<body>
<p>Hello, dear User! Please, accept your registration</p>
<input type='submit' value='Accept' id='btn1'>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <senderemail#gmail.com>' . "\r\n";
$headers .= 'Cc: Some text' . "\r\n";
mail($to,$subject,$message,$headers);
echo "Mail sent successfully";
?>
Maybe my scenario does not correspond to the classic or correct approach to solving this particular task, because I'm just trying to figure out, how to get desired result, anyway yet I can't find any close example for this.
Any advice, guide or example would be very helpful

Php email form not sending email from web email form

I am trying to troubleshoot this form. It is not sending reservation requests from the form on the website. Despite showing a message that the form was sent.
I tried editing email and the headers.
<?
//print_r($_POST);
$to = “email#emaildomain.com, {$posting['email']}";
function msg($text){
echo "
<script type='text/javascript'>
alert('".$text."');
top.location.href = 'http://www.aribbq.com';
</script>
";
exit;
}
function error($text){
echo "
<script type='text/javascript'>
alert('".$text."');
history.go(-1);
</script>
";
exit;
}
if (!$_POST[date]) {error('Please, insert Date.');}
if (!$_POST[time]) {error('Please, insert Time.');}
if (!$_POST[party]) {error('Please, insert Party.');}
if (!$_POST[reservation_name]) {error('Please, insert Name.');}
if (!$_POST[reservation_email]) {error('Please, insert Email.');}
if (!$_POST[reservation_phone]) {error('Please, insert Phone.');}
if(isset($_POST['submit'])){
// then send the form to your email
//$from = ('Reservation from AriBBQ.com'); // sender
$mailheaders = "From: contact#aribbq.com" . "\r\n"; // . "CC:
design#youremail.com"
$mailheaders .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$subject = "AriBBQ.com Online Reservation";
$body = "\n Contact Name: ".$_POST[reservation_name]." \r\n\n";
//
$body .= " Email: ".$_POST[reservation_email]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$body .= " Book a table \r\n\n
Date: ".$_POST[date]." \r\n\n
Time: ".$_POST[time]." \r\n\n
Party: ".$_POST[party]." \r\n\n
Contact Details \r\n\n
Name: ".$_POST[reservation_name]." \r\n\n
Email: ".$_POST[reservation_email]." \r\n\n
Phone: ".$_POST[reservation_phone]." \r\n\n
Message: ".$_POST[reservation_message]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$result = mail($to , $from , $subject , $body , $mailheaders);
if($result) {msg('Thank you, your reservation has been sent. We
will send you a confirmation text or call in person.');} //
else{error('Sending mail is failed. Please try again');} //
} else {
error('No submitted. Please try again');
}
?>
You see the form online at http://aribbq.com/. Click on reservations. Once the email is received, we want to be able to reply to the sender's email address.
Alright, essentially, you need to turn on error reporting because your script threw about 20 errors at me which you would see with error reporting on. As my comment above said, add error_reporting(E_ALL); to the top of your script while you debug.
The issues I came across are as follows:
Parse error: syntax error, unexpected '#' in /mail.php on line 4 caused by an incorrect double quote character, not " but “. Subtle, but problematic.
Next up, Multiple or malformed newlines found in additional_header in /mail.php because as of PHP 5.5.2, a bug was fixed to prevent mail header injection, so all of your \n\n within the $mailheaders should be removed, I recommend appending PHP_EOL to the end of each line instead.
You have your $from variable included in the mail() call, this presents 2 issues. One, the mail() function does not have a from parameter, you include it within the headers. Two - your variable is actually commented out.
As I mentioned in the comment above, again, your email address variable to send to is typed as $posting['email']', and $posting['Email'] within $mailheaders. The problem here is $posting doesn't exist. Secondly, your form, which you should include the HTML for in future questions for self-contained examples for people to more easily help you (see https://stackoverflow.com/help/how-to-ask), doesn't post email at all, it posts reservation_email.
Finally, the majority of your $_POST references do not include quotes so PHP doesn't know what to do with the words in between the square brackets. $_POST[date] should be $_POST['date'], for example.
I've made all the above changes and managed to successfully email myself with the script and email form provided, the only thing that I didn't look at was your msg() which didn't show me a success message. I did, however, put an echo statement before this function call which printed out fine.
I hope this helps you get your script up and running, good luck and remember, error_reporting(); is your friend!

PHP: Line Breaks on email sending not working

The following PHP code works perfectly, but it is not doing line breaks for some reason.
PHP:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: '".$title."' <".$store_email."> \n";
$subject = "New Payment Received";
//MESSAGE
$message = "New payment was successfully recieved through paypal payment terminal:";
$message .= "\r\n\nFrom ".$paypal->pp_data['payer_email'];
$message .= "\r\nPaid: ".$paypal->pp_data['payment_gross']." ".$paypal->pp_data['mc_currency'];
$message .= "\r\nDate: ".date('d/m/Y');
$message .= "\r\nTime: ".date('g:i A');
mail($admin_email,$subject,$message,$headers);
Any wonder what's wrong? Thanks in advance.
You're sending HTML e-mail. Line breaks have no meaning in HTML, you'll need <br /> tags.
The direct answer ceejayoz gives is correct and to the point in that the html element <br> is needed because it is a html email.
The bigger issue is that not all email is readable in html (example: user doesn't allow html emails). Anyone sending email should send it in 2 parts. One being a html formatted message and the other "alternative" in plain text. In that way the recipient will be able to read the email regardless of email reader.
The \r\n line break works in plain text alternative part and in html<br> or other elements as needed to format.
Doing this will avoid the next question. Recipients are complaining my emails are blank.

Bullet points in email information received from an filled in website form

I am building a website form page where a user enters in information which gets sent to my email once submitted. This appears to work ok, however, I want the information to be displayed in bullet points within the email once it has been sent.
Below shows the bit of code that sends all the information in one big chunk, but how do I add bullet points from each title i.e. company name, email, phone number etc? or even a line space is fine...
Code:
$to = 'bla#hotmail.com';
$subject = 'Quote';
$message = 'FROM: '.$companyname. '-' .$fullname. 'Email: '.$email. ' Phone Number: '.$phonenumber;
$headers = 'From: bla#hotmail.com' . "\r\n";
Expected Outcome:
Company Name:
Full Name:
Email:
Phone Number:
To send the list with formatting (or format text in general) you'll have to use HTML.
So, $headers will become:
$headers = 'From: bla#hotmail.com' . "\r\nContent-type: text/html\r\n";
to tell the client this mail is HTML and not plain text.
Then, the message will be
$message = <<<EOF
<html>
<body>
<ul>
<li>Company name: $companyname</li>
<li>Full name: $fullname </li>
</ul>
</body>
</html>
EOF;
See also "Example #4 Sending HTML email" here

How is this contact us script vulnerable / being manipulated?

A client recently got a spam warning from their host.
I think I have pin pointed the issue to an old contact us form. Simple html on the front end and a simple PHP script on the back end.
if ($_POST['submit'] == "Send"){
//START SEND MAIL SCRIPT
$mail = $_POST['email'];
$to = "me#gmail.com";
$subject = "Message from Website Contact Us Form";
$headers = "From: Contact us Form <webmaster#website.co.uk>";
$message = "Message from Contact Us Form\n\n";
$message .= "\nName: " . $_POST['contactname'];
$message .= "\nEmail: " . $_POST['contactemail'];
$message .= "\nTelephone: " . $_POST['contactphone'];
$message .= "\n\n\nMessage:\n" . $_POST['contactmessage'];
if(mail($to,$subject,$message,$headers)) {
header('Location: http://www.website.co.uk/contact-us/?action=success');
}else{
header('Location: http://www.webisite.co.uk/contact-us/?action=fail');
}//END IF MAIL
}//END SCRIPT
I know the remedies to fix it such as sanitizing post vars properly, using captchas, using a hidden 'honeypot' blank field, js tricks etc etc (I also like the look of this script too http://www.alt-php-faq.com/local/115/)
But to help me understand what was going on I want to know how this script is being manipulated. A foreign script posting vars to it but how do they send email to anyone apart from
'me#gmail.com' or if they are forcing cc / bcc fields somehow why do I not get all spam as well??
Thanks
Line like this $message .= "\nName: " . $_POST['contactname']; can be dangerous.
If $_POST['contactname']='MegaSteve4 \r\nCc: email1#mail.com, email2#mail.com'; are set, 2 uses will get spam mail.
See carefully. Its appending more headers. In this case Cc. I am not sure if Cc is a raw email header. But I hope you get the idea.
You're not doing any escaping of the post data. That means that this form is vulnerable to injection attacks.
I couldn't tell you how they did it, but that's probably what happened.

Categories