Jump to content

ryanC

Members
  • Posts

    97
  • Joined

  • Last visited

Posts posted by ryanC

  1. Hi, I am getting close to finishing the form I've been working on. My goal is to have users upload an image in the form, and have that image be sent along with the rest of the email content.

    Taking code from a suggested thread,  I am able to get the user uploaded image into my assets folder (which is in the code below). So I can at least receive the image... but I would like for that image to be sent in the content of the email instead. Would I be able to modify my code to do that?

    The section of the script that sends the image to the assets folder starts with //image upload begin:

     

    <?php
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    $email .= $_REQUEST['email'];
    
    if (isset($_POST['submit']) and ($_POST['email']) and ($_POST['comments']) ) {
    
    //image upload begin
    $upload_path = $config->paths->assets . "files/useruploads/";
    $user_image = new WireUpload('user_image'); // References the name of the field in the HTML form that uploads the photo
    $user_image->setMaxFiles(5);
    $user_image->setOverwrite(false);
    $user_image->setDestinationPath($upload_path);
    $user_image->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));
     
    // execute upload and check for errors
    $files = $user_image->execute();
    //image upload end
         
    $to = 'me@email.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";
    $comments = $sanitizer->entities($sanitizer->textarea($input->post->comments));
    $message .= 'Comments: ' . $comments;
    
    $success = $email;
    $success .=$message;
    
    $mail = wireMail();
    $mail->to($to)->from('me@email.com');  
    $mail->subject($subject); 
    $mail->body($message);
    $mail->send(); 
    }
    
    else {
    header("location: error-page");
    exit;
    }
    ?>
    <form id="option3form" action="./" method="post" enctype="multipart/form-data" 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><br /><br />
    <p>Click the "Choose File" button below to upload your image.</p>
    <input type="file" name="user_image" />
    <input type="submit" name="submit" value="Submit">
    </form>

    Thanks!

     

  2. 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
  3. 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!

  4. 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
  5. 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. 

  6. <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.

  7. sure, this is for the results page:

    <?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(); 
    }
    ?>

    form page: 

        <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="send" value="Submit">
        </div>
        </form>
        </div>

     

  8. 1 minute ago, adrian said:

    Ok, great to see that working, although I am still not sure why it didn't work on the Frontend, or has that solved itself also?

    But back to the original topic - this result shows that your email is successfully being prepared and sent by PW - now it's just a matter of why it isn't actually going out. I admit I didn't properly read the early posts, but if you are using WireMailSMTP, did you use the TEST option there to test your settings?

     

    It is on Front End now, so everything you did with the update was successful.

    I do have WireMailSMTP installed, and after entering Gmail settings it said it the settings were correct. Let me open up the module..

  9. 10 minutes ago, adrian said:

    Sorry, I don't know what you mean - can you post a screenshot and explain please?

    I think you definitely have some AJAX issues, but I have no idea what at the moment. Any chance this site is live somewhere I could take a look?

    Not that it should matter, but do you have the core SessionHandlerDB module installed?

    Also, what happens if you reload the page after submitting the console mail code - does the Mail panel show up populated then?

    Or, what about putting that code in a template file and loading a page with the template - does the main Mail panel show then?

    Ok... looks like we were successful here. I didn't submit but now we are up to date. Site is not live yet, and I don't have SessionHandlerDB installed.

     

×
×
  • Create New...