Welcome Guest, Not a member yet? Register   Sign In
PHPmailer in CI3? [SOLVED]
#1

(This post was last modified: 09-02-2018, 06:57 AM by Gianluigi.)

Hi, as wirte in slack I have trouble to put PHPMailer in CI3.

This code works, but when I call the controller it don't load the view. Mail sent correctly, but page don't load (blank page).

Controller Home.php:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
   
   function __construct(){
       parent::__construct();
   }
   
public function index()
{
       $this->load->helper('mail');
       if(send_mail('info', '[email protected]', 'Name', 'Object', 'Message <b>bold</b>!'))
       {
           $this->load->view('welcome');
       }

}

Helper mail_helper.php:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! function_exists('send_mail'))
{
function send_mail($send_from = NULL, $send_to = NULL, $send_name = NULL, $send_subj = NULL, $send_body = NULL)
{
       include(APPPATH . "helpers/mail_helper/Exception.php");
       include(APPPATH . "helpers/mail_helper/PHPMailer.php");
       include(APPPATH . "helpers/mail_helper/SMTP.php");
       use PHPMailer\PHPMailer\PHPMailer;
       use PHPMailer\PHPMailer\Exception;
       $smtp_host = 'myhost';
       switch ($send_from) {
           case 'info':
               $smtp_user = 'info_user';
               $smtp_pass ='info_pass';
               break;
           case 'others':
               $smtp_user = '';
               $smtp_pass ='';
               break;
       }
       $send_from = $send_from . '@domain.com';
       $mail = new PHPMailer(true);
       $mail->IsSMTP();
       $mail->SMTPOptions = array(
         'ssl' => array(
         'verify_peer' => false,
         'verify_peer_name' => false,
         'allow_self_signed' => true
        )
       );
       $mail->SMTPAuth   = true;
       $mail->Host       = $smtp_host;
       $mail->Port       = 587;
       $mail->SMTPSecure = "tls";
       $mail->Username   = $smtp_user;
       $mail->Password   = $smtp_pass;
       $mail->SMTPDebug  = 0;
       $mail->setFrom($send_from, 'Website Name');
       $mail->addAddress($send_to, $send_name);
       $mail->Subject = $send_subj;
       $mail->Body    = $send_body;
       $mail->IsHTML(true);
       if (!$mail->send()) {
           return true;
       }
       else {
           return false;
       }
}
}

In helpers/mail_helper/ folder there are PHPMailer files included.

If I set $mail->SMTPDebug 1 or 2 instad of 0, page print error: ERR_CONTENT_DECODING_FAILED.

What's wrong with this?
Any help for corrections or to put it as library instead of helper will be great.

Thank you!
Reply
#2

(08-30-2018, 02:53 PM)Gianluigi Wrote: Hi, as wirte in slack I have trouble to put PHPMailer in CI3.

This code works, but when I call the controller it don't load the view. Mail sent correctly, but page don't load (blank page).

Controller Home.php:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
   
   function __construct(){
       parent::__construct();
   }
   
public function index()
{
       $this->load->helper('mail');
       if(send_mail('info', '[email protected]', 'Name', 'Object', 'Message <b>bold</b>!'))
       {
           $this->load->view('welcome');
       }

}

Helper mail_helper.php:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include(APPPATH . "helpers/mail_helper/Exception.php");
include(APPPATH . "helpers/mail_helper/PHPMailer.php");
include(APPPATH . "helpers/mail_helper/SMTP.php");

if ( ! function_exists('send_mail'))
{
/**
* Send mail
*
* @param string $send_from
* @param string $send_to
* @return string  $send_name
* @return string  $send_subj
* @return string  $send_body
*/
function send_mail($send_from = NULL, $send_to = NULL, $send_name = NULL, $send_subj = NULL, $send_body = NULL)
{

       $smtp_host = 'myhost';
       switch ($send_from) {
           case 'info':
               $smtp_user = 'info_user';
               $smtp_pass ='info_pass';
               break;
           case 'others':
               $smtp_user = '';
               $smtp_pass ='';
               break;
       }
       $send_from = $send_from . '@domain.com';

       $mail = new PHPMailer(true);

       $mail->IsSMTP();
       $mail->SMTPOptions = array(
         'ssl' => array(
         'verify_peer' => false,
         'verify_peer_name' => false,
         'allow_self_signed' => true
        )
       );
       
       $mail->SMTPAuth   = true;
       $mail->Host       = $smtp_host;
       $mail->Port       = 587;
       $mail->SMTPSecure = "tls";
       $mail->Username   = $smtp_user;
       $mail->Password   = $smtp_pass;
       $mail->SMTPDebug  = 0;
       
       $mail->setFrom($send_from, 'Website Name');
       $mail->addAddress($send_to, $send_name);

       $mail->Subject = $send_subj;
       $mail->Body    = $send_body;
       $mail->IsHTML(true);

       if (!$mail->send())
       {
           return true;
       }
       else
       {
           return false;
       }
       
}
}

In helpers/mail_helper/ folder there are PHPMailer files included.

If I set $mail->SMTPDebug 1 or 2 instad of 0, page print error: ERR_CONTENT_DECODING_FAILED.

What's wrong with this?
Any help for corrections or to put it as library instead of helper will be great.

Thank you!

Use after include. Check the Error Log
Reply
#3

(This post was last modified: 08-31-2018, 03:09 PM by Gianluigi.)

Oh, I've wrong here:


if (!$mail->send())
{
return true;
}
else
{
return false;
}

Remove the ! give me the page as it shoud to... Can close this. Thank you.
Reply
#4

To close a Forum topic edit your topic title and add [SOLVED] to the end of it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB