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


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://muabongda.com vantaiit

    function sendmail(){

    $config = array(
    ‘protocol’ => ‘smtp’,
    ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
    ‘smtp_port’ => ’465′,
    ‘smtp_user’ => ‘vantai12a7@gmail.com’,
    ‘smtp_pass’ => ‘***********’
    );
    $config['smtp_timeout']=’30′;
    $config['charset']=’utf-8′;
    $config['newline']=’\r\n’;

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

    $this->email->from(‘vantai12a7@gmail.com’, ‘vovantaiit’);
    //$this->email->to(‘khanhquay1988@yahoo.com.vn’);
    $this->email->to(‘ngoisaobangofvt@yahoo.com’);
    $this->email->subject(‘Email Test’);
    $this->email->message(‘message content’);

    if ($this->email->send()) {
    echo “mail send success”;
    } else {
    show_error($this->email->print_debugger());
    }
    }

    but the error :

    220 mx.google.com ESMTP bo7sm10842313igb.2

    hello: 250-mx.google.com at your service, [184.154.230.14]
    250-SIZE 35882577
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES

    Failed to authenticate password. Error: 535-5.7.1 Please log in with your web browser and then try again. Learn more at 535 5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 bo7sm10842313igb.2..

  • mark

    may i know sir a sample smtp_user and smtp_pass .. bcoz i cant connect .. thanks sir jeff .. i love your tuts

  • http://www.code-noodle.co.uk lee

    this tutorial series is awesome, I’ve spent months trying to get my head around an MVC framework. Finally clicked with this.

  • http://shoutfu.net glyn thomas

    Thank you so much for these excellent tuts, really enjoying the site, apologies if you already know the answer, your question from memory is you need the $this->email->set_newline(“\r\n”); (a carriage return and a line feed) so that the Mail eXchange (MX) protocol can recognize that the email is complete and ready to send the email on to the next MX server, without it, the MX server will just sit there waiting.

    Sometime ago, I remember being logged directly into an MX server, in a terminal session, meant to be checking the routes and hops, but could not help the temptation to build an email from Bill Gates, hilariously funny at the time to myself, the email sits there waiting for a carriage return and new line equivalent in terminal session, before it will send.

    also, if you have SMTP timeout errors (I have a really slow ISP), for me, changing the;

    var $smtp_timeout = 8; // SMTP Timeout in seconds

    to 8 seconds from 5 fixed the issue (Email.php in library)

    really enjoying the videos, great resource.

  • Stole

    Hi, i have a problem. I get the success message but there is no email in my inbox? Tried a couple of times but nothing worked. First i had the problem with the ssl but worked it out somehow, but now i don’t get the mail even though the message “Your email was sent, fool.” appears. Thank you

  • Tina

    If you are using a fresh xampp install you may have this problem:

    A PHP Error was encountered
    Severity: Warning

    Message: 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?)

    Filename: libraries/Email.php
    Line Number: 1689

    Go to:
    D:\Sites\xampp\php\php.ini

    Uncomment:
    ;extension=php_openssl.dll

    Found solution here:
    http://stackoverflow.com/questions/1555145/sending-email-with-gmail-smtp-with-codeigniter-email-library

  • Gaurav Kanojiya

    Jeff, you are a GEM to us. Really before coming to this tutorial I was so afraid of starting coding in MVC. I remember once i thought its impossible for me to learn it fast and in well mannered way. But now, trust me, i am going through all your awesome videos and video-by-video my confidence level in CI is increasing. For that I heartly want to say thank you to you for sharing such a nice Tutorial with us. THANKS A TONNES…takecare :)

  • Herri

    I’m sorry before, but could you upload the video again.. Looks like the video had been remove.. thanks once again

  • Barry

    when i do the email.php config file it tells me the $config variable is not defined on my email controller QQ

  • ahmed

    thanks very much

  • Vishal Choudhari

    Nice tutorial

  • Aurel

    This is really amazing!

    when I transfere the $config array to the config folder. It does work, but with a warning of ” Undefined variable: config”

    related to email controller $this->load->library('email', $config);

    why could this be

    Other then that, I am loving this

    • it4nium

      Hi Aurel,
      once you create the “email.php” into the config folder, you have to remove $config when loading the email library from your controller. CI will automatically read $config from email.php so you don’t need to pass it.

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

      My english isn’t good however I hope this was useful.

  • Elias DEBS

    I had the same problem with ssl on localhost

    Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://www.my.site.com:443 (Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?)

    you have to enable ssl on your system. Folow these steps : http://stackoverflow.com/questions/1705856/socket-transport-ssl-in-php-not-enabled

    Kind Regards ! :)

  • Anders

    What’s the security risks with sending emails like this? I’m using MAMP on my macbook pro. It worked like a charm the first time I tried it, and I have to say it was wicked fun haha. I was amazed how simple it was to send an email!

  • george

    hello… keep getting this error… Fatal error: Call to undefined method CI_Controller::Controller() in any ideas???

    • corn

      try typing this in the construct function:-

      parent::__construct();

      • http://twitter.com/BuzzeW_ Warre Buysse

        Worked over here. Would you have an explanation why the __construct(); works and controller/ CI_controller doesn’t? I had this problem quite a few times, and i’m able to fix it without knowing what’s wrong..

  • http://www.facebook.com/Sajeev.R.Nair Sajeev Nair

    Jeffery you are awesome, another lucid yet succinct tutorial !

    For any one having problems ( Jan 26 2013)
    This worked for me :
    $config = Array(

    ‘protocol’ => “smtp”,

    ‘smtp_host’ => “ssl://smtp.googlemail.com”,

    ‘smtp_port’ => 465,

    ‘smtp_user’ => “xxx@gmail.com”,

    ‘smtp_pass’ => “xxx”,

    ‘mailtype’ => “html”,

    ‘charset’ => “iso-8859-1″,

    ‘smtp_timeout’ =>30,

    ‘newline’ =>”rn”,

    ‘crlf’ => “rn”,

    ‘mailtype’ => “text”,

    ‘charset’ => “utf-8″

    );

  • http://www.facebook.com/Sagar.S.Shinde Sagar Subhash Shinde

    Nice tutorial once again. But I couldnt practice it as I didnt have the required files like the extensions of PHP.

  • http://imadalin.ro/ Madalin Ignisca

    About the CRLN mandatory stuff:

    this is from RFC 2822:

    ///////////////////////////////////////
    2.2. Header Fields

    Header fields are lines composed of a field name, followed by a colon
    (“:”), followed by a field body, and terminated by CRLF. A field
    name MUST be composed of printable US-ASCII characters (i.e.,
    characters that have values between 33 and 126, inclusive), except
    colon. A field body may be composed of any US-ASCII characters,
    except for CR and LF. However, a field body may contain CRLF when
    used in header “folding” and “unfolding” as described in section
    2.2.3. All field bodies MUST conform to the syntax described in
    sections 3 and 4 of this standard.

    //////////////////////////////////////

    if you have an old version of qmail server then put only LF as it fails with CRLF (may happen with others).

    On postfix works exactly how the email rfc requires.

  • Paramanathan Ilanthirayan

    LOAD THE MANUAL CUSTOM CONFIGURATION FILE (‘email’) by following code is not working.
    $this->load->library(‘email’, $config);
    That line suppose to be $this->load->library(‘email’);
    But in the next video you code right…..

  • Malhar Vora

    Awesome tutorial. Thanks.

  • Akhtar

    i am not getting any error message on my wamp sever but not receiving email also . anybody help me

  • Suyog

    You simply rock!

  • http://www.facebook.com/shinde89 Rahul Shinde

    Showing such kind of error can please guide me

    ( ! ) Fatal error: Class ‘Controller’ not found in C:wampwwwcodeIgniterapplicationcontrollersemail.php on line 7
    Call Stack
    #TimeMemoryFunctionLocation
    10.0004694336{main}( )..index.php:0
    20.0011787352require_once( ‘C:wampwwwcodeIgnitersystemcoreCodeIgniter.php’ )..index.php:202
    30.01251996712include( ‘C:wampwwwcodeIgniterapplicationcontrollersemail.php’ )..CodeIgniter.php:250

  • sss

    This doesn’t works?

    $path = $this->config->item(‘server_root’);
    echo ‘PATH’.$path; die();

  • Eric Greer

    More wonderful tutorials as always. Thanks Jeffery!

  • http://www.facebook.com/meme.mjv22 Mark Jones Vidad

    I’ve been reading all the comments below regarding the error on the constructor, and by removing the $config variable regards for putting the details($config[] ) details in the config.php

    -> that’s was the only error then but when I did the details above

    Errors like this came out, what should you suggest for this?(Thanks for the reply, much appreciated)
    ———————————————————————-
    A PHP Error was encountered
    Severity: Warning
    Message: fsockopen() expects parameter 2 to be long, string given
    Filename: libraries/Email.php
    Line Number: 1689
    ———————————————————————-
    A PHP Error was encountered
    Severity: Warning
    Message: fwrite() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1846
    ————————————————————————
    A PHP Error was encountered
    Severity: Warning
    Message: fgets() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1869
    ————————————————————————
    A PHP Error was encountered
    Severity: Warning
    Message: fwrite() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1846
    ————————————————————————
    A PHP Error was encountered
    Severity: Warning
    Message: fgets() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1869
    ————————————————————————-
    A PHP Error was encountered
    Severity: Warning
    Message: fwrite() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1846
    ————————————————————————-
    A PHP Error was encountered
    Severity: Warning
    Message: fgets() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1869
    ————————————————————————
    A PHP Error was encountered
    Severity: Warning
    Message: fwrite() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1846
    ————————————————————————
    A PHP Error was encountered
    Severity: Warning
    Message: fgets() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1869
    ———————————————————————–
    A PHP Error was encountered
    Severity: Warning
    Message: fwrite() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1846
    ———————————————————————-
    A PHP Error was encountered
    Severity: Warning
    Message: fwrite() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1846
    ———————————————————————
    A PHP Error was encountered
    Severity: Warning
    Message: fgets() expects parameter 1 to be resource, boolean given
    Filename: libraries/Email.php
    Line Number: 1869

  • Jonie

    I also ran into the ssl-error.
    I’m new at all this and found this very useful so I’m quoting and thanking JoseTomasTocino: “if you’re using Wampserver in Windows, you can go to the taskbar, click the wampserver icon, then PHP -> PHP extensions and select php_openssl”.
    http://stackoverflow.com/questions/6487649/setting-up-emails-in-code-igniter-2-02

    BTW, these tutorials are really great, thanks a lot!

  • aireol

    you got to restart xampp [or any localhost] to fix the error after removing the comment at :
    C:xamppphpphp.ini

    ;extension=php_openssl.dll

  • Kush

    Superb!!!!!

  • Hasibul Hasan

    when i upload it my browser. it did not show me error. but page was loading forever. it says:

    “waiting for localhost.” can anyone solve this problem..

  • http://www.forward.ph/ Lizel Wrighte

    Thank you very much for posting this. Great for starters! Keep up the good work!

  • aisha

    Thankyou. Thankyou.. Thankyou.. For taking the time to put this together. V. Diligent effort.
    Aisha

  • Ajmal

    Great tutorial it worked wonderfully

  • Alaa

    fantastic work,

    but does it really worth to learn codeIgniter ??