Jump to content

Simple smtp contact form as template?


Maverick
 Share

Recommended Posts

Hey there guys,

So, I have a working contact form using phpmailer and smtp connection to receive the e-mails from website and sending autoreply to the person who sent the mail via contact form. Everything is working correctly, but I would like to integrate it with processwire more as now it's kind of too external.

My setup is as follow:

I have the contact form in html as template contact.php

The actual php sending script + phpmailer files are in a folder in my root section

I have this in the contact.php template

<form method="post" action="/test55/php/contact-form.php" id="contact">

to get the php sending script (which is outside processwire)

in the contact-form.php I have this to get the phpmailer file

require "/test55/php/PHPMailerAutoload.php";

I am wondering how could I integrate it within processwire instead of keeping the files outside? I've noticed there are three smtp modules, two based on swiftmailer and one on phpmailer, so far I can't figure it out at all.

Link to comment
Share on other sites

Hi Maverick,

How do you handle validation, if there are errors in the forms (e.g. empty required field) ?

You could move the code form your "contact-form.php" into the same template that is rendering the form, or simply include this file when the form was submitted. If the form data is valid, process the data with your working code. If the data is not valid or the form was not submitted, display the form as usual.

Once your code is implemented in a template, you can can optionally use a module to send the mails, but if everything works - why change a running system? :)

Cheers

Link to comment
Share on other sites

The reason why I would like to change it is learning something and also since I use pw I thought it would be more "secure" to have it integrated inside of pw. The script looks like this:

    $name = strip_tags(trim($_POST["name"]));
    $email = strip_tags(trim($_POST["email"]));
    $subject = strip_tags(trim($_POST["subject"]));
    $message = strip_tags(trim($_POST["message"]));
     
    if (empty($name)) {
        echo 'Enter name';
    }   
    elseif(empty($email)) {
        echo 'Enter email';
    }
    elseif(empty($subject)) {
        echo 'Enter subject';
    }
    elseif(empty($message)) {
        echo 'Enter message';
    } 
    else {
    
    require "PHPMailerAutoload.php";
        
    $mail = new PHPMailer();
    $mail->isSMTP();  
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "tls";
    $mail->Host = "host.host.net";
    $mail->Port = 587;
    $mail->Username = "username";
    $mail->Password = "password";
          
    $mail->From = $email;
    $mail->FromName = $name;
    $mail->Encoding = "base64";
    $mail->Timeout = 200;
    $mail->ContentType = "text/html";
    $mail->addAddress("email@email.com", "Name");
    $mail->Subject = "Mail subject";    
    $mail->Body = "test message";
   
    if($mail->Send()) {
        
        $automail = new PHPMailer();
        $automail->IsSMTP();
        $automail->SMTPAuth = true;
        $automail->SMTPSecure = "tls";
        $automail->Host = "host.host.net";
        $automail->Port = 587;
        $automail->Username = "username";
        $automail->Password = "password";

        $automail->From = "email@email.com";
        $automail->FromName = "Name";
        $automail->Encoding = "base64";
        $automail->Timeout = 200;
        $automail->ContentType = "text/html";
        $automail->AddAddress($email, $name);
        $automail->Subject = "Subject recieved";
        $automail->Body = "test message";
        
        if($automail->Send()){
            echo 'Sent';
        }
        else {   
            echo 'Something is wrong';
        }
    }
    }
Link to comment
Share on other sites

I have just installed the PHPMailer module https://processwire.com/talk/topic/5814-phpmailer-module/ and removed the

require "PHPMailerAutoload.php";

part from the php code. Also as you suggested Wanze I've moved the code form "contact-form.php" into the template file and it's working :D Thanks a lot! Probably not the most "clean" way to do it, but it works

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...