简体   繁体   中英

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

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?> <? 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 = "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM