Sign in to follow this  
louisstephens

Sending Form Emails

Recommended Posts

This has probably been asked a million times, but I can't seem to find an answer. I have always just used a php script to process the emails and was excited that I could pass in an email address from a page to the script using $page. However, this does throw a 403.

I was told that I shouldnt be doing it this way, but I was wondering how do you all go about handling emails? I was going to use the same page template, but I need the option of changing out the to email address dynamically.

Share this post


Link to post
Share on other sites

no idea what you're talking about, but if you post some code and more info, it should be a piece of cake....

email is a strong point for PW (wiremail, mandrill integration...)

Share this post


Link to post
Share on other sites

Sorry If i was a bit confusing earlier. What I was trying to do was create one form, where the "send to" could be changed dynamically (as this will be used on seperate pages). I got the form set up with one small issue. I cant seem to figure out to "echo" the email address into the email:

<?php
 
include("./head.inc"); 
 
echo $page->body;
 
$sent = false;
$error = '';
$emailTo = 'email@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 = "<p class='error'>Please check that you have completed all fields.</p>";
    }
 
    // if no errors, email the form results
    if(!$error) {
        $msg = "Full name: $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 "<h2>Thank you, your message has been sent.</h2>"; 
        $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">
		<fieldset>
        <p>
        <label for="fullname">Your Name</label><br />
        <input style="width:300px;" type="text" id="fullname" name="fullname" value="$form[fullname]" />
        </p>
 
        <p>
        <label for="email">Your Email</label><br />
        <input style="width:300px;" type="email" name="email" id="email" value="$form[email]" />
        </p>
 
        <p>
        <label for="comments">Comments</label><br />
        <textarea style="width:300px;" id="comments" name="comments" rows="6">$form[comments]</textarea>
        </p>
 
        <p><input type="submit" name="submit" value="Submit" /></p>
		</fieldset>
        </form>
 
_OUT;
 
}
 
// include site's footer
include("./foot.inc"); 

The way my install is set up:

I have multiple copies of the same page, all with a different header called (header_select). Header_select contains the field "contactAddress".

I need to get the contactAddress (based upon which header is chosen for that given page), and then pass that to the $emailTo field.

Share this post


Link to post
Share on other sites

you would retrieve the email address like:

$emailTo = $page->Header_select->contactAddress;

BTW - you are mixing up your cases, your variable is camelCase, your page select is underscore_case with the first letter capitalized, and your page field is camelCase.

For clarity and ease I would recommend at least being consistent with your field naming, e.g. always use underscore_case (no caps) or camelCase.

  • Like 1

Share this post


Link to post
Share on other sites

Sorry about that Macrura. I hastily built that field earlier but do plan on changing it to match the others.

I appreciate the insight from everyone! 

Share this post


Link to post
Share on other sites

no problem..

have you echo'd what PW is generating for your $page->Header_select->contactAddress ?

Share this post


Link to post
Share on other sites

Yeah, apparently the code I had in place was actually ok. It was an issue with my localhost set up. The emails were being seen as "spam" so they got rejected. I appreciate all the help!

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By anderson
      Hi,
      For the purpose of learning, as shown in this photo, I created a repeater field "we", then a template, then a page.
      But in /templates/testrepeater.php, I has some problem, the "foreach" part does not work as expected.
      <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member); ?> <img src="<?php echo $member->images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Could anybody please help point out my error?
      Thanks in advance.

    • By Demo
      Hi,
      I'm looking for a developer for a quick project. Developer would be responsible to create a new form on exiting process wire site which on submit would send data to sales force. We already have the HTML code for the form and action/REST API build. 
      Thank you
      Rajesh
    • By Demo
      Hi,
      I'm a newbie to Process wire. I'm trying to pass the form values to a different URL (i.e to sales force). We have a RestAPI to do a post method with URL Prams. when we tested the rest API it works with static values. So, i select the the option called submit to another URL in actions tab in my form. 
      So now the issue is how can i pass the field values dynamically to the REST API prams on form submit?
       
      If their is any easy way to submit form details to sales force please advice.
       
      Thanks in advance!! 

    • By gregory
      Hi guys, I need help.
      how do I translate Next Page?
      <?php if($page->next->id) {echo "<div class='float-right'><a class='button' href='{$page->next->url}'> Next Page </a></div>";} ?> I usually use this:
      <?php $lang = $user->language->name; if($lang == 'default') {echo "Next Page";} else {echo "Pagina successiva";} ?> or
      <?php echo __("Next Page"); ?> Thanks
    • By nuel
      Hi there
      Basically I want to call code within a ProcessWire page that isn't used as a template. Example: www.mypwpage.com/myphpfile.php
      I have a working PW Website with a couple of pages like /artists, /releases, /videos etc. Now I need a page /download without any editable fields in the backend, just calling some PHP code (that was coded by another guy) containing a form that checks unique download-codes in a second database and starts the download of the desired file. The script is working fine right now as part of a static website, but since I built PW behind the site, this independent «Download Section» of the page doesn't work anymore.
      Right now I have the main file download.php as a page template on a newly created empty page called /download, so until now the form is working (wow). After sending the form containing the download-code, the file check_code.php in a subfolder /site/templates/download is called and that's where I get an error.
      Any help?