Wednesday, November 30, 2016

Send Email With PHPMailer and CodeIgniter

CodeIgniter email class is built for handling double-byte characters which may not be the best shot for you, it sometimes break up characters, probably, due to improper UTF-8 encoding.

Instead of using CodIgniter Email class, we could instead integrate PHPMailer which you could download from here. In this way we could specify how our body is to be decoded.

Step 1. Download PHPMailer

Github page

Step 2. Paste PHPMailer to CodeIgniter

Go ahead and extract the zip file then copy it to "application/libraries".

Step 3. Create a Controller

Create a controller and name it with "Email.php".
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Email extends CI_Controller {
        function __construct(){
            parent::__construct();
            $this->load->library('phpmailer');
        }
        function send_email() {
            $response = false;
            $mail = new PHPMailer();
            $subject = 'Test subject';
            $body = 'Hi there, <strong>Carl</strong> here.<br/> This is our email body.';
            $email = 'reciever@example.com';


            $mail->CharSet = 'UTF-8';
            $mail->SetFrom('kodingmadesimple@gmail.com','Carl');

            //You could either add recepient name or just the email address.
            $mail->AddAddress($email,"Recepient Name");
            $mail->AddAddress($email);

            //Address to which recipient will reply
            $mail->addReplyTo("reply@yourdomain.com","Reply");
            $mail->addCC("cc@example.com");
            $mail->addBCC("bcc@example.com");

            //Add a file attachment
            $mail->addAttachment("file.txt", "File.txt");        
            $mail->addAttachment("images/profile.png"); //Filename is optional

            //You could send the body as an HTML or a plain text
            $mail->IsHTML(true);

            $mail->Subject = $subject;
            $mail->Body = $body;

            //Send email via SMTP
            $mail->IsSMTP();
            $mail->SMTPAuth   = true; 
            $mail->SMTPSecure = "ssl";  //tls
            $mail->Host       = "smtp.googlemail.com";
            $mail->Port       = 465; //you could use port 25, 587, 465 for googlemail
            $mail->Username   = "yourlessenabledaccount@gmail.com";
            $mail->Password   = "yourpassword";

            if(!$mail->send()){
                $response['message'] = 'Email has been sent successfully';
            }
            else{
                $response['message'] = 'Oops! Something went wrong while trying to send your email.';
            }
            echo json_encode($response);
        }
    }

Using gmail requires encryption "tls/ssl". Before you send via gmail, you need to allow less secure app to access your email account. You could make use of your gmail account or just create a new one. To do that, click here to turn it on.
That's basically it, you simply need to access your controller from your browser or extend it to your view.

13 comments:

  1. i get this error "Unable to load the requested class: Phpmailer"

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. same here... Unable to load the requested class: Phpmailer

      Delete
    3. hi Unable to load the requested class: Phpmailer fixed or not?

      Delete
    4. same problem here anyone tell the answer

      Delete
    5. Hello World !
      Good Day !

      Keep you services updated & reliable with my stuff
      Huge stuff of Tools, E-boooks, Tutorials, Scripting, Viruses, Spying e.t.c

      See Me On
      I C Q :> 752822040
      Tele-Gram :> @killhacks

      Many other stuff like
      SSN/DL
      ID's
      CC CVV
      DUMPS
      Combos/I.P's/Proxies
      You can get from my collections :-)

      No one TEACH you
      No one GUIDE you
      No one BOOST you
      But I'm always here for you

      Hit me up for you desired stuff
      I C Q here :> 752822040
      Tele-Gram here :> @killhacks

      %Waiting for you guys%

      Delete
  2. I think the final part should be this instead
    if(!$mail->send()){
    $response['message'] = 'Oops! Something went wrong while trying to send your email.';
    }
    else{
    $response['message'] = 'Email has been sent successfully';
    }

    ReplyDelete
  3. // updated controller
    CharSet = 'UTF-8';
    $mail->SetFrom('info@yourdomain.com','Yourdomain');

    //You could either add recepient name or just the email address.
    $mail->AddAddress($email,"Recepient name");
    $mail->AddAddress($email);

    //Address to which recipient will reply
    $mail->addReplyTo('reply@yourdomain.com","Reply");
    $mail->addCC("cc@example.com");
    $mail->addBCC("bcc@example.com");

    //Add a file attachment
    $mail->addAttachment("file.txt", "File.txt");
    $mail->addAttachment("images/profile.png"); //Filename is optional

    //You could send the body as an HTML or a plain text
    $mail->IsHTML(true);

    $mail->Subject = $subject;
    $mail->Body = $body;

    //Send email via SMTP
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl"; //tls
    $mail->Host = "mail.yourdomain.com";
    $mail->Port = 465; //you could use port 25, 587, 465 for googlemail
    $mail->Username = "reply@yourdomain.com";
    $mail->Password = "your-password";

    if($mail->send())
    {
    $response['message'] = 'Email has been sent successfully';
    }
    else
    {
    $response['message'] = 'Oops! Something went wrong while trying to send your email.';
    }
    echo json_encode($response);
    }
    }

    ReplyDelete
  4. //some part auto truncated


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

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


    class Email extends CI_Controller {
    function __construct()
    {
    parent::__construct();

    require APPPATH.'libraries/phpmailer/src/Exception.php';
    require APPPATH.'libraries/phpmailer/src/PHPMailer.php';
    require APPPATH.'libraries/phpmailer/src/SMTP.php';

    }

    function send_email()
    {
    $response = false;
    $mail = new PHPMailer();
    $subject = 'Test subject';
    $body = 'Hi there, <strong>Carl</strong> here.<br/> This is our email body.';
    $email = 'yourid@gmail.com';


    .....

    ReplyDelete
  5. Hi Clients!

    We have the fresh and valid USA ssn leads and dead fullz
    99% connectivity with quality
    *If you have any trust issue before any deal you may get few to test
    *Every leads are well checked and available 24 hours
    *Fully cooperate with clients

    *Format of Fullz/leads/profiles
    °First & last Name
    °SSN
    °DOB
    °(DRIVING LICENSE NUMBER)
    °ADDRESS
    (ZIP CODE,STATE,CITY)
    °PHONE NUMBER
    °EMAIL ADDRESS
    °REFERENCE DETAILS
    °BANK ACCOUNT DETAILS

    ****Contact Me****
    *ICQ :748957107
    *Telegram :@James307
    *Gmail :taimoorh944@gmail.com

    lead cost $2 for each
    Price can be negotiable if order in bulk

    *Contact soon!
    *Hope a long term deal
    *Thank You

    ReplyDelete
  6. Hello all
    am looking few years that some guys comes into the market
    they called themselves hacker, carder or spammer they rip the
    peoples with different ways and it’s a badly impact to real hacker
    now situation is that peoples doesn’t believe that real hackers and carder scammer exists.
    Anyone want to make deal with me any type am available but first
    I‘ll show the proof that am real then make a deal like

    Available Services

    ..Wire Bank Transfer all over the world

    ..Western Union Transfer all over the world

    ..Credit Cards (USA, UK, AUS, CAN, NZ)

    ..School Grade upgrade / remove Records

    ..Spamming Tool

    ..keyloggers / rats

    ..Social Media recovery

    .. Teaching Hacking / spamming / carding (1/2 hours course)

    discount for re-seller

    Contact: 24/7

    fixitrogers@gmail.com

    ReplyDelete
  7. TESTIMONY ON HOW I GOT MY LOAN FROM A GENUINE FINANCE COMPANY LAST WEEK. Email for immediate response: drbenjaminfinance@gmail.com

    I am Mrs,Leores J Miguel by name, I live in United State Of America, who have been a scam victim to so many fake lenders online between November last year till July this year but i thank my creator so much that he has finally smiled on me by directing me to this new lender who put a smile on my face this year 2020 and he did not scam me and also by not deceiving or lying to me and my friends but however this lending firm is BENJAMIN LOAN INVESTMENTS FINANCE (drbenjaminfinance@gmail.com) gave me 2% loan which amount is $900,000.00 united states dollars after my agreement to their company terms and conditions and one significant thing i love about this loan company is that they are fast and unique. {Dr.Benjamin Scarlet Owen} can also help you with a legit loan offer. He Has also helped some other colleagues of mine. If you need a genuine loan without cost/stress he his the right loan lender to wipe away your financial problems and crisis today. BENJAMIN LOAN INVESTMENTS FINANCE holds all of the information about how to obtain money quickly and painlessly via Call/Text: +1(415)630-7138 Email: drbenjaminfinance@gmail.com

    When it comes to financial crisis and loan then BENJAMIN LOAN INVESTMENTS FINANCE is the place to go please just tell him I Mrs. Leores Miguel direct you Good Luck....

    ReplyDelete
  8. Hello World !
    Good Day !

    Keep you services updated & reliable with my stuff
    Huge stuff of Tools, E-boooks, Tutorials, Scripting, Viruses, Spying e.t.c

    See Me On
    I C Q :> 752822040
    Tele-Gram :> @killhacks

    Many other stuff like
    SSN/DL
    ID's
    CC CVV
    DUMPS
    Combos/I.P's/Proxies
    You can get from my collections :-)

    No one TEACH you
    No one GUIDE you
    No one BOOST you
    But I'm always here for you

    Hit me up for you desired stuff
    I C Q here :> 752822040
    Tele-Gram here :> @killhacks

    %Waiting for you guys%

    ReplyDelete