Part of the EllisLab Network
   
 
Who knows how to send form submissions via email?
Posted: 20 January 2007 03:22 PM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  01-19-2007

I suspect there’s an easy way to send all the contents of a form to an email address, natively with CodeIgnitor scripting, but I thought I’d first try some PHP techniques I’ve used in the past with the Email library. Well, it’s not working. Here’s the entire controller:

// this first part handles the Lead_Request checkbox array from the form

if (isset($_POST['Lead_Request']) && is_array($_POST['Lead_Request']))
        
{
          $_POST[
'Lead_Request'] = implode(", ", $_POST['Lead_Request']);
        
}
    
        $this
->db->insert('leads', $_POST);

// insert works without everything below
            
        
$Lead_Date = $_POST('Lead_Date');
        
$First_Name = $_POST['First_Name'];
        
$Last_Name = $_POST['Last_Name'];
        
$Company = $_POST['Company'];
        
$Title = $_POST['Title'];
        
$Phone = $_POST['Phone'];
        
$Email = $_POST['Email'];
        
$Opt_in = $_POST['Opt_in'];
        
$AcctPackage = $_POST['AcctPackage'];
        
$Lead_Received = $_POST['Lead_Received'];
        
$Lead_Source = $_POST['Lead_Source'];
        
$Promotion_Code = $_POST['Promotion_Code'];
        
$Lead_Request = $_POST['Lead_Request'];
        
$Situation = $_POST['Situation'];
        
$Comments = $_POST['Comments'];

        
$body = 'The following user has submitted a lead from the Internal Lead Form:\n\nName: $First_Name                 $Last_Name\nCompany: $Company\nPhone Number: $Phone\nEmail: $Email\nOpt In:            $Opt_in\nAccounting Package: $AcctPackage\nLead Received: $Lead_Received\nLead Source: $Lead_Source\nPromotion Code: $Promotion_Code\nCurrent Situation: $Situation\nLead Request: $Lead_Request\n\nComments: $Comments\n';

        
$this->load->library('email');
        
$this->email->from('to@xyz.com', 'Todd S');
        
$this->email->to('mickey@abc.com');
        
$this->email->subject('Internal Lead Submission');
        
$this->email->message($body);

        
$this->email->send();
    
        echo
$this->email->print_debugger();
    
}

Unfortunately, it doesn’t spit out any error, just a blank page. The db Insert works fine up until I set all the $_POST variables for the email.  I suspect one solution might to be implode all the $_POST variables into a $body array without setting them individually, as I’ve done. I’m just not sure exactly how to do that in CodeIgnitor shorthand.

Profile
 
 
Posted: 20 January 2007 03:40 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  9
Joined  01-19-2007

Okay, so I fixed this:

$Lead_Date = $_POST['Lead_Date']
// example above had parens here

And now I’m getting the email, but the message doesn’t contain the entire $body string. With ‘$body’ I get simple $body, which seems logical to me. But I tried that because with just $body I got the labels, but none of the real variables:

The following user has submitted a lead from the Internal Lead Form:nnName:
$First_Name                             $Last_NamenCompany: $CompanynPhone Number: $PhonenEmail:
$EmailnOpt In:                  $Opt_innAccounting Package: $AcctPackagenLead Received:
$Lead_ReceivednLead Source: $Lead_SourcenPromotion Code:
$Promotion_CodenCurrent Situation: $SituationnLead Request:
$Lead_RequestnnComments: $Commentsn

Profile
 
 
Posted: 20 January 2007 04:12 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  9
Joined  01-19-2007

Okay, so now I’ve resorted to putting $_POST variables directly in the email function: 

$this->email->message('A form was submitted by: '.$_POST['First_Name']);

This works, but I’m having trouble creating the line breaks with the ole “\n”

Profile
 
 
Posted: 20 January 2007 04:39 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
RankRankRank
Total Posts:  551
Joined  06-17-2006

Variables are only resolved within double quotes. This applies to escape sequences too:

$whom = "World";
echo
"Hello \n $whom";        // What you want. Hello and World on next line.

echo 'Hello \n $whom";        // What you don't want. Prints everything as you see in within single quotes.

 Signature 

CodeCrafter - Open Source Code Generation for CI

Profile
 
 
Posted: 20 January 2007 09:38 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  486
Joined  09-14-2006

Crafter is right and may I also add, you are probably better off using a view to create your email message as below. It makes for much cleaner code.
Controller:

if (isset($_POST['Lead_Request']) && is_array($_POST['Lead_Request']))
        
{
          $_POST[
'Lead_Request'] = implode(", ", $_POST['Lead_Request']);
        
}
    
        $this
->db->insert('leads', $_POST);

        
$body = $this->load->view('email_view', $_POST, TRUE); // this will parse the view and return it as a string

        
$this->load->library('email');
        
$this->email->from('to@xyz.com', 'Todd S');
        
$this->email->to('mickey@abc.com');
        
$this->email->subject('Internal Lead Submission');
        
$this->email->message($body);

        
$this->email->send();
    
}


email_view.php

The following user has submitted a lead from the Internal Lead Form:

Name: <?= $First_Name  ?> <?= $Last_Name ?>
Company
: <?= $Company ?>

... OTHER FIELDS HERE ...

Comments: <?= $Comments ?>

 Signature 

Code Igniter 1.5.4 / CentOS 5 / PHP 5.2.3 / Apache 2.2.2 / MySQL 5.0.27

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64458 Total Logged-in Users: 16
Total Topics: 80971 Total Anonymous Users: 1
Total Replies: 435716 Total Guests: 180
Total Posts: 516687    
Members ( View Memberlist )