2

I added this contact form to a custom page template in wordpress:

<form id="contact_form" action="inc/mail.php" method="POST">
    <input id="name" name="name" placeholder="Name">
    <input id="email" name="email" placeholder="Email">
    <input id="phone" name="phone" placeholder="Phone Number">
    <textarea  id="comments" name="comments" placeholder="Comments / Questions"></textarea>
    <input class="button" type="submit" id="submit" value="Submit">
</form>

And I created a PHP page to handle the form once posted:

<?php   
// Gather form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$client = "";
$clientname = "";
$password = "";

if($name && $email && $comments) {

    // Create instance of PHP mailer
    require("class.phpmailer.php");
    $mail = new PHPMailer();

    $mail->IsSMTP(); 
    $mail->Host = "smtp.gmail.com"; 
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465;
    $mail->Username = $client;
    $mail->Password = $password;

    $mail->From = $client;
    $mail->SetFrom($email, $name);
    $mail->AddAddress($client, $clientname);

    $mail->IsHTML(false);   

    $formcontent="From: \n $name \n\n Email: \n $email \n\n Phone: \n $phone \n\n Comments: \n $comments";
    $mail->Subject = "Miss Mary's Inquiry";
    $mail->Body = $formcontent;

    if(!$mail->Send()){
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }
}

?>

The form and handler work perfectly outside of Wordpress, but I think Wordpress is preventing the PHP script (mail.php) from being accessed. Is there a way to post the form to my PHP script without Wordpress / htaccess interfering?

Thanks!

1
  • Oh man, this is so wide open to expoitation. Commented Jul 1, 2013 at 3:49

1 Answer 1

1

If you want to send mails in wordpress try using plugins. There are tons of wordpress plugins for that. Conatact form 7 is the one which I like the most. Still you can develop your own but create it as plugin. You can convert the above code into a shortcode plugin. Check this link

wp_mail( $to, $subject, $message, $headers, $attachments )

This function is normally used for sending mails in wordpress

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.