CodeIgniter Forums
Sending mails (and using cron with CI) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Sending mails (and using cron with CI) (/showthread.php?tid=69586)



Sending mails (and using cron with CI) - glorsh66 - 12-18-2017

I am writhing my own user authorization library, and mostly it's going fine (i will share final version on github)

But i have noticed - quite a lag when i sending mail to someone.
For instance - i made a registration form (controller) at the end of which placed mail sending command $this->email->send();
And it can take a while before script is executed (so user see is as page is loading)

is it a good idea to send email in the same script or do i need a make special table in database (pending messages) and just run special "mail script" which is supposed to get all pending messages and send them in the background 
(And run in trough the cron)
And following question - is it possible to use CI (i wanna use CI libraries) with cron? How am i suppose to start it? (because CI start with index.php, not directly from controller..)


RE: Sending mails (and using cron with CI) - skunkbad - 12-18-2017

I always use an email queue in the database. It has many advantages, including resending, and verification that email was sent.

See this for the answer to your second question:

https://www.codeigniter.com/userguide3/general/cli.html


RE: Sending mails (and using cron with CI) - Waschi - 12-18-2017

Hi glorsh66,

Quote:is it a good idea to send email in the same script or do i need a make special table in database
It depends, but long running tasks should not blocking the user-interface if its possible.
So yes you could outsource the email-sending. I also would recommand to use a third-party application for email sending like Mailgun.
In that way, you dont have to worry about loading-times/performance issues or scaling in your email-sending.
You get also a direct response, so the user dont have to wait and you dont need to code extra tables etc.
That would be my suggestion.

You can also build a cronjob for this, but than the email would not send in real-time and the user has to wait for that mail, till the cron runs.

Another way, would be to use a message-broker like activemq and schedule the jobs to an consumer, but that might be to way to much, for just sending a registration mail.

Quote:is it possible to use CI (i wanna use CI libraries) with cron? How am i suppose to start it? (because CI start with index.php, not directly from controller..)
Yes thats possible.
I have some cronjobs on my server. I simply call that controll that way:
PHP Code:
/usr/bin/php /var/www/html/yourwebsite/index.php controllername functionname 



RE: Sending mails (and using cron with CI) - glorsh66 - 12-18-2017

Thaks for advice! Really apreciate it!


RE: Sending mails (and using cron with CI) - glorsh66 - 12-18-2017

(12-18-2017, 09:10 AM)skunkbad Wrote: I always use an email queue in the database. It has many advantages, including resending, and verification that email was sent.

See this for the answer to your second question:

https://www.codeigniter.com/userguide3/general/cli.html

Btw - how make a CLI controller private - i mean not accessible from web?
I can only imagine a crutch - make a secret parameter and check it on the firs line of script.


RE: Sending mails (and using cron with CI) - skunkbad - 12-18-2017

(12-18-2017, 12:30 PM)glorsh66 Wrote:
(12-18-2017, 09:10 AM)skunkbad Wrote: I always use an email queue in the database. It has many advantages, including resending, and verification that email was sent.

See this for the answer to your second question:

https://www.codeigniter.com/userguide3/general/cli.html

Btw - how make a CLI controller private - i mean not accessible from web?
I can only imagine a crutch - make a secret parameter and check it on the firs line of script.

Read the documentation for common functions:
https://www.codeigniter.com/userguide3/general/common_functions.html#is_cli


RE: Sending mails (and using cron with CI) - InsiteFX - 12-19-2017

Check the ip address and only allow connections from it...