PHP Mail function mail not sending

code =

<div class="phpcontactform">
            <h3>Guaranteed Rent Enquiry</h3>
            <form class="phpmailform validate-form" action="" method="post">
                <div class="phpmailformone">
                    <div class="input-holder">
                    </div>
                    <input type="text" class="validate" name="first_name" id="first_name" placeholder="First Name" required="" aria-required="true">

                    <div class="input-holder">
                    </div>
                    <input type="text" class="validate" name="form_submission[homeflow_pot]" id="homeflow_pot" style="display: none !important;" '="" value="">



                    <input type="text" class="validate" name="last_name" id="last_name" placeholder="Last Name" required="" aria-required="true">

                    <div class="input-holder">
                    </div>

                    <input type="text" class="validate" name="email" id="email" placeholder="Email" required="" aria-required="true">

                    <div class="input-holder">
                    </div>

                    <input type="text" class="validate" name="telephone" id="telephone" placeholder="Telephone" required="" aria-required="true">

                    <div class="input-holder">
                    </div>

                    <select name="wherefrom" class="validate">
                        <option value="">How did you hear about us?</option><option value="Friend / Family">Friend / Family</option>
                        <option value="TV">TV</option>
                        <option value="Local Radio">Local Radio</option>
                        <option value="Talksport">Talksport</option>
                        <option value="Google Search">Google Search</option>
                        <option value="Online">Online</option>
                        <option value="Rightmove / Zoopla">Rightmove / Zoopla</option>
                        <option value="Email">Email</option>
                        <option value="Newspaper">Newspaper</option>
                        <option value="Other">Other</option></select>
                        <label class="statement_text  optin-label__opt_in_marketing"><input class="checkbox_class_opt_in_marketing valid" type="checkbox" name="marketing_preferences[opt_in_marketing]" aria-invalid="false"> Get emails with the latest news and information on the local property market, our products and services. You can unsubscribe at any time.</label>

                        <input id="guaranteedRentSubmitButton" class="btn std-button-red no-margin-right" style="width: 100%;" type="submit" value="Send" style="margin-top: 30px;" name="SubmitBtn">
                </div>
                <div class="phpmailformtwo">
                    <div class="input-holder">
                        <select id="branch_selector" class="validate" name="city]">
                		<option value="">Select a branch</option>
                		<option value="10537">Aberdeen</option>
                		<option value="29771">Ashford</option>
                		<option value="29078">Banbury</option>
                		<option value="28442">Barnstaple</option>
                		<option value="24877">Basildon</option>
                		<option value="15412">Basingstoke</option>
                		<option value="28449">Bedford</option>
                		<option value="28450">Beverley and Hull</option>
                		<option value="8903">Birmingham</option>
                		<option value="28451">Bolton</option>
                		<option value="5091">Bournemouth</option>
                		<option value="28766">Brentwood</option>
                		<option value="7908">Bristol</option>
                		<option value="5090">Bromley</option>
                		<option value="33895">Byfleet</option>
                		<option value="9617">Cambridge</option>
                		<option value="9738">Cardiff and Newport</option>
                		<option value="9556">Carlisle</option>
                		<option value="28453">Central Scotland</option>
                		<option value="9988">Chelmsford</option>
                		<option value="11712">Cheltenham</option>
                		<option value="9842">Chester</option>
                		<option value="28765">Coventry</option>
                		<option value="28456">Crawley</option>
                		<option value="10495">Crewe &amp; Sandbach</option>
                		<option value="23221">Derby</option>
                		<option value="10938">Doncaster</option>
                		<option value="33608">Dulwich</option>
                		<option value="23220">Dundee</option>
                		<option value="28458">Eastbourne</option>
                		<option value="11167">Edinburgh</option>
                		<option value="16245">Epsom</option>
                		<option value="6503">Exeter</option>
                		<option value="11494">Glasgow</option>
                		<option value="34077">Glossop</option>
                		<option value="11888">Harrow</option>
                		<option value="28460">Hastings</option>
                		<option value="28461">Hereford</option>
                		<option value="12056">High Wycombe</option>
                		<option value="33599">Horwich &amp; Blackrod</option>
                		<option value="12464">Ipswich</option>
                		<option value="12880">Lancaster</option>
                		<option value="13365">Leeds</option>
                		<option value="13041">Leicester</option>
                		<option value="23179">Lincoln</option>
                		<option value="12753">Liverpool</option>
                		<option value="13406">Luton</option>
                		<option value="16076">Macclesfield</option>
                		<option value="28623">Maidenhead</option>
                		<option value="28462">Milton Keynes</option>
                		<option value="14005">Newcastle</option>
                		<option value="28463">Northampton</option>
                		<option value="28464">Norwich</option>
                		<option value="14213">Nottingham</option>
                		<option value="28465">Oldham &amp; East Manchester</option>
                		<option value="28624">Oxford</option>
                		<option value="14796">Peterborough</option>
                		<option value="24307">Plymouth</option>
                		<option value="15246">Portsmouth </option>
                		<option value="15276">Preston</option>
                		<option value="28466">Reading &amp; Newbury</option>
                		<option value="29185">Retford</option>
                		<option value="15638">Romford</option>
                		<option value="28467">Salisbury</option>
                		<option value="28468">Sheffield &amp; Rotherham</option>
                		<option value="9005">Solihull</option>
                		<option value="28470">South Manchester</option>
                		<option value="1121">Southampton</option>
                		<option value="20537">Southend on Sea</option>
                		<option value="28471">Southport and Ormskirk</option>
                		<option value="10543">St Albans</option>
                		<option value="28472">Stoke on Trent</option>
                		<option value="8989">Tamworth</option>
                		<option value="34102">Taunton &amp; Bridgwater</option>
                		<option value="28473">Telford</option>
                		<option value="28474">Thorne</option>
                		<option value="34202">Torquay</option>
                		<option value="28455">Truro</option>
                		<option value="9087">Warminster</option>
                		<option value="25066">Warrington</option>
                		<option value="33097">Waterlooville</option>
                		<option value="17750">Watford</option>
                		<option value="28457">West Norwood</option>
                		<option value="17835">Wigan</option>
                		<option value="28476">Wirral</option>
                		<option value="15516">Wokingham</option>
                		<option value="17873">Worcester</option>
                		<option value="28477">Worthing &amp; Littlehampton</option>
                		<option value="13107">Wrexham</option>
                		<option value="18056">York</option></select>
                    </div>
                     or

                    <div class="input-holder">
                        <input type="text" class="validate" name="postcode" id="postcode" placeholder="Postcode">
                    </div>



                    <div class="input-holder">
                        <textarea class="validate" name="message" id="question" placeholder="Question"></textarea>
                    </div>

                </div>
            </form>

            <?php
                    if(isset($_POST["SubmitBtn"])){

                    $to = "richacrunch2@gmail.com";
                    $subject = $_POST['subject'];
                    $email_from=$_POST["email"];
                    $message=$_POST["message"];

                    $wherefrom=$_POST["wherefrom"];
                    $city=$_POST["city"];
                    $postcode=$_POST["postcode"];

                    $email_message .= "First Name: ".clean_string($first_name)."\n";
                    $email_message .= "Last Name: ".clean_string($last_name)."\n";
                    $email_message .= "Email: ".clean_string($email_from)."\n";
                    $email_message .= "Telephone: ".clean_string($telephone)."\n";
                    $email_message .= "Comments: ".clean_string($message)."\n";
                    $email_message .= "Wherefrom: ".clean_string($wherefrom)."\n";
                    $email_message .= "City: ".clean_string($city)."\n";
                    $email_message .= "Postcode: ".clean_string($postcode)."\n";

                    $headers = 'From: '.$email_from."\r\n".
                    'Reply-To: '.$email_from."\r\n" .
                    'X-Mailer: PHP/' . phpversion();
                    mail($email_to, $email_subject, $email_message, $headers);
                    echo "Email successfully sent.";
                }
            ?>
        </div>

Where am I going wrong? This is unable to send e-mail?

Live link of the form.

Go through every line of your code, check which variables are set with var_dump(), evaluate the return value of mail() and use a wrapper like PHPMailer instead.

Are you sure it is not being sent, or is it that it is being sent and you’re not receiving it?

2 Likes

Obvious mistake that @Gandalf spotted aside, it’s really bad form to use whatever the user types into a form as your “from” address. Unless your mail server is configured to allow what is known as “Open Relay”, it will probably reject the message. On a properly-configured mail server, you can only send mails from an address that server is configured to support. Create a “spare” account on your own domain and use that as the from-address, and use the “Reply-to” header if you want to just be able to click on “reply” to get back to the enquirer.

I’d also move the PHP code to be before the HTML form, apart from anything else it means your user won’t see the form being redrawn with blank boxes and presume it hasn’t worked. Presumably you’ve checked that the “include” statement for the file that contains your clean_string() function is correct?

2 Likes

sir, i am learning from a tutorial. Can you please elaborate this if possible?

Do you happen to be working locally?

1 Like

I also found this:

But they have not given example with contact form with HTML where data is coming through user input.

No on blue host shared hosting sir.

SwiftMailer would be better than using PHP’s inbuilt mail() function.

Why not start with a simple example to send an email and then add the user input later?

1 Like

Those variables are not defined. Take a close look at the variable names at the top of the script and compare with those which you are passing into the mail function.

1 Like

Yes. You use the function clean_string() several times while you are creating the $email_message variable in preparation for sending the email. As with any language, functions must either be built into the language, or defined somewhere. I can’t see that one in the PHP online documentation, I can’t see where you define it, so the only other valid way is if it’s defined in another file and referenced either using include or require statements.

Don’t you get an error message when you run the code? If not, make sure that error reporting is enabled while you’re getting used to things. It’s tempting to try to ignore warnings so they don’t mess the screen up, but you need to know what’s going wrong.

Enabling error reporting on a production server is a security vulnerability. I highly doubt BlueHost allows that. Instead you need to look at the error logs or develop locally. You should really be developing locally rather than directly on a live production server that BlueHost provides and is public facing.

XAMP and MAMP are an easy way to spin up a local develop environment. I don’t recommend them for anything other than learning but that is another topic.

Ah, I read that post about it being on a live server but I hadn’t considered that it wouldn’t be possible to enable error reporting. As you say, a local dev environment might be better for learning.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.