Get $500+ of the best After Effects files, video templates and music for only $20!
CodeIgniter From Scratch: Day 3
videos

CodeIgniter From Scratch: Day 3. Sending Emails

This entry is part 3 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

At the request of some of our readers, this week, we’ll examine just how easy it is to send emails with attachments using Gmail. With raw PHP, this would be a long and tedious task. However, with CodeIgniter, it’s a cinch! I’ll show you how in this eighteen minute video tutorial.

Catch Up

Day 3


Add Comment

Discussion 419 Comments

Comment Page 6 of 6 1 ... 4 5 6
  1. Sat says:

    Awesome Tutorials dude… Thanks a lot… I am learning real codeIgniter from your tutorials.. Plz keep going….

  2. Mr Chub says:

    Great
    But i have a error:
    Call to undefined method CI_Controller::controller() in C:\wamp\www\ci\application\controllers\email.php on line 10
    Here is my code:
    class Email extends Ci_Controller{

    function __construct()
    {
    parent:: controller();
    }
    function index()
    {
    $config = Array(

    ‘protocol’=>’smtp’,
    ‘smtp_host’=>’ssl://smtp.googlemail.com’,
    ‘smtp_port’=>465,
    ‘smtp_user’=>’myemail’,
    ‘smtp_pass’=>’mypass’

    );

    /*forbinder til libary */
    $this->load->libary(‘email’, $config);
    $this->email->set_newline(“\r\n”);

    /*afsenderen*/
    $this->email->from(‘nettutsTutorials@gmail.com’);
    /*modtageren*/
    $this->email->to(‘renefrisker@gmail.com’);
    $this->email->subject(‘Det er en email test’);
    $this->email->messange(‘Det virke sku’);

    if($this->email->send()){
    echo’Din mail bliv sent’;
    }

    else
    {
    show_error($this->email->print_debugger());
    }
    }
    }

    • Peter Häggstrand says:

      @mrChub: Look at the previous comment page, you will find the answer there. It has to do with the version nr of CodeIgniter. This tutorial is based on 1.7, you’re propably using 2.1.

    • Pablo Vera H. says:

      In construct function is: parent::__construct();
      This is for Codeigniter 2.0.

      • Matt says:

        I was getting the same error. I came here after already changing my code to your solution, but I’m now getting a 504 error.

        Gateway Time-out

        The gateway did not receive a timely response from the upstream server or application.

        Any thoughts?

  3. Rene says:

    i have a error:

    Severity: Warning

    Message: mail() [function.mail]: Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()

    Filename: libraries/Email.php

    Line Number: 1553

    can any one help

    • Hey,

      The problem doesn’t lie with codeigniter. It’s your apache server that has some problems doing the requested operation. The first thing i’d do is to make sure that you have an updated server ( php 5.0+ at least ), secondly take a look at the php.ini file ( which should be located in your xampp folder under php ).

      Do a search for the keyword “mail” and you’ll find a block like this :

      [mail function]
      ; For Win32 only.
      ; http://php.net/smtp
      SMTP = localhost
      ; http://php.net/smtp-port
      smtp_port = 25

      make sure that this block is uncommented and your SMTP is set to localhost :)

      hope this helps !

    • You could also try setting up mercury for xampp and configuring it. In some countries your service provider blocks all outgoing traffic on ports under 1024 ( this is the case in my country ).

  4. Dale says:

    Fantastic series, thanks for making these.

  5. mehmet demir says:

    I am writing exactly the same code but I’m getting error like “unable to connect to ssl://smtp.googlemail.com:465 (Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?)” if I get rid of the ssl part it didn’t work again

  6. Roy says:

    Nice Tutorial

  7. Rexx says:

    Man just keep up with good work! Your tutorials really helps, thank you so much!

  8. Edward says:

    Thank you so much for this series!!!! This has been so helpful in learning CodeIgniter!!!!

  9. obaid says:

    you are using live server or local server for email to gmail??????????

  10. obaid says:

    email is sent and no error occur but when i open my gmail account there was no email.
    But the message “your email is sent ,fool” was printed on browser
    please tell me the solution

  11. pinar says:

    awesome tutorials! thanks for taking the time to make them :) keep them coming…

  12. reza says:

    I love these tutorials. Thanks so much for teaching.

  13. Nisho says:

    Thanks for sharing mate, these are just great tutorials!

  14. foo man says:

    This tutorial uses an older version of Code Igniter 1.7? So I made the adjustments for the CI_2.1 using the parent::__construct, but somehow I am still drawing a blank page from my URL http://localhost/shell/index.php/email/index. NO EMAIL WAS SENT FOOL….What should I do to get this to work? I can’t move onto the next tutorial and its frustrating :-/ Thanks in advance!

    ‘smtp’,
    ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
    ‘smtp_port’ => 465,
    ‘smtp_user’ => ‘user’,
    ‘smtp_pass’ => ‘pass’
    );

    $this->load->library(‘email’);

    $this->email->set_newline(“\r\n”);

    $this->email->from(‘rezapiri@gmail.com’, ‘Top Dog’);
    $this->email->to(‘rezapiri@gmail.com’);
    $this->email->subject(‘This is an email test’);
    $this->email->message(‘Its working’);

    if($this->email-send())
    {
    echo ‘Your email was sent fool.’;
    }

    else
    {
    show_error($this->email->print_debugger());
    }

    }

    }

  15. foo man says:

    the last post didn’t capture the controller etc…

    ‘smtp’,
    ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
    ‘smtp_port’ => 465,
    ‘smtp_user’ => ‘user’,
    ‘smtp_pass’ => ‘pass’
    );

    $this->load->library(‘email’);

    $this->email->set_newline(“\r\n”);

    $this->email->from(‘rezapiri@gmail.com’, ‘Top Dog’);
    $this->email->to(‘rezapiri@gmail.com’);
    $this->email->subject(‘This is an email test’);
    $this->email->message(‘Its working’);

    if($this->email-send())
    {
    echo ‘Your email was sent fool.’;
    }

    else
    {
    show_error($this->email->print_debugger());
    }

    }

    }

  16. Jaiveek says:

    Ur videos r really very much helpful….thnks.. :)

  17. alex says:

    Thanks for all you did. It’s very very good stuff. These videos can teach me a lot!!!

  18. Muhammad Ashfaq says:

    I am seeing this error

    Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\xampp\htdocs\ci\application\controllers\email.php on line 21

    Whlie my code is

    ‘smtp’ ,
    ‘smtp_host’ => ‘ssl://smtp.googlemail.com’ ,
    ‘smtp_port’ => ’465′ ,
    ‘smtp_user’ => ‘ashfaq202@gmail.com’ ,
    ‘smtp_pass’ => ‘*******’
    );
    }
    $this->load->library(‘email’, $config);
    $this->email->set_newline(‘\r\n’);

    $this->email->from(‘ashfaq202@gmail.com’, ‘Muhammad Ashfaq’);
    $this->email->to(‘ashfaq207@yahoo.com’);
    $this->email->subject(‘Testing my codeIgnitor Email’);
    $this->email->message(‘This is a perfect Software’);

    if($this->email->send())
    {
    echo ‘Email has been sent successfully’;
    }
    else
    {
    show_error($this->email->print_debugger());
    }
    }

  19. Rohit says:

    This took way too long. Windows Sucks!

    All the people who are facing openssl and socket errors inspite of opening ssl (uncommenting php_openssl.dll) in PHP.ini need to INSTALL openssl and Microsoft VC++ Redistibutable.

    Worked for me!

  20. Thibault says:

    Most hosting providers this will work :

    $this->load->library(‘email’);

    $config['protocol'] = ‘sendmail’;
    $config['mailpath'] = ‘/usr/sbin/sendmail’;
    $config['charset'] = ‘iso-8859-1′;
    $config['wordwrap'] = TRUE;

    $this->email->initialize($config);

  21. Pradeep says:

    Hi every one I am not able to send the mail I am getting a blank page, can anyone tell me if there are any major changes codeigniter latest version, that we need to apply in our code to make the code work.
    Thank you.

  22. Codex says:

    Great series, thanks for producing this.

    One thing I noticed though is in the newer version of codeigniter, having this line produces an error when email.php is included in the config folder:

    $this->load->library(‘email’, $config);

    It needs to be changed to just:

    $this->load->library(‘email’);

  23. Farghana says:

    Just awesome tutorials!! I am learning Codeigniter thanks to you!

  24. mohammed says:

    Just awesome tutorials!! I am learning Codeigniter thanks to you!

    :D

  25. Rob says:

    For whatever reason, I keep getting a 404 error. I’ve followed the video, and have updated my code to 2.0. Anyone help?

    My Code:

    ‘smtp’,
    ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
    ‘smtp_port’ => 465,
    ‘smtp_user’ => ‘######’,
    ‘smtp_pass’ => ‘######’
    );

    $this->load->library(‘email’, $config);
    $this->email->set_newline(“\r\n”);

    $this->email->from(‘robabby23@gmail.com’, ‘Rob Abby’);
    $this->email->to(‘robabby23@gmail.com’);
    $this->email->subject(‘This is an email’);
    $this->email->message(‘Its working!’);

    if ($this->email->send())
    {
    echo “Your email was sent, bitch!”;
    }

    else
    {
    show_error($this->email->print_debugger());
    }
    }

    }

    • Rob says:

      I realized that my 404 error had nothing to do with this Tut. I had followed the CodeIgniter Getting Started tut just before starting on these, and it had me declare a route that was overriding this controller.

      I commented out this line, and now I am getting the same SSL error as everyone else on windows, but at least its progress!

      Comment out this line if you have done the CI Docs Tut:

      $route['(:any)'] = ‘pages/view/$1′;

    • Rob says:

      Alright, I have updated my Code, my PHP.ini file, restarted Wamp, and now my page appears to just be stuck in a loading cylce…

  26. Hi,
    I am relatively new to CI, and mostly a windows user. Somehow this tutorial is not working for me. Its one error or the other and now its driving me crazy as to what wrong I am doing. I used the exact same code (with suggested modifications for new version) just to get errors like :

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

    “fwrite() expects parameter 1 to be resource, boolean given”

    Generally I am not very good with debugging the codes, so if any additional data is required to solve these problems, please someone let me know. I am using xampp 1.7 on windows 7 64 bit, codeigniter 2.1

    Somebody please help me out.

  27. When you move your email configuration to the separate email.php config file, you need to change your load function to

    $this->load->library(‘email’);

    instead of

    $this->load->library(‘email’,$config);

    :) for the rest superb !

  28. Chris says:

    I keep getting this error

    Message: file_exists() [function.file-exists]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/newci/attachments/giants.jpg) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp)

    Filename: libraries/Email.php

    Line Number: 1145

    so I got rid of

    $config['server_root'] = $_SERVER['DOCUMENT_ROOT'];

    From the config.php file and I changed the “email.php” file from

    $path = $this->config->item(‘server_root’);
    $file = $path . ‘/newci/attachments/giants.jpg’;

    $this->email->attach($file);

    and turned it into

    $file = ‘/newci/attachments/giants.jpg’;

    $this->email->attach($file);

    Now I don’t get any error’s , and my e-mail arrives, but I don’t get an attachment. Could anyone shed some light on this please?

  29. Michel says:

    And even to date, this video is being enjoyed ! It’s all still working like a charm in version 2.1.0 (with some minor tweaks which people can find by googling the error)

    Tyvm !

  30. Sanatan says:

    Awesome tutorial! Thanks a lot!!

  31. Jmoola says:

    I’ve tried all above suggestions, but still get an error:

    Severity: Warning

    Message: mail() [function.mail]: Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()

    Filename: libraries/Email.php

    Line Number: 1553

    I’ve checked php.ini and all the settings seem to be good. Any suggestions? Thanks

  32. Tom says:

    Thanks men!

  33. juster says:

    I followed the tutorial. Emails can be send but I got a notice like this

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: config

    Filename: controllers/email.php

    Line Number: 13
    I am using codeigniter 2.1.0

  34. jester says:

    The codeigniter series is the best way to learn codeigniter

  35. Andrew says:

    Hi, i need your help please, i have this error…

    Exit status code: 1
    Unable to open a socket to Sendmail. Please check settings.
    Unable to send email using PHP Sendmail. Your server might not be configured to send mail using this method.

  36. Rahul Patil says:

    Hello,

    Thanks for valuable tutorials. These are the real Codeigniter tutorials IMHO..

    Anways, coming back to the point.

    As you said, we can autoload email config in /config/email.php.. I did exactly the same and tried to run the email function (localhost/project1/index.php/email). Though it sent email successfully, i got this error :

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: config

    Filename: controllers/email.php

    Line Number: 12

    • Rahul Patil says:

      Fixed it..

      Just remove $config from $this->load->library(‘email’,$config); as it is being auto loaded :)

      Regards.
      Rahul

  37. First of all I hope this will resolve all the problem with user using CI 2.0.x.

    Instead of creating arrays for cofig just pass data lik $config['port']=465, I will leave all of my code, don’t worry. Next as $config['mail_type'] put html, or u might get an error.

    $this->load->library(‘email’);// first we load library
    $this->email->set_newline(“\r\n”);//set the new line rule
    $config['charset'] = ‘utf-8′;
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = ‘html’;
    $config['mailtype'] = ‘text’; // instead of html this is optional i just wanted to show other the default value in emal lib
    $config['protocol']=’smtp’;
    $config['smtp_port']=’465′;
    $config['smtp_host']=’ssl://smtp.googlemail.com’;
    $config['smtp_user']=#####@gmail.com’; // u put your mail here
    $config['smtp_pass']=’######’;// your pass

    $this->email->initialize($config);// crucial

    $this->email->from(‘#######@gmail.com ‘, ‘Marko Kovacevic’);
    $this->email->to(‘######@gmail.com’);

    $this->email->subject(‘Email Test’);
    $this->email->message(“The stuff works”);

    if($this->email->send())
    {
    echo “Mail sent “‘;
    }
    else{

    echo $this->email->print_debugger();

    And other part if you get error that you can’t send mai using PHP mail(), and your server in not configured you make changes in php.ini file(Win users might need to change LoadModule ssl, i think it is in httpd.conf file).

    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    SMTP = ssl://googlemail.com
    ; http://php.net/smtp-port
    smtp_port = 465

    I hope i helped a litle ;)

  38. Wangming says:

    help me alot

Comment Page 6 of 6 1 ... 4 5 6

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.