Search code examples
phpformsmailto

How to use the Mailto function in PHP


I am really new at all of this and need help... I have a forum I am trying to send, but for some reason it is not sending. The mailto function is not sending an email on the server, and it is also not refreshing to redirect.html. Here is my code:

 <?php 
                $problem = FALSE;
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    if (empty($_POST['name'])) {
                        $problem = TRUE;
                        print '<p class="important">&bull;&nbsp;What is your name?</p>';
                    }   
                    if (empty($_POST['email'])) {
                        $problem =TRUE;
                        print '<p class="important">&bull;&nbsp;What is your email?</p>';
                    }
                    if (empty($_POST['message'])) {
                        $problem =TRUE;
                        print '<p class="important">&bull;&nbsp;What is your message?</p>';
                    }
                    if ($problem = TRUE) {print '<br />';}
                    }
            ?>
            <form action="index.php" id="contact_me" method="post">
            <table border="0" cellspacing="0" cellpadding="0" width="500">
                  <tr>
                    <td width="160">Your Name:<span class="important">*</span></td>
                    <td width="340"><input name="name" type="text" size="30" value="<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['name'])) { print htmlspecialchars($_POST['name']); }}?>"/>
                    </td>
                  </tr>
                  <tr>
                    <td><p>Your Email:<span class="important">*</span></p>
                    </td>
                    <td><input name="email" type="text" size="30" placeholder="example@example.com" value="<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['email'])) { print htmlspecialchars($_POST['email']); }}?>"/></td>
                  </tr>
                  <tr>
                    <td>Message:<span class="important">*</span></td>
                    <td><textarea name="message" rows="6" cols="40" placeholder="Your message here"><?php if($_SERVER['REQUEST_METHOD']=='POST'){if(isset($_POST['message'])){print htmlspecialchars($_POST['message']);}}?></textarea></td>
              </tr>
            </table><br />
            <input type="submit" name="submit" id="submit" value="Submit" >
            </form>
            <?php
            // Start the email function
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    if ($problem == FALSE) { // holy cow they didn't mess up
                        $name=$_REQUEST['name']; 
                        $email=$_REQUEST['email']; 
                        $message=$_REQUEST['message'];   

                        $from="From: $name<$email>\r\nReturn-path: $email"; 
                        $subject="Mathew Blair - Contact form"; 
                        mail("mathewblair@live.com", $subject, $message, $from);

                        Header("Location: redirect.html");
                    }
            }
            ?>

Thank you for your help!


Solution

  • on your last condition

     if ($problem = TRUE) {print '<br />';}
    

    you are not checking if problem is true but you are SETTING problem to TRUE

    change it to:

     if ($problem == TRUE) {print '<br />';}