• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with my PHP email script

Explorer ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

Bear with me--this is my first website ever, and my first time using PHP.  I've done a good amount of reading in the interest of understanding all of this, and I think I'm pretty close to having it right, but for some reason my email script isn't working.

Screenshots of my script and the form that triggers it are below.

Basically, the PHP script gives me the echo I've coded, but no e-mail actually arrives in my inbox, and I'm not sure why (I've checked junkmail, and there's nothing there, either).

If you guys see any glaring errors in my code, I would appreciate it!

PS - I realize I haven't written security verifications in my PHP script ... for now I'm using DW's dedicated "email" and "phone" fields, and I'm limiting the name fields to 15 characters.  I'm also using a reCaptcha verification, as you can see (which works on my domain--these screenshots were taken in the localhost environment where the reCaptcha isn't authorized).  Just FYI.  Obviously open to feedback on that as well, but since I'm so new to this I'm trying to eat in small bites!

You guys rule, thanks in advance.

FormImage.png

MailscriptImage.jpg

Views

472

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , May 31, 2017 May 31, 2017

Compile your message into one variable and pass that to the mail function. Ensure the mail function can send mail to you by running a simple test. If that works, then compile as I mentioned earlier. Mail should be sending the strings $to, $subject, $message. Your mail script is sending to, subject, and instead of message as one string you're trying to send all the other values as separate arguments. Mail is expecting one string for the message.

mail("youremail@domain.com", "Test Email","This is a

...

Votes

Translate

Translate
Community Expert ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

That's one piece of the puzzle.  Where's the rest of it?

Pasting code directly into the forum is more convenient than posting screenshots of code.

Nancy

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

Nancy, when you say, "where's the rest of it," what do you mean exactly?  That's the entire code of my email script.  Here it is, pasted:

*****************

<?php

$to="info@mywebsite.com";

$subject="Submission for ".$_POST[referralfirstname].$_POST[referrallastname];

$firstname=$_POST['firstname'];

$lastname=$_POST['lastname'];

$email=$_POST['email'];

$phone=$_POST['phone'];

$timezone=$_POST['timezone'];

$besttime=$_POST['besttime'];

$referral=$_POST['referral'];

$referralfirstname=$_POST['referralfirstname'];

$referrallastname=$_POST['referrallastname'];

mail ($to, $subject,"First Name: ".$firstname, "Last Name: ".$lastname, "Email: ".$email, "Phone: ".$phone, "Time Zone: ".$timezone, "Best Time To Call: ".$besttime, "Referred From: ".$referral, "Referrer's First Name: ".$referralfirstname, "Referrer's Last Name: ".$referrallastname);

echo "Thank you for your submission! We will be in touch shortly!"

   

?>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

Where is the HTML <form> that goes with it?

Nancy

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

Ah, of course!  Here it is:

*********************

<form action="mailscript.php" method="post" name="contactForm" id="contactForm" title="Contact Form">

     <p class="answer"><em>*Required field </em></p>

     <p>

       <input name="firstname" type="text" required class="label" id="firstname" placeholder="First Name*" maxlength="15">

      

       <input name="lastname" type="text" required class="label" id="lastname" placeholder="Last Name*" maxlength="15">

      

       <input name="email" type="email" required class="label" id="email" placeholder="Email address*">

      

       <input name="phone" type="text" required class="label" id="phone" placeholder="Phone*" maxlength="15" />

      

       <select name="timezone" class="selectItem" id="timezone" title="Time Zone">

         <option>Time Zone</option>

         <option>Pacific</option>

         <option>Mountain</option>

         <option>Central</option>

         <option>Eastern</option>

         <option>Other</option>

       </select>

      

       <select name="besttime" class="selectItem" id="besttime">

         <option>Best Time To Call</option>

         <option>Morning</option>

         <option>Midday</option>

         <option>Afternoon</option>

         <option>Evening</option>

       </select>

       <select name="referral" required class="selectItem" id="referral">

         <option>How did you hear about us?</option>

         <option>Someone referred me</option>

         <option>YouTube video</option>

         <option>Facebook video</option>

         </select>

    

     <p class="answer"><em>If you were referred here by someone, please enter their name here.  This will help ensure that they receive credit for your referral.</em></p>

     <input name="referralfirstname" type="text" class="label" id="referralfirstname" placeholder="Referrer's First Name" maxlength="15">

    

     <input name="referrallastname" type="text" required class="label" id="referrallastname" placeholder="Referrer's Last Name" maxlength="15">

     

      <div class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXX"></div>

      

     </p>

    

      <input name="submit" type="submit" class="submitButton" id="submit" value="Submit">

    

   </form>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

Compile your message into one variable and pass that to the mail function. Ensure the mail function can send mail to you by running a simple test. If that works, then compile as I mentioned earlier. Mail should be sending the strings $to, $subject, $message. Your mail script is sending to, subject, and instead of message as one string you're trying to send all the other values as separate arguments. Mail is expecting one string for the message.

mail("youremail@domain.com", "Test Email","This is a test to see if you receive an email");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

Thank you!  That was a huge help.  I ran the test e-mail and it worked.

Re:  the message string ... I'm trying to get my message to include the values of of the variables I've defined.  The e-mail is coming through beautifully, but the body of the e-mail is empty.  I've tried a couple different notations for getting the variables to show up, but nothing seems to work.  

Ideally these would be separated by line breaks and not in one long horizontal line.  I tried adding "\r\n" to the end of each variable, but that just threw an error code. 

$message=

    "First Name: ".$firstname.

    "Last Name: ".$lastname.

    "Email: ".$email.

    "Phone: ".$phone.

    "Time Zone: ".$timezone.

    "Best Time To Call: ".$besttime.

    "Referred From: ".$referral.

    "Referrer's First Name: ".$referralfirstname.

    "Referrer's Last Name: ".$referrallastname.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 31, 2017 May 31, 2017

Copy link to clipboard

Copied

LATEST

I think I figured it out ... changed my message code to read this way:

****************

//message portion of the e-mail, containing prospect info//

$message="First Name: $firstname \r\n";

$message .="Last Name: $lastname \r\n";

$message .=    "Email: $email \r\n";

$message .=    "Phone: $phone \r\n";

$message .=    "Time Zone: $timezone \r\n";

$message .=    "Best Time To Call: $besttime \r\n";

$message .=    "Referred From: $referral \r\n";

$message .=    "Referrer's First Name: $referralfirstname \r\n";

$message .=    "Referrer's Last Name: $referrallastname";

//mail command//

mail($to, $subject, $message, $headers);

I'm sure there's probably a cleaner way to do this, but it works!  Thank you for your help everyone!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines