Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Wrapping Up The Project Sending The Contact Form Email

Barbara Vega
seal-mask
.a{fill-rule:evenodd;}techdegree
Barbara Vega
Full Stack JavaScript Techdegree Student 19,486 Points

phpmailer and wordpress help

I'm trying to implement the phpmailer library and code learned in this section to create a contact form for Wordpress. The code I am using is successful on a regular html site but not on Wordpress.

So the situation is that when I use phpmailer and this coding, the page redirects to a 404 error page not found and I do not receive an email.

The form is in the middle of this page (not the form on the right which also doesn't work...it's using contact form 7 plugin) http://www.missouriinjuryhelp.com/test-form-php/

Can somebody please help?

Here is my code:

<?php
/**
 * Template Name: Test Page
 *
 */
?>


<?php get_header(); ?> 
    <?php get_sidebar(); ?>            
    <!-- Start of content --->

    <div class="content">
         <?php if(have_posts()) { while(have_posts()) : the_post(); ?>   

            <h3><?php the_title();?></h3>

            <div class="horizontal-separator post-<?php echo $post->ID;?>"></div>

            <div class="content-item">






   <?php 

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $name = trim($_POST["name"]);
            $email = trim($_POST["email"]);
            $phonenumber = trim($_POST["phonenumber"]);
            $message = trim($_POST["message"]);

            if ($name == "" OR $email == "" OR $message == "" ) {
                echo "You must specify a value for a name, email address, and message.";
                exit;
            }

            foreach($_POST as $value){
                if(stripos($value, 'Content-Type:') !== FALSE ){
                    echo "There was a problem with the information you entered.";
                    exit;
                }

            }

            if ($_POST["address"] != ""){
                echo "Your form submission has an error.";
                exit;               
            }

            require_once("inc/phpmailer/class.phpmailer.php");
            $mail = new PHPMailer();

            if (!$mail->ValidateAddress($email)) {
                echo "You must specify a valid email address.";
                exit;
            }

            $email_body = "";
            $email_body = $email_body . "Name: " . $name . "<br>";
            $email_body = $email_body . "Email: " . $email . "<br>";
            $email_body = $email_body . "Phone Number: " . $phonenumber . "<br>";
            $email_body = $email_body . "Message: " . $message . "<br>";



            $mail->SetFrom($email, $name);

            $mail->AddReplyTo("barbara@onthemapmarketing.com","First Last");

            $address = "barbara@onthemapmarketing.com";
            $mail->AddAddress($address, "Name of Website");

            $mail->Subject    = "New Case submitted  - SEO by On The Map";

            $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

            $mail->MsgHTML($email_body);


            if(!$mail->Send()) {
              echo "There was a problem sending the e-mail: " . $mail->ErrorInfo;
              exit;
            } 
            echo "Thanks for the message.  We will contact you soon!";

            ?>
            <p>
            <center>
            <form method="post" action="">
            <table>
                <tr>
                    <th><label for="name">Name</label></th><td><input style="width:165px" type="text" name="name" id="name"></td>
                </tr>
                <tr>
                    <th><label for="email">Email</label></th>
                    <td><input style="width:165px" type="text" name="email" id="email"></td>
                </tr>
                <tr>
                    <th><label for="phonenumber">Phone</label></th>
                    <td><input style="width:165px" type="text" name="phonenumber" id="phonenumber"></td>
                </tr>
                <tr>
                    <th><label for="message">Message</label></th>
                    <td><textarea style="width:167px" name="message" id="message"></textarea></td>
                </tr>
                <tr style="display: none;">
                    <th><label for="address">Address</label></th>
                    <td><input type="text" name="address" id="address">
                    <p>Are you human?  Please leave this field blank</p>
                    </td>
                </tr>
                <tr><td><input type="submit" value="Send"></td></tr>
            </table>    

        </form>
            </center>

            <?php




            exit;
        }
    ?>



<div class="section page">
    <div class="wrapper">

        <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
            <p>Thanks for the email!  We will be in touch shortly!</p>
        <?php } else { ?>

        <form method="post" action="">
            <table>
                <tr>
                    <th><label for="name">Name</label></th><td><input style="width:165px" type="text" name="name" id="name"></td>
                </tr>
                <tr>
                    <th><label for="email">Email</label></th>
                    <td><input style="width:165px" type="text" name="email" id="email"></td>
                </tr>
                <tr>
                    <th><label for="phonenumber">Phone</label></th>
                    <td><input style="width:165px" type="text" name="phonenumber" id="phonenumber"></td>
                </tr>
                <tr>
                    <th><label for="message">Message &nbsp;</label></th>
                    <td><textarea style="width:167px" name="message" id="message"></textarea></td>
                </tr>
                <tr style="display: none;">
                    <th><label for="address">Address</label></th>
                    <td><input type="text" name="address" id="address">
                    <p>Are you human?  Please leave this field blank</p>
                    </td>
                </tr>
                <tr><td><input type="submit" value="Send"></td></tr>
            </table>    
            </form>
        <?php } ?>

    </div>

</div>







            </div>

         <?php endwhile; } else { ?>  
          <h3><?php the_title();?></h3>

            <div class="horizontal-separator post-<?php echo $post->ID;?>"></div>

            <div class="content-item">
                <?php the_content(); ?>   
            </div>

         <?php } ?>

<!--<div class="mailOption">
<a href="#" class="emailButton"></a>

</div>-->

    </div>

<div class="rightsidebar">
        <div class="formbox">
        <?php get_template_part('form'); ?>
        </div>




<div class="widget-box sidebar-banner" style="background-image:url(<?php echo get_field('sidebar_image', 'option'); ?>)">
            <div class="side-descrip"><?php echo get_field('sidebar_introduction', 'option'); ?></div>
            <h3>
            <?php if(get_field('sidebar_company_name_image', 'option')) { ?>
            <img src="<?php echo get_field('sidebar_company_name_image', 'option'); ?>" title="" alt="" />
            <?php } else {  echo get_field('sidebar_company_name', 'option'); } ?>

            </h3>

            <div class="side-descrip"><?php echo get_field('sidebar_introduction_2', 'option'); ?></div> 
            <h4 class="for-sd-phone"><a href="tel:3149325790" style="color:#FFF !important;text-decoration:none;">(314) 932-5790</a></h4>  
            <div class="side-address"><?php echo get_field('sidebar_address', 'option'); ?></div>  

            <a class="side-banner" href="<?php echo get_field('ad_image_link_1', 'option'); ?>" name=""><img src="<?php echo get_field('ad_image_1', 'option'); ?>" alt="" title="" /></a>
            <a class="side-banner1" href="<?php echo get_field('ad_image_link_2', 'option'); ?>" name=""><img src="<?php echo get_field('ad_image_2', 'option'); ?>" alt="" title="" /></a>
        </div>

 </div>       

        <div class="footerBg"><img src="<?php echo bloginfo('stylesheet_directory'); ?>/images/innerBodyfooter.png" ></div>

    <!-- End of content --->
<?php  get_footer(); ?>

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Just a guess, but you need to set the form's action attribute to the url of the page where that template is used ("test-form-php/"). Right now the attribute is empty and different browsers will handle this differently. Some will go back to the home page and some will submit back to the same page. My suggestion would be to use the the_permalink() function to fill in the attribute and not leave it blank.

Also, I wanted to mention the WP has a built in function for sender emails called wp_mail, that you could use to send emails. It's not very flexible but it works great for sending basic emails.

Augustin Cristian Nebunu
Augustin Cristian Nebunu
2,182 Points

I am not entirely sure but I don't think wordpress works with phpmailer. If you want to send an email from a localhost installed wordpress instance then you may have to find another way

Andrew Shook
Andrew Shook
31,709 Points

PHPMailer is part of WordPress's core. The wp_mail() function used when users register for he site is a function that wraps the PHPMailer class.