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
- Follow us on Twitter, or subscribe to the Nettuts+ RSS Feed for more daily web development tuts and articles.







Awesome tutorials dude
okay, to everyone who is having issues sending the email (like those ssl errors, or that it keeps loading on port 465) i have found the answer!
i am pretty sure, in your code, you have this
$this->email->set_newline(‘\r\n’);
‘ (single quote) should be ” (double quote, AKA magic quotes) and then it will work, strange, and sad, but true.
so change it to
$this->email->set_newline(“\r\n”);
Thank you. It worked for me
Thanks, funny mistake..
instead
$this->email->set_newline(“\r\n”)
you can use
$config['newline'] = “\r\n” (only in double quotes)
in your email.php config file
and thanks for screencasts!
This is great and easer than cAkE
Great work Jeff! Keep on doing this!
I loving the tutorials the most! Awesome job! Just purchased a Net-Tuts+
However, I keep getting these nasty errors
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to smtp.googlemail.com:25
Filename: libraries/Email.php
Line Number: 1646
First I realized that for some reason when I run it from my localhost via MAMP no email is ever sent even it gives the confirmation email… I’m about to update my MAMP version.
So then I uploaded a copy to my server and still got all the errors. I tried all of the suggestions above and the only solution that makes them go away and allow an email to be sent is remove the $config variable from this line:
$this->load->library(‘email’, $config);
so it looks like this
$this->load->library(‘email’);
and of course in which case I need to keep the config settings in that file… any thoughts or insights and this is very peculiar?
Ahhhhh figured it out. I’m a total tool. Misspelled “smtp_port” as “smtp_post”
Hey Jeff, I just wanted to let you know first of all how awesome this framework is. I’ve never worked with one before, and so far it’s absolutely amazing. Second, your videos have been immensely helpful, please keep them up!
Hello Jeff,
Firstly, I want to thank you!!
You are doing a great Job!! Keep it Up!!
I wonder if you could deal with the creation of library, helpers, and the way we could integrate a live file uploading progress bar into codeigniter?
For example a screencast about the integration of uberupload or fancyupload…
It will be very nice!!
Yes I totally agree the nettuts is one of the best. They are being unselfish and shares a lot of things.
Be sure that your server firewall doesn’t block those ports or you will get nothing but errors. That’s what you get for using cheap hosting.
Nevermind! I was on Host Monster and a previous post mentioned to switch the protocol to sendmail instead of smtp and it worked all of the sudden. Yay for forums.
Same here!
ERROR: My browser hangs on the send() function when trying to send email to a gmail account. Here’s my code:
$config = Array(
‘protocol’ => ’smtp’,
’smtp_host’ => ’ssl://smtp.googlemail.com’,
’smtp_port’ => 465,
’smtp_user’ => ‘accountname@gmail.com’,
’smtp_pass’ => ‘accountpass’
);
$this->load->library(‘email’, $config);
$this->email->set_newline(‘\r\n’);
$this->email->from(‘accountname@gmail.com’, ‘Bart Schroder’);
$this->email->to(‘accountname@gmail.com’);
$this->email->subject(‘This is an email test’);
$this->email->message(‘It works’);
if($this->email->send())
{
echo ‘Your email was sent. Great job!’;
}
else
{
show_error($this->email->print_debugger());
}
Can you help?
I have the same problem.. haven’t been able to get it to send, and I can’t figure out the problem.
Has anyone else had this & been able to get it to work?
hah! you know what.. I was trying it in chrome, and it wouldn’t work.. then I went to safari, got an error, fixed the error & it worked, and then tried it back in chrome again and it seemed to work.
weird!
Wooow…Thanx very much, great tutorials, keep on going!
Really apreciate what u’re doing…
Thx again
Great tutorial! Thanks.
Thanks man… great tutorials, i really like your way! Do you use Texter or something like that @ Mac?
Thank you.
wont work for php 5.3
Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\www\ci_day3\system\codeigniter\Common.php on line 130
Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\www\ci_day3\system\codeigniter\Common.php on line 136
source file needs to be updated
I m recieving A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
Filename: libraries/Email.php
Line Number: 1652
Can someone help me out
I have the same issue. Can’t figure it out. My code is pretty much exactly the same as the screencast…
Did anyone have any issues with the attach function? My attempt worked fine in sending an email via gmail, however when I tried to attach the yourinfo.txt file, the email still sends but there is no attachment and no message body.
$this->email->message(‘Good day to you’);
$path = $this->config->item(’server_root’);
$file = $path . ‘attachments/yourinfo.txt’;
$this->email->attach($file);
I have also tried with ‘/attachments/yourinfo.txt’ (though echo shows www//attachments with this) and echo’d out both to show it is the correct location of C:/wamp/www/attachments/yourinfo.txt but it still wont attach
BTW Jeff, love your work, this is making learning CI easy stuff
I should mention that further to this I added a JPEG to the same folder, this I can attach and see the message body. I can open this file via typing in the address in windows explorer whereas the same filepath for the text file cannot be found by windows explorer. Possibly a windows only issue?
zzz… windows saved it as yourinfo.txt.txt, my bad :p
apologies – you can delete this post and replies
how can i download this video pls
i am going away for a while and i dont have internet ,it would be great to see them offline
I’ve watched and read many tutorials and walk away from them without getting a complete grasp of them. I’ve now been watching you screencast and am learning so much from them. Thank you and please keep them coming.
Hi Jeff,
Thanks a lot for this tutorial and the hole series I hope you do not stop it.
I have a question : I am facing a lot of trouble to be able to send emails from my localhost till now. I am using windows XP and wamp 2.0 as my web server any suggestions to make this sending email process works successfully?
Awesome screencasts. I am learning a lot!
However one issue I’m having with this one. I have my controller code pretty much word for word as it is in the screencast (except stuff like my gmail username/password of course). But I get a connection time out to ssl://smtp.googlemail.com:465 This timeout causes all sorts of other PHP/CI errors to pop up as well. Any one else have this problem?
Here is my config array:
$config = array(
‘protocol’ => ’smtp’,
’smtp_host’ => ’ssl://smtp.googlemail.com’,
’smtp_port’ => 465,
’smtp_user’ => ‘xxxxxxxxxxx@gmail.com’,
’smtp_pass’ => ‘xxxxxxxx’
);
Hey Jeff, hope you read this.
I’m surprised nobody answered this but, newlines are actually considered 2 characters.
A carriage return (r) and a line feed (n).
It’s something more commonly found in software programming but as you can see it applies “across the board.”
I keep getting the following error message;
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: config
Filename: controllers/email.php
Line Number: 15
This is my code;
load->library(‘email’, $config);
$this->email->set_newline(“\r\n”);
$this->email->from(‘myemail@gmail.com’, ‘Firstname Lastname’);
$this->email->to(‘myemail@gmail.com’);
$this->email->subject(‘This is a codeigniter email test’);
$this->email->message(‘It is working. Great!’);
$path = $this->config->item(’server_root’);
$file = $path . ‘/CI_1.7.2_day3/attachments/info.txt’;
$this->email->attach($file);
if($this->email->send())
{
echo ‘Your email was sent, you fool.’;
}
else
{
show_error($this->email->print_debugger());
}
}
}
Anyone have any ideas where I have gone wrong?
Ok, I tried again using the source code from this tutorial and I am getting the same error message.
Maybe there is a problem with xammplite for windows? I will try upgrading to the latest version (I am running a version that is 3 years old) and see if it fixes it.
Hi ,
I have been trying to use codeigniter on a mac which is set up as a server. Initially, I used it with MAMP and it works fine. But now I wanna use it with the already running apache and php framework on the server.Can you help me with the steps for the same?
Thanks a lot for all the amazing tutorials !!!
Great stuff! I am feeling very motivated to use CI for my next pet project. Thank you Jeffrey!
thanks Jeffrey Way.I like your video .
i am far from you so and my internet speed is very slow.
and my english is very poor.
you know some place cannot visit your site. i must use a very way to send you.
i keep up.
These tutorials are great except for those of us with low-bandwidth (due to location) connections. If I try to watch it “live” then I get 2.5 seconds of video (as near as I can time it) followed by 8 seconds of pause while it downloads more. If I play briefly then pause and wait for the whole video to download then at some point there is a better than 50% chance that it will freeze Safari or Firefox (I’ve tried both). They are excellent videos but I’ve had to do the “wait and hour and a half for download, watch until freeze, force quit browser then start again” three times for the first video twice for the second and I’m on my third attempt for day 3.
Hi Jeff,
I just came across these a few days ago and they are brilliant, very well put together and very easy to follow, it’s also great to see the mistakes as well and how you corrected them, as it all helps in the learning process, so far I have done days one to three and got around the email arrors by using my own smtp credentials.
In the past I have looked at PHP Frameworks but been put off by the apparent complexity of them when applied to real world applications, this series has made me look again at CI and I think I would like to use it for my next site upgrade.
I apreciate that you actually started this series last year so I am a bit out of date, but please keep up the great work
I really enjoyed watching .. Thanks Jeff
Well I FINALLY got this one to work. I have to say I love these tutorials except I hated this particular one.
For anyone else new to this one I have found a couple things that have helped me. First read through all the comments first and then if that doesn’t help you try changing your smtp_host to:
’smtp_host’ => ’sslv2://smtp.googlemail.com’
For some reason it wouldn’t accept it without v2 added and you might want to try v3 if that doesn’t work. Also for people with problems with $config in $this->load->library(‘email’, $config); you might try removing the config from that and adding this line after:
$this->email->initialize($config);
For anyone still struggling with the ssl stuff from localhost visit this link…
http://phpcamp.net/toolbar/enabling-ssl-socket-transport-in-xampp-works-for-wamp-too
I got mine to work on WAMP following the directions
For anyone still struggling with the ssl stuff from localhost visit this link…
phpcamp.net/toolbar/enabling-ssl-socket-transport-in-xampp-works-for-wamp-too
I got mine to work on WAMP following the directions
This may have already been posted but I didn’t see it when I was trying to fix my smtp problem so here are the comments I have:
I ran into multiple issues when trying to put together an email in CI 1.7.2 using the smtp protocol. Here’s a synopsis of what worked for me (taken right from my coding comments):
* ATTENTION : When using the smtp protocol the format below seems to be required
*
* FIRST: Set up config array
*
* SECOND: Do single line initialization ($this->load->library(‘email’,$config))
* instead of using $this->email->initialize($config) after loading since
* some variables do not get set that way
*
* THIRD: Reset any parameters that do not get set, in the example below, I have to run
* the Email::set_mailtype() after the initialization because it doesn’t get set
As mentioned in the comments, setting the config with initialize after loading the email object doesn’t work with smtp. But when I initialized the config in the constructor, it worked. However, the mailtype was set to text (the default), so calling set_mailtype after the initialization allowed me to change it to html.
I hope this helps someone else. I lost a good deal of time on this bug.
This may have already been posted but I didn’t see it when I was trying to fix my smtp problem so here are the comments I have:
I ran into multiple issues when trying to put together an html email in CI 1.7.2 using the smtp protocol. Here’s a synopsis of what worked for me:
Setting the config with initialize after loading the email object doesn’t work with smtp. But when I initialized the config in the constructor, it worked. However, the mailtype was set to text (the default), so calling set_mailtype after the initialization allowed me to change it to html.
Apparently some config settings do not get set. Go figure.
Anybody know if it’s possible to tell if the file was relly attached to the email? Because $this->email->attach($file) doesn’t return true when the file sucessfully attached to the email…
Thanks!
Love it man!!!
Love your tutorials Jeffrey! Keep up the good work
Love your Codeigniter tutorials Jeffrey! Keep up the good work
This was just the lesson I was looking for. Thank you for all your tutorials.
Direct Link to the video:
Day 3: Sending Emails
http://a-mirror.video2.blip.tv/NETTUTS-CIDay3313.flv
Great tutorials, thanks dude.
Great tutorials, thank you