How to Integrate PHPmailer with Codeigniter 3

How to integrate PHPMailer with Codeigniter 3

here is a guide

1. installing PHP Mailer

Download the latest PHPMailer Build from Github.
You can find the project here

Click now on "clone or download" and download it as zip - as in the image below is shown.
PHP Mailer Download as ZIP

The folder in the zip is called PHPMailer-master.
Unzip this in your application/third_party/ folder and rename the folder to phpmailer. You should see something like this
Sample Image

2. PHP Mailer Library

Imho its best to create a library which handles your PHPMailer Object (Phpmailer_library.php)
This library could look like

class Phpmailer_library
{
public function __construct()
{
log_message('Debug', 'PHPMailer class is loaded.');
}

public function load()
{
require_once(APPPATH."third_party/phpmailer/PHPMailerAutoload.php");
$objMail = new PHPMailer;
return $objMail;
}
}

3. Using this library in one of your controllers, models etc.

class Welcome extends CI_Controller {

public function index()
{
$this->load->library("phpmailer_library");
$objMail = $this->phpmailer_library->load();
}
}

i think this should pretty much do the job.
If you've any troubles, don't hesitate to ask ;)


Update 25.06.2018

Since the PHPMailer guys removed the autoloader you've two options now:

1.) via Composer

for those who didn't know - Codeigniter supports Composer - you simply have to activate the autoload - you can find this in your config.php

$config['composer_autoload'] = true;

For more informations take a look here

After that - run composer like

composer require phpmailer/phpmailer

You now should have within your application/vendor folder the phpmailer files.

The library should look like

class Phpmailer_library
{
public function __construct()
{
log_message('Debug', 'PHPMailer class is loaded.');
}

public function load()
{
$objMail = new PHPMailer\PHPMailer\PHPMailer();
return $objMail;
}
}

2.) download

follow step 1

The library should look like

class Phpmailer_library
{
public function __construct()
{
log_message('Debug', 'PHPMailer class is loaded.');
}

public function load()
{
require_once(APPPATH.'third_party/phpmailer/src/PHPMailer.php');
require_once(APPPATH.'third_party/phpmailer/src/SMTP.php');

$objMail = new PHPMailer\PHPMailer\PHPMailer();
return $objMail;
}
}

and everything else should remain the same

How to Send QRCode to Gmail Using PHPMailer (CodeIgniter 3)

You're already using it to set the Body property in $mail->Body = $mailContent;. To add an image from an external file:

$mail->addAttachment('path/to/file.png');

If your QR code generator creates a binary string containing PNG data, use addStringAttachment:

$mail->addStringAttachment($imagedata, 'QR code.png');

Alternatively you might want to embed it and create a reference to it that you can use in your message body using addStringEmbeddedImage.

Sending an html email with phpmailer and codeigniter

Using $this->load->view('viewname'); without any additional parameters will output the view. What you need is to set the third parameter to TRUE as suggested in the Returning views as data of the CodeIgniter User Guide's View documentation.

Example:

$string = $this->load->view('viewname', '', true);

Could not instantiate mail function - Codeigniter and PHPMailer

You did not include your PHPMailer config. Since you are not using SMTP do you have this set?

$mail->isSendmail();

Also, assuming you are using CI3 it would probably be easier if you installed PHPMailer with composer and had it autoload.

I just tested this and it works fine using sendmail.

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

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

class Phpmailer_test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}

public function index()
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->isSendmail();

//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addReplyTo('info@example.com', 'Information');

//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
}

Sending email with gmail smtp with codeigniter email library

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

// Set to, from, message, etc.

$result = $this->email->send();

From the CodeIgniter Forums

Send email by using codeigniter library via localhost

Please check my working code.

function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx@gmail.com', // change it to yours
'smtp_pass' => 'xxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);

$message = '';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxx@gmail.com'); // change it to yours
$this->email->to('xxx@gmail.com');// change it to yours
$this->email->subject('Resume from JobsBuddy for your Job posting');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}

}


Related Topics



Leave a reply



Submit