Jump to content

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.

Link to comment
Share on other sites

Link to comment
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:

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
        <form action="./" method="post">
        <label for="fullname">Your Name</label><br />
        <input style="width:300px;" type="text" id="fullname" name="fullname" value="$form[fullname]" />
        <label for="email">Your Email</label><br />
        <input style="width:300px;" type="email" name="email" id="email" value="$form[email]" />
        <label for="comments">Comments</label><br />
        <textarea style="width:300px;" id="comments" name="comments" rows="6">$form[comments]</textarea>
        <p><input type="submit" name="submit" value="Submit" /></p>
// include site's footer

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.

  • Like 1
Link to comment
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
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

  • Create New...