1

I'm trying to send an email with the message being partly from a variable and partly preset. The email arrives fine when it's either just set as text or just a variable but as soon as I try and use both it never arrives. I've confirmed that all the variables are correct by echoing them at the end but it just never actually arrives in my mailbox.

The code is probably not the best way of doing things since I've not done much coding for a few years but if anyone can tell me why this is happening it would be appreciated:

                <?php
                $action=$_REQUEST['action'];
                if ($action=="")    /* display the contact form */
                    {
                    ?>
                    <form  action="" method="POST" enctype="multipart/form-data" id="emailform">

                        <input type="hidden" name="action" value="submit">

                        <label for="contact_name">Name <span>*</span></label>                           
                        <input name="name" type="text" value="" size="30"/><br>

                        <label for="email">Email <span>*</span></label>                         
                        <input name="email" type="text" value="" size="30"/><br>

                        <label for="contact_name">Why can't you pay? <span>*</span></label>                         
                        <input name="nopay" type="text" value="" size="30"/><br>

                        <input class="submit-button" type="submit" value="Send Email" id="submit" name="submit">
                    </form>
                    <?php
                    } 
                else                /* send the submitted data */
                    {
                    $name=$_REQUEST['name'];
                    $email=$_REQUEST['email'];
                    $message='Why cant you pay '. $_REQUEST['nopay'];
                    if (($name=="")||($email=="")||($message==""))
                        {
                        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
                        }
                    else{       
                        $from="From: $name<$email>\r\nReturn-path: $email";
                        $subject="Sacred Arts Camp Enquiry";
                        mail("myemail@gmail.com", $subject, $message, $from);

                        echo "<p>Thanks for your email!<br/>Someone will pick it up shortly and be in touch to help you out.</p>";
                        }
                    }  
                ?>
Risi Cheshire
  • 25
  • 1
  • 3
  • Does it get to the `echo "

    Thanks for your email!
    Someone will pick it up shortly and be in touch to help you out.

    ";` all the time?
    – Rasclatt Jan 02 '17 at 17:03
  • Yes, it does. When I was testing to ensure all variables are correct I was putting them after that and they were displaying also. – Risi Cheshire Jan 02 '17 at 17:18
  • Try `if(mail("myemail@gmail.com", $subject, $message, $from)) echo 'Sent';` See if it writes `Sent` – Rasclatt Jan 02 '17 at 17:20
  • I would use `$_POST` instead of `$_REQUEST`. Also, you will want to validate your email address with `filter_var($_POST['email'],FILTER_VALIDATE_EMAIL);` – Rasclatt Jan 02 '17 at 17:24
  • Also, `$action=$_REQUEST['action'];` will generate a warning, it's not fatal but you should address it properly. You should remove `$action=$_REQUEST['action'];` and then change `if ($action=="")` to `if(!isset($_POST['action'])) {` – Rasclatt Jan 02 '17 at 17:28

1 Answers1

0

Please put this code in

 else   
{
        $to = $email; // Send email to our user
        $subject = 'Sacred Arts Camp Enquiry'; // Give the email a subject 
        $message = ' your message';                    
        $headers = 'From:www.yourwebsite.com' . "\r\n"; // Set from headers
        mail($to, $subject, $message, $headers); // Send our email      
}

And please try this with online server.

Mohit Yadav
  • 471
  • 8
  • 17