CodeIgniter Forums
PHPmailer in CI3? [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: External Resources (https://forum.codeigniter.com/forum-7.html)
+--- Forum: Addins (https://forum.codeigniter.com/forum-13.html)
+--- Thread: PHPmailer in CI3? [SOLVED] (/thread-71573.html)



PHPmailer in CI3? [SOLVED] - Gianluigi - 08-30-2018

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!


RE: PHPmailer in CI3? - Paradinight - 08-30-2018

(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


RE: PHPmailer in CI3? - Gianluigi - 08-31-2018

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.


RE: PHPmailer in CI3? - InsiteFX - 09-01-2018

To close a Forum topic edit your topic title and add [SOLVED] to the end of it.