3
votes

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>&#42;</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 = "[email protected]";
 $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);
 //
 ?>
1
I don't think spaces are allowed in email addresses, so the attempt to "send the email anyway" won't work. The real problem here is that you're telling the user that the email was successfully sent without actually checking if it was successfully sent. If the call to @mail() results in an error then the user should be told that there was an error.David
You're not checking the return value of mail(), so you're assuming the email is sent. In fact, with the @, you're suppressing any error messages that you might be getting.andrewsi
Yes, but the 'Email Field' is just a text-field like any other field. It has nothing to do with where the form is sent. It simply tells the receiver what the senders' email address is. It could be called 'Address' or 'City'.Ghost Echo
Somehow you are not getting that your wish to send the email even if the emailaddress is invalid will never lead to a message really being sent. With the '@' you are surpressing errormessages. Messages you could use to tell the user something went wrong. Even if you filtered out the spaces from the emailaddress entered, there is still no guarantee it results in a valid/existing emailaddress.RST
@user2118228 - not really. Some mail servers check the validity of the From headers in emails they receive, and if it's not a valid address, it won't send the email.andrewsi

1 Answers

2
votes

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.