Jump to content
nikola

Processing contact forms

Recommended Posts

Hey! I've been tackling the code Ryan submitted for his simple contact form and I pretty much got it working, however, I was just wondering what you would recommend for spam filtering. Is just having a hidden field in the form something that you normally use for this? Or is there something more advanced you would recommend?

Share this post


Link to post
Share on other sites

Hi MidRo,

Welcome to ProcessWire and the forums. I see your question has not been answered 7 days later! Apologies, we are normally not that slow :-)

RE spam, yes, a number of us (including Ryan) recommend using a 'honeypot' field to filter out spam. Read more here:

https://processwire.com/talk/topic/960-comment-spam-filtering-alternatives/

  • Like 3

Share this post


Link to post
Share on other sites

Hello,

Thanks for this nice and simple contactform.

It works very well  :)

For Anti Spam adding a input-field with a sum:

2+2 = ?

post-3100-0-93592700-1424724337_thumb.pn

But how can I adding the field in an if-condition?

I made this on an other site in php-code, and works good:

...
if (($_POST['human'] == '4'))
...

PW is new for me and don't now how implement this with the API-field.

<?php

// Mailform

$sent = false;
$error = '';
$emailTo = 'myname@gmail.com'; // or pull from PW page field

// sanitize form values or create empty
$form = array(
    'fullname' => $sanitizer->text($input->post->fullname),
    'email' => $sanitizer->email($input->post->email),
    'comments' => $sanitizer->textarea($input->post->comments),
    ); 

// check if the form was submitted
if($input->post->submit) {
	
    // determine if any fields were ommitted or didn't validate
    foreach($form as $key => $value) {
        if(empty($value)) $error = "<div class='alert alert-danger' role='alert'>Please check that you have completed all fields.</div>";
    }

    // if no errors, email the form results
    if(!$error) {
        $msg = "Naam: $form[fullname]\n" . 
               "Email: $form[email]\n" . 
               "Comments: $form[comments]"; 

        mail($emailTo, "Contact Form", $msg, "From: $form[email]");

        // populate body with success message, or pull it from another PW field
        echo "<div class='alert alert-success' role='alert'>Dank je wel, uw emailbericht is verstuurd.</div>"; 
        $sent = true;	
    }
}

if(!$sent) {

    // sanitize values for placement in markup
    foreach($form as $key => $value) {
        $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); 
    }

    // append form to body copy
    echo <<< _OUT

        $error
        <form action="./" method="post">
        	<div class="form-group">
        		<label>Naam</label>
        		<input type="text" class="form-control" id="fullname" name="fullname" value="$form[fullname]" />
        	</div>

        	<div class="form-group">
        		<label>Email</label>
        		<input type="email" class="form-control" name="email" id="email" value="$form[email]" />
        	</div>

        	<div class="form-group">
        		<label>Comments</label>
        		<textarea rows="5" id="comments" class="form-control" name="comments">$form[comments]</textarea>
        	</div>
					
					<!-- Anti Spam -->
        	<div class="form-group">
    				<label>Hoeveel is 2+2 (Anti Spam!)</label>
    				<input type="text" class="form-control" name="human" placeholder="?" id="human" >
   				</div>

        	<div class="form-group">
        		<button type="submit" name="submit" value="Submit" class="btn btn-default">Verstuur</button>
        	</div>
        </form>

_OUT;

};
?>

Regards,

Christophe

Share this post


Link to post
Share on other sites

Hi guys,

thanks for the great tutorial, however, I get zero output. Which is unfortunate. I'm new to this whole thing and my php-skills are poor, but could this have to do something with the last line of the file, the template-include?

// include site's main template which outputs everything
include("./home.php"); 

It would be awesome if anyone could help me with this.

By the way, what would be my main template? I thought there was no such thing, the only thing that's consistent in my pages are the includes of the header and footer.

/edit: I managed to do the redirect etc., after the mail is sent my code displays a success message, unfortunately though, it doesn't send a mail. I've tried several of my email adresses as recipients, it just doesn't work. Checked my spam folders as well, no luck there. 

I'm developing locally on a WAMP stack, could this have something to do with that perhaps something about the configuration of my apache server?

/edit2: when I inspect the output of the site I get the following line:

<form action="./" method="post">

Do I need to specify a designated mail option in a separate php- script? or does the one Ryan posted already do its job?

Best regards, 

Chris

Share this post


Link to post
Share on other sites

I read about problems with blacklistings of sharedhosting servers.

Mails sendig via php mail function end up in the spam folder.

To avoid this i want to use smtp to send the mails.

What would be the easiest way to set up an smpt-mail-form?

Thanks and regards.

Share this post


Link to post
Share on other sites

Use wireMail, the processwire mail class, and install the wiremailsmtp or swiftmailer module to let it use smtp instead of php mail. There are lot's of other topics (e.g. the modules support topics) around, where you can find written out examples on how to use wiremail and the module specific api's.

Share this post


Link to post
Share on other sites

Thanks for your answer!

Ok, i installed the module "Wire Mail SMTP" and fill in the smtp data.

My template contains the following code to submit the mail:

$numSent = wireMail($emailTo, "Webmail", $msg, "From: $form[email]");

Is that the right way?
The mail didn't reached me.

Share this post


Link to post
Share on other sites

Thanks for your answer!

I checked the api docs and also tested the module configuration.

In the api docs the following code is given:

$numSent = wireMail($to, $from, $subject, $textBody);

Is that right? Or do just have to write "wireMail(....);"?

I think the arguments are ok (it works with the common php mail):

$emailTo, "Webmail", $msg, "From: $form[email]"

Share this post


Link to post
Share on other sites

Why do you think your arguments are right? I translated your fields to the variables used by the docs. You need to change the order to make it work.

// API Docs
wireMail($to, $from, $subject, $textBody);

// Your code
wireMail($to, $subject, $textBody, $headers);

… (it works with the common php mail):

While wireMail is using php mail as default sending mechanism it's not a plain replacement method.

Share this post


Link to post
Share on other sites

You are absolutely right!

This code works!

$numSent = wireMail($emailTo,  $form, "Formmail", $msg);

Thanks and best regards!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...