Welcome Guest, Not a member yet? Register   Sign In
Sending Codeigniter Views As HTML Content Using PHPMailer
#1

[eluser]ariffin246[/eluser]
Hello,

The following function grabs the content from view with html forms and emails the posted content to the administrator email. The configuration is fetched from a config email file under the config folder.

It can send normal content which are coded directly within the function but unable to send an external view in the email.

If the isHTML is set to true, the function goes through an infinite loops and crash.

Anyone faced similar problem as mentioned ? What's the best solutions to fix this?

I know CI got built in email class, but for some reason I had to use the PHPmailer class.

Controller
Code:
function sandbox()
{
  
//Loading the class and config file
$this->load->library('Mailer');
$this->config->load('mail', TRUE);
    
    
    //Create an email object
    $mail = new PHPMailer();


//Email default config from the config file.
$mail->IsSMTP();
$mail->SMTPAuth   = $this->config->item('SMTPAuth', 'mail');
$mail->Host       = $this->config->item('Host', 'mail');
$mail->Port       = $this->config->item('Port', 'mail');
$mail->Username   = $this->config->item('Username', 'mail');
$mail->Password   = $this->config->item('Password', 'mail');

//Mail Content
//Email From
$mail->SetFrom($this->input->post('email'), $this->input->post('name'));
//Email Reply To
$mail->AddReplyTo($this->input->post('email'), $this->input->post('name'));
//Email Subject
$mail->Subject    = "Contact Us";
//Email Content From View
$content = file_get_contents($this->load->view('mail/contact_us', '' , TRUE));
$mail->MsgHTML($content);
//Another Email Content
$mail->MsgHTML("test");

$mail->AddAddress($this->config->item('mailToEMAIL', 'mail'), $this->config->item('mailToNAME', 'mail'));


   // if the mail is sent or did not sent...
   if($mail->Send())
    {
    
    
    $this->session->set_flashdata('successMSG','Email Sent');
    redirect('contact_us');

    }
   else
    {
    $this->session->set_flashdata('errorMSG','Unable to send the email');
    redirect('contact_us');
    }
    
  
  
  
}
#2

[eluser]Aken[/eluser]
Code:
$content = file_get_contents($this->load->view('mail/contact_us', '' , TRUE));

// Wrong, should be:

$content = $this->load->view('mail/contact_us', '' , TRUE);

You're essentially passing the string data from a view as the first parameter of file_get_contents(), which is wrong. Using the TRUE third parameter of $this->load->view() will return the contents as a string, which is all you need.
#3

[eluser]ariffin246[/eluser]
[quote author="Aken" date="1344836139"]
Code:
$content = file_get_contents($this->load->view('mail/contact_us', '' , TRUE));

// Wrong, should be:

$content = $this->load->view('mail/contact_us', '' , TRUE);

You're essentially passing the string data from a view as the first parameter of file_get_contents(), which is wrong. Using the TRUE third parameter of $this->load->view() will return the contents as a string, which is all you need.[/quote]

I tested by replacing the code as you mentioned but the function goes through infinite loop and crashes. The email is sent and received successfully but the view is not included in the email.

Any idea why it happens?
#4

[eluser]Aken[/eluser]
What I suggested should have zero impact on loops and redirects. That must be in your code somewhere else. Why don't you remove any redirects and verify that your send works first, then add them in after.
#5

[eluser]ariffin246[/eluser]
[quote author="Aken" date="1344904807"]What I suggested should have zero impact on loops and redirects. That must be in your code somewhere else. Why don't you remove any redirects and verify that your send works first, then add them in after.[/quote]

Thanks for helping me out to solve the issue. I used the following code and managed to solve it. Now it works !

Code:
$mail->Body = $this->load->view('mail/contact_us', '' , TRUE);




Theme © iAndrew 2016 - Forum software by © MyBB