0

im building a photobooth app whereby a user can take photos and email those photos to themselves, i use phpmailer smtp, the only parameters i pass to phpmailer are the email and the photo names ( because i saved the photos in the server folder right after the photo is taken, therefore i just need to handle the photo name).

the process goes like this: after taking photos, the user enters and submit their email and selected photos by clicking 'send' to send the photos to their email, it will then pass these parameters to the phpmailer to send the photos, and then goes to a thank you page. however, when the user clicks 'send', the process to connect to gmail and attach the photos takes a very long time and the UI is on a loading state, therefore i tried different ways to run the phpmailer process: like open the thank you page after click 'send' and then running phpmailer in a new tab (but i cannot keep the tab focus on the thank you page since js disabled opening a new tab in the background); or after click 'send', redirect to phpmailer but run the thank you page first (however phpmailer will still run the slow email process first before opening the thank you page).

any methods that are simple to use and can help to solve the slow email process? as long as it doesnt affect the UI whereby user needs to wait for the email process to complete before doing anything else, then i will be happy to try

ADD ON: it has to be a public email because my client is not gonna use their corporate side to operate this as there's too many security procedures, and btw im doing this on a windows system. BTW, as a photobooth users, people would want their photos as soon as possible, and using queue and cron is hard to inform the user when the message is sent, therefore that may not be a feasible solution

ADD ON: i cannot store user's email address in any db due to security, so i cannot store the emails somewhere and then send them later using any task scheduling method

Edgar Lee
  • 3
  • 3
  • 1
    Does this answer your question? [How can I run a PHP script in the background after a form is submitted?](https://stackoverflow.com/questions/4626860/how-can-i-run-a-php-script-in-the-background-after-a-form-is-submitted) – Justinas Dec 16 '19 at 08:37
  • Does this answer your question https://stackoverflow.com/questions/23191522/how-to-send-emails-using-phpmailer-in-the-background – Hitesh Tripathi Dec 23 '19 at 08:46

1 Answers1

1

SMTP is generally not safe to use during web page processing as it is often unacceptably slow, as you're finding. The solution is to not send directly to remote servers, but to queue locally, which is usually predictably and reliably fast. The most straightforward way to do this is to install a local mail server, such as postfix, and send your messages via SMTP to localhost. This will usually submit messages in a few hundredths of a second, plenty fast enough for most purposes. On most Linux distributions this is no more complex than apt install postfix, and then simply calling $mail->isSMTP() (with no other SMTP settings - defaults should work) in your PHPMailer script.

A more complex approach is to store the fact that you need to send a message in a queue (which is essentially instant), then use a separate process or scheduled task (cron) to pick up that info and send the message. This decouples the sending from your page processing, making it fully asynchronous. The only problem here is that it becomes more difficult to inform the user that their message has been sent.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • the problem is that my client says it has to be a public email because they not gonna use their corporate side to operate this as there's too many security procedures, and btw im doing this on a windows system. as a photobooth users, people would want their photos as soon as possible, and like you said about using queue and cron, its hard to inform the user when the message is sent, therefore that may not be a feasible solution – Edgar Lee Dec 17 '19 at 03:43