1

I want to send a welcome email when a user registers in my app. When a user registers he gets redirected to his profile page. I tried sending email while user creation but the email() takes 7 seconds to send email, and the page waits till then and then redirects the user to profile after 7 sec.

This is not desired. I want to redirect user as soon he registers, and send an email along the process. It takes 7 sec don't know why. i tried it online on godaddy and hostgator account as well as on my localhost.

BTW: i am using PHPMailer to send email.

How can can i make a standalone process which on invoke calls my sendMail.php with email $_POST[] parameters {to, subject, body}.

i though ajax call will do the trick, but as soon as my page redirects from registration to profile, the email script stops. I tried this code:

<script language="JavaScript">
    $.post( "sendMail.php", { to: "$to", subject: "$subject", htmlBody: "$htmlBody", altBody: "$altBody" } );
location.href=profile.html
</script>

Please help, i searched a lot but they work on shell which i am not, and other solutions were unix/linux based. i want to make it work on xampp as well as godaddy linux shared hosting, with NO ssh access.

ADi
  • 219
  • 1
  • 5
  • 17
  • 1
    You're best bet is to setup a job server and pass these things off async. – wesside Dec 13 '13 at 15:42
  • Put the redirect in the success function of your ajax call. Like : $.post( "sendMail.php", { to: "$to", subject: "$subject", htmlBody: "$htmlBody", altBody: "$altBody" }, function(){location.href=profile.html;} ); – FLX Dec 13 '13 at 15:44
  • Have a look at http://php.net/ignore_user_abort to let your PHP-script running at the moment your browser disconnects – Peter van der Wal Dec 13 '13 at 15:45
  • Depending on the PHP server configuration (that is: will flush() work), you can kill the connection with the browser and continue your script. See [this question](http://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection) – Michel Dec 13 '13 at 15:51
  • @FC ur solution is okay, but it does not solve my problem. the redirect will happen after 7 secs. – ADi Dec 13 '13 at 17:17
  • @Peter, i tried ignore_user_abort(true) in some other scenrio, it does work, but i don't want to use ajax as its a security whole and i have to rely on client side. i desire as server end solution. thoughi have not tried the abort method with ajax, will try that and respond but i will look for server side solution, not going to depend on ajax. – ADi Dec 13 '13 at 17:21

3 Answers3

0

You could try to put the javascript in your profile page so it runs once they hit the profile page and not on the form submit. Just need to check if the welcome email has been sent already in you sendMail.php script.

But using this script to send an email may not a very good idea as it could enable a malicious user to send an email with what ever content they wanted to whoever they wanted and if it was marked as spam then it would be your server that got blocked. It is a very common technique used by "spammers".

You would have to be very careful in how you handeled the email before it was sent so as not to breach your agreement with your hosting company.

Chris
  • 435
  • 4
  • 11
  • Very good point u mentioned, i didn't thought of the security hole. Thanks for this. i also thought sending email from client side call is not suit able. but i don't want the script to be depandand on anything, if i send email from profile page. the user may redirect to other page or close browser, hence same issue as i currently have. – ADi Dec 13 '13 at 16:58
  • Then you could after they click "save" redirect them to a blank page that has the script to send the welcome email, but also using include or require, add what ever page you want to so they get something displayed to them, then when they login next time they just go to the normal profile page or what ever. – Chris Dec 13 '13 at 17:06
0

Php is single threading language. This means that you can not start a job before previus one executed (and finished)...

You may change task order. I mean you may send the email, after welcome page completely rendered.

Try with this order;

1) register user

2) show welcome page,

3) send welcome email....

with this order, your new user will not wait for 7 seconds before see welcome page.

thomas
  • 331
  • 2
  • 8
  • thanks for ur input, but the problem is i don't want the email system to be defendant. The user may redirect to any other page or close the browser. hence the script will not complete. user will have to wait 7 sec on welcome page to complete the email process – ADi Dec 13 '13 at 17:03
  • yes, but it may happen even if you send with ajax. if the user close or redirect another page, its not important how you send the email. the task will be terminated... maybe you try to register a cronjob with php for sending email (registering a cronjob takes under a second), and then render the welcome page. (and after sending email, delete the cronjob) – thomas Dec 13 '13 at 17:20
  • yes cron job seems a good idea, i just don't know how to make one in php yet, will google it now. Thanks. if i have got answer specifically according to my problem, that would have gave me many solutions to my task, i may use same system when a purchase is done and send an email. also i might have use the system to do some task behind when a certain event occurred. this standalone process system is very useful and i'm still looking for such implementation. but kguest has provided a good souyrce for my current issue. trying it. – ADi Dec 13 '13 at 17:51
0

You could use Mail_Queue, which is a job queue specifically for sending emails, or utilise Zend_Queue [ZF1, ZF2] to write something customised to work with PHPMailer. You might even consider using Gearman.

kguest
  • 3,804
  • 3
  • 29
  • 31
  • can i use REST api to make async calls to send mail like with nusoap library? gearman documentation is not very friendly to me at least, and other tutorials are not helpful much, it requires a worker server to be run all the time, which is not what i though to be used i didn't understood mail_queue example. it contains require_once "Mail/Queue.php"; in example code and doesn't mention where they got those files from and what it contains. – ADi Dec 13 '13 at 19:17
  • You would need to set up some form of cronjob in the case of Mail_Queue or Zend_Queue; or you could try a RESTful API utilising nusoap, phpmailer or whatever. You would need to either install the Mail_Queue package via the PEAR installer or download the files from http://pear.php.net/package/Mail_Queue/download – kguest Dec 15 '13 at 11:06