Jump to content

Getting email from a processwire form (solved)


ryanC
 Share

Recommended Posts

1 minute ago, ryanC said:

That's right, I had it as submit first but was experimenting with making it "send", but didn't update the "isset" part. Is it better to use send or submit, or does it make a difference?

Doesn't matter - it just needs to be the name of one of the fields in the form that will be sent and available via POST. Again, check the Tracy Request Info panel to see what has been POSTed.

Link to comment
Share on other sites

<div class="formWrap">
    <h1>Simple Email Form 1</h1>
    <p><em>Most basic form possible to get email response in processwire</em></p>
 
    <form id="simpleForm1" action="http://localhost:8888/signage-B/form-response-1" method="post" target="_blank">
 
        <label for="name">Name:</label>
        <input type="text" name="name" id="name">
        
        <label for="email">Email:</label>
        <input type="email" name="email" id="email"><br /><br />
        
        <label for="comments">Comments:</label>
        <textarea name="comments" id="comments"></textarea>
     
    <div class="submission-test">
        <input type="submit" name="submit" value="submit">
    </div>
    </form>
    </div>

 

<?php
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

 
if (isset($_POST['submit'])) {
    $to = 'me@email.com';
    $subject = 'Feedback from my site';
    $message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
    $message .='Email: ' . $_POST['email'] . "\r\n\r\n";
    $message .='Comments: ' .$_POST['comments'];
     
     
    $mail = wireMail();
    $mail->to($to)->from('me@email.com'); // all calls can be chained
    $mail->subject($subject); 
    $mail->body($message);
    $mail->send(); 
}
?>

Ok, updated code...

is " $mail->send(); "

affected by the other things being called "submit"?

Also, after doing the updates I no  longer see "Input POST" in the request info area. It was there before, but all I see is "Input GET" now.

Link to comment
Share on other sites

OK, I tested your code and it works fine here, although I am confused what you are doing with $email - you define it and never use it. Also, I assume you are replacing the two instances of me@email.com with valid email addresses you can check?

The one thing here that might be off - you are submitting the form to another page (I think):  http://localhost:8888/signage-B/form-response-1 - is this a valid PW page, or just a PHP script? If it's just a script, you need to bootstrap in PW (https://processwire.com/api/include/) so that script has access to wireMail() etc. Does that make sense?

Link to comment
Share on other sites

Well this started by me following along with an online tutorial, https://learning.linkedin.com/blog/tech-tips/send-email-from-a-web-form-with-php 

The code below is the working example of the form, that I have running on my desktop/htdocs (real email in my desktop version).  Processwire not involved yet:

<?php
    
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if($email) {
        $headers .="\r\nReply-To: $email";
    }
    $headers = "From: me@email.com\r\n";
    $headers .= 'Content-Type: text/plain; charset=utf-8';
    if (isset($_POST['submit'])) {
    $to = 'me@email.com';
    $subject = 'Feedback from my site';
    $message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
    $message .='Email: ' . $_POST['email'] . "\r\n\r\n"; 
    $message .='Comments: ' .$_POST['comments'];

    $success = mail($to, $subject, $message, $headers, '-me@email.com');
 }

?>

 

( $email I believe was there to make sure validation went through, along with $headers. )

So the goal was to take the above code and get it to work in Processwire, which started the thread.  The final result is the code you just tested, which has the wireMail stuff added to it, which has come from this thread. So now I'm full circle trying to adapt the above code into something Processwire can use.

Regarding the pages, the HTML Form itself is a page that is based on my main template. When I hit submit, it goes to a page called "form-response-1", which is based on a new template called "form-response-template.php". 

This is literally the entire template for "form-response-template.php"

 

<?php $myPath = $config->urls->templates;?>


<?php
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

 
if (isset($_POST['submit'])) {
    $to = 'me@email.com';
    $subject = 'Feedback from my site';
    $message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
    $message .='Email: ' . $_POST['email'] . "\r\n\r\n";
    $message .='Comments: ' .$_POST['comments'];
     
     
    $mail = wireMail();
    $mail->to($to)->from('me@email.com'); // all calls can be chained
    $mail->subject($subject); 
    $mail->body($message);
    $mail->send(); 
}
?>

<!DOCTYPE HTML>
<html>
<head>
<base href="<?= $myPath ?>" />

<title><?php echo $page->title; ?></title>
</head>

<body>
    <h1>Information Received.</h1>
    <p>Thank you.</p>
    
    <!--orig-->
    
    <?php if (isset($success) && $success) { ?>
    <h1>Thank You</h1>
    Your message has been sent.
    <?php } else { ?>
    
    <h1>Not Received</h1>
    <p>Sorry, there was a problem sending your message.</p>
    <?php } ?>
    
        
    <main>
     <?php echo $page->page_content; ?>

    </main>
    
    
</body>
</html>

That's about the extent of what I am understanding regarding all of this. I really appreciate your help here.

Edit-I should add that when I hit "submit", the form result page does come up in my browser, so that part of the sequence works. 

Link to comment
Share on other sites

@ryanC I understand that you want to learn PHP from ground up but building upon a CMF like ProcessWire has the benefit of a few things: you do not have to reinvent the wheel, you can find plenty of examples in this forum to solve all basic needs and more, when you ask for help others can help you out more easily because you speak the same lingo, so to speak :)

For example: https://processwire.com/talk/topic/407-processing-contact-forms/?do=findComment&comment=36384

Your basic form processes the received data without sanitizing it which is not good. As @adrian pointed out above, you validate the email but discard the validated value afterwards. All in all, a lot should be ironed out before your form can work and be secure but since you are on ProcessWire I recommend seriously considering switching to a code snippet like the one I linked to.

You can start by reading up on API variables over here (as an introduction): https://processwire.com/api/variables/ This page is not complete since more has been added but you can find the rest here: https://processwire.com/api/ref/ The PW form example should make more sense after looking up the relevant API variables.

If you happen not to understand all the bits you can – of course – use a search engine to find things like: https://stackoverflow.com/questions/3700042/in-php-what-does-represent or ask a question in the relevant forum discussion.

You made sure you can send emails with ProcessWire, and now it's time to make sure you also use ProcessWire to build that form. I'm sure you will not regret it ;) 

Edited by szabesz
missing bits added
  • Like 4
Link to comment
Share on other sites

Thanks sbazesz, that link looks extremely helpful, exactly the type of thing I'm looking for. I will see about integrating it, if I have more specific questions about that I will probably have some follow-up questions.

I'm starting to get a better feel for Processwire in general, when I first started my test site I wanted to give up, now I have multiple templates, have hanna running, a search form, all that good stuff. So I'm sure I will get this down too.

  • Like 1
Link to comment
Share on other sites

Hi, thanks for all the help in this thread. I have been reading the links and have made some good progress. I have been able to receive the email I was trying to get two ways. I will explain what I did in case it helps someone else, and then my follow up questions will be at the end.

First I tried to use the complete form from:

https://processwire.com/talk/topic/407-processing-contact-forms/

But was running into ‘forbidden’ errors. So I did some searching and was able to get my original php mail code to work by just placing the script outside of the processwire install:

 @adrian said:

On 12/22/2014 at 5:18 PM, adrian said:

the htaccess rules that come with PW prevent calling php files from the templates folder directly :) You should get a forbidden error. If you want to call the file directly, you should place it in a folder outside the site folder. This is pretty common practice when processing forms, or calling ajax scripts. You can redirect ($session->redirect) back to a page after the submission, so the user won't even see the .php file in their browser. 

 

That worked right away, but I still wanted to do it all within Processwire itself. Even though I wasn’t able to get around the forbidden errors, I was able to follow along enough to adapt my own form to receive the email.

1: created a template that contained the html form content, and had the form post to itself (action=“./“). 

2: at the top of the template I included a link to the form script:

<?php
if($input->post->submit) {
    include("./includes/form-script.php");
} else {
   // output contact form
}

?>

3: created a blank page based on the template in step 1. Form posts to itself, submit button pulls the form script from a separate include file, email sent, mission accomplished.

This is the code I am using that DOES work:

<?php
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (isset($_POST['submit'])) {
    $to = 'myemail@.com';
    $subject = 'Feedback from my site';
    $message = 'Name: ' . $sanitizer->text($input->post->name) . "\r\n\r\n";
    $message .='Email: ' . $sanitizer->email($input->post->email)  . "\r\n\r\n";
    $message .='Comments: ' .$sanitizer->text($input->post->comments);
    
    $success = $email; //if $success gives you a “Thank you” message

    $mail = wireMail();
    $mail->to($to);  
    $mail->subject($subject); 
    $mail->body($message);
    $mail->send(); 
}
?>

Question 1: placing the form script outside of processwire in order to get the email was pretty easy, how secure is that approach?  

Question 2:  My code above is a hybrid of the posts from this thread. Do I have it set up correctly, and in a secure way? I am still getting my head around wireMail and sanitization.

Question 3: I am using MAMP, and my account is gmail. My emails are not coming in until 15-25 minutes later, is it likely this is just on Gmail's end?

I am still going to try to use some of the forms that have been provided from others but this is where I’m at right now. Thanks!

Link to comment
Share on other sites

I usually use this code (sometimes changed for some specific needs). I have installed the wiresmtp module and the mail gets send through that module. The form is a bootstrap modal that pops up.

<a href="#myModal" data-toggle="modal" data-target="#myModal">Contact</a>

This is a version that only uses 1 to email.

<?php

if (isset($_POST["submit"])) {
    $result = '<div class="alert alert-success">Form submitted</div>';

    if (!$_POST['name']) {
        $error = "<br />Vul je naam in";
    }

    if (!$_POST['email']) {
        $error .= "<br />Vul je emailaddress in";
    }

    if (!$_POST['comment']) {
        $error .= "<br />Vul een opmerking in";
    }

    if ($_POST ['email'] != "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

        $error .= "<br />Gebruik een geldig emailaddress";
    }


    if (isset($error)) {
        $result = '<div class="alert alert-danger"><strong>Da hej mangs, er is iets niet goed ingevuld:</strong> ' . $error . '</div>';
    } else {

        if (wireMail("putyouremail@here.com", $_POST['email'],
            "Mail van de website website!",
            "You have an email from the website contact form\r\n", "
Name: " . $_POST['name'] . "
Email: " . $_POST['email'] . "
Comment: " . $_POST['comment'])) {

            $result = '<div class="alert alert-success"><strong>Bedankt voor je email. Wij nemen zo spoedig mogelijk contact op.</strong></div>';

        } else {

            $result = '<div class="alert alert-error"><strong>Da hej mangs, er is iets niet goed gegaan!</strong></div>';
        }
 echo $result;
    }

}
?>


<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade slide left" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>

                </button>
                <p class="modal-title" id="myModalLabel">Contact met Webhoes!</p>

            </div>
            <div class="modal-body">
                <p class="lead">Laat wat van je horen!</p>
                <form method="post" id="myForm">
                    <div class="form-group">
                        <label for="name">Naam:</label>
                        <input type="text" name="name" id="name" class="form-control" placeholder="Name" value=""
                               required/>
                    </div>
                    <div class="form-group">
                        <label for="email">Email:</label>
                        <input type="email" name="email" id="email" class="form-control" placeholder="Email" value=""
                               required/>
                    </div>
                    <div class="form-group">
                        <label for="comment">Opmerking:</label>
                        <textarea class="form-control" id="comment" name="comment" required></textarea>
                    </div>
                    <input type="submit" name="submit" class="btn btn-success btn-lg" value="Verstuur">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-error" data-dismiss="modal">Annuleren</button>
            </div>
        </div>
    </div>
</div>

 

Last week I made a version that took an emailid (first part of the email) with js. On send it combines the first part to the domain to create the to emailadress. It is my way of not reveiling the email to the general public.

It works, but some additional security checks could be added.

  • Like 2
Link to comment
Share on other sites

On 11/1/2017 at 10:03 PM, ryanC said:

Question 1: placing the form script outside of processwire in order to get the email was pretty easy, how secure is that approach?  

As long as you do not have security concerns regarding those files there is nothing to worry about.

On 11/1/2017 at 10:03 PM, ryanC said:

Question 2:  My code above is a hybrid of the posts from this thread. Do I have it set up correctly, and in a secure way? I am still getting my head around wireMail and sanitization.

As an added precaution you might want to use htmlspecialchars() or better yet $sanitizer->entities() like this:

$comments = $sanitizer->entities($sanitizer->textarea($input->post->comments));
$message .= 'Comments: ' . $comments;

This is because $sanitizer->textarea() is for input – BTW, in your code you use $sanitizer->text() which is for single line only, so you might want to change it – while $sanitizer->entities() is for output which in this case will be performed at the email client's end. See this discussion for example (last comment at the bottom): https://stackoverflow.com/questions/17115426/do-i-need-to-sanitize-user-inputted-data-before-sending-it-in-an-email

On 11/1/2017 at 10:03 PM, ryanC said:

Question 3: I am using MAMP, and my account is gmail. My emails are not coming in until 15-25 minutes later, is it likely this is just on Gmail's end?

Sometimes it happens but I have not yet figured out when. Other times I get emails in "almost no time".

  • Like 1
Link to comment
Share on other sites

Thanks szabesz, for the code and the link. I've noticed the Gmails are coming in regularly again, so I guess that part is out of my hands.

I've gotten the form working and sending emails, thanks to the help from you guys so I'm going to mark this as solved. I will probably be back asking more about wireMail at some point, but that would be for a different thread.

 

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...