CodeIgniter Forums

Full Version: Sending mails (and using cron with CI)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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..)
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
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 
Thaks for advice! Really apreciate it!
(12-18-2017, 10: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.
(12-18-2017, 01:30 PM)glorsh66 Wrote: [ -> ]
(12-18-2017, 10: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/g...tml#is_cli
Check the ip address and only allow connections from it...