Solved

php code architecture thoughts and suggestions

Posted on 2003-10-31
6
282 Views
Last Modified: 2012-05-04
I am working on a php project. Currently I have a php script that display a fair amount of text, email addresses atec on a page. The user has an option to edit it. If they do, a mail script which I have written is called and it parses a mail together and sends it out using the PHPMailer class.

after the mail is sent the user is moved back to another page.

This system is used to track work (here at work) and is used very often and many emails are sent from it. All the data is stored in a mysql database.

Here is my dilemma:
When a user sends out email, the script does not return until my script has parsed the message together and theclass file has connected to the mail server and sent the message. This can cause a very noticiable delay: 2-10 seconds usually, and if there are a few attachments, or large attachments, much longer.

What I would like to discuss:
How I can move this load of off the users browser and have "the server" take care of it.

Two ways I have thought of so far are :





having a php script which looks for some entry in the database and when it sees it, shoot off a message and change that value in the db. This would be done using a script that is run say every 5 minutes on the server via a batch file??




Secondly, which would probably be more preferrable is to have the mysql database call a php script via a command window when a certain value is changed (i.e a trigger function).


Now, i know mysql does not yet have triggers, but maybe some of my fellow coders here have already thought of a way around this or would like to think of a way.



3rdly, I would love to hear any other ideas anyone has on this topic.



Thanks alot,
Anthony
0
Comment
Question by:lazyelm
6 Comments
 
LVL 3

Expert Comment

by:ashoooo
ID: 9659843
You can create cron jobs to run, say, every 5 mins.

You can save all the mail requests in a db. The script would return as soon as it has made the changes to the db.

The cron job would then go through all the records and send emails and delete the records from this table.

0
 
LVL 6

Accepted Solution

by:
aolXFT earned 500 total points
ID: 9659869
Hmmm The only way I can see to solve your problem lies, in writing your own custom mail function, for either PHP, or MySQL.

This would depend on the sustem you are using, eg Linux, Windows, etc, but the main idea would involve spawning another process, and using it to send the mail, using the fork() command.
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9659897
Then you could let the main process get on with the job, and let the spawned one, send the mail. I don't particularly recommend doing this with PHP, as an apache(or other WebServer) module, though, because I don't have the necessary experience with apache programming to make such a recommendation.

The same would apply to a MySQL Operation. The problem is that you have no way to check for errors, because you would have to wait until the mail-sending is finished, before you see if it worked.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:ashoooo
ID: 9659961
I would recommend using cron jobs in linux/unix and at commands in windows to do the mailing job. Your PHP script should return as soon as it makes changes to the DB.

If at a later time, the mailing fails, you can notify the user using.. well.. another email :) or display it on the next page he visits.

Depends on your architecture....
0
 

Expert Comment

by:msknr
ID: 9660824
0
 
LVL 11

Expert Comment

by:shmert
ID: 9739825
Just posted this in the other thread, too... (actually accidentally double-posted :P)

Try this syntax to get the php call to system() to return _immediately_

system('/usr/local/bin/long_script > /tmp/php_out &');

This redirect output to the /tmp/php_out file, and the ampersand tells it to run in the background.  Both of these steps are essential.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now