Home > My Works, PHP, Programming Help > phpmailer for codeigniter (ci)

phpmailer for codeigniter (ci)

on our new server we was getting problem with mail sending. yahoo and some other mail server was not receiving our mails to there inbox. so i imported the most popular php class (phpmailer ) in codeigniter as plugin. it’s install and use is too simple. you have to download the zip file from here, then unzip it to your plugin (systemplugins) folder. then edit phpmailer_pi.php , and edit the default configuration as needed as your mail server (default configuration will work fine with gmail smtp, you only have to edit the user name and pass) .

download:

phpmailer for codeigniter

usage:

you can now use all the functions that phpmailer supported. here is a the example that you will get in phpmailer site, but in ci format.

 < ?php
/*
 * Created on May 19, 2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

 class Action extends Controller {

	function Action()
	{
		parent::Controller();
	}

   function testMailer()
   {
	   error_reporting(E_ERROR);

	   $this->load->plugin('phpmailer');
	   $mail=new PHPMailer();

	/*$mail->IsSMTP();
	$mail->SMTPAuth   = true;                  // enable SMTP authentication
	$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
	$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
	$mail->Port       = 465;                   // set the SMTP port 

	$mail->Username   = "[email protected]";  // GMAIL username
	$mail->Password   = "xxxxxx";            // GMAIL password
	*/

	$mail->From       = "[email protected]";
	$mail->FromName   = "sender name";
	$mail->Subject    = "This is the subject";
	$mail->Body       = "Hi,This is the HTML BODY";                      //HTML Body
	$mail->AltBody    = "This is the body when user views in plain text format"; //Text Body
	//print_r($mail);
	$mail->WordWrap   = 50; // set word wrap

	$mail->AddAddress("[email protected]");
	$mail->AddReplyTo("[email protected]","sender name");
	$mail->AddAttachment("/path/to/file.zip");             // attachment
	$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment

	$mail->IsHTML(true); // send as HTML

	if(!$mail->Send()) {
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	  echo "Message has been sent";
	}

   }
}

?>
  1. Dharmendra Kaswan
    August 21st, 2009 at 13:19 | #1

    A PHP Error was encountered

    Severity: Warning

    Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtpdomain.com:444 (Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?)

    Filename: plugins/class.smtp.php

    Line Number: 121

    Mailer Error: SMTP Error: Could not connect to SMTP host.

    If i dont use “$mail->SMTPSecure = “ssl”;” (in core php test) it works fine.

    Any Help?

    • August 28th, 2009 at 22:44 | #2

      looks like your mail server don’t need ssl, so just comment it . 🙂

  2. Bappy
    September 20th, 2009 at 12:03 | #3

    I am getting this error vaia
    can u help me out here?
    “Mailer Error: SMTP Error: Could not connect to SMTP host.”

  3. rosnovski
    October 6th, 2009 at 06:54 | #4

    Your solution was perfect for me. I cannot thank you enough for this. Don’t know if you are a God believing man but God Bless you anyway.

    PS: HE will you know.

    Keep them solutions coming dude.

    • October 6th, 2009 at 21:51 | #5

      it really nice to hear that my solution worked for you. thanks for comment, this type of comment helps me to work for community.

      btw, i believe in god 🙂

  4. Bappy
    October 7th, 2009 at 23:36 | #6

    It doesn’t work me bro
    can you help me?
    “Mailer Error: SMTP Error: Could not connect to SMTP host.”

    • October 8th, 2009 at 13:52 | #7

      hi, did you configured it correctly ?
      $mail->IsSMTP();
      $mail->SMTPAuth = true; // enable SMTP authentication
      $mail->SMTPSecure = “ssl”; // sets the prefix to the servier
      $mail->Host = “smtp.gmail.com”; // sets GMAIL as the SMTP server
      $mail->Port = 465; // set the SMTP port

      $mail->Username = “[email protected]”; // GMAIL username
      $mail->Password = “xxxxxx”; “

  5. Bappy
    October 9th, 2009 at 00:34 | #8

    thank u for replying vaia .
    yes i did .
    i uncommented it, and changed the last two fields
    still getting error! 🙁
    can there be any error because of CI version?

  6. rajesh
    March 3rd, 2010 at 11:10 | #9

    HI,
    i jaust want to know how we receive email in codeigniter?

  7. Johnray
    March 23rd, 2010 at 03:16 | #12

    Great Work! Thanks for posting. It really helped me. Thanks a lot!

  8. July 29th, 2010 at 10:45 | #13

    nice post.
    but when i tried on my application, it’s still not send anything to yahoo account.

    no meesage sent on yahoo account…

    do you know what the problem??
    thanks…

  9. Dan
    October 7th, 2010 at 08:59 | #14

    Worked immediately with gmail, thanks for the post… so much easier than trying to install sendmail… etc

  10. asim
    November 1st, 2011 at 05:54 | #15

    HI i m not reciving mails to my hotmail id can any body help me its working for gmail and yahoo but not for hotmail

    Thanks

  1. May 4th, 2011 at 03:34 | #1

61 queries in 0.118 seconds