0

Good day everyone, I'm looking for the best way to automate my purchases through my web app. Hope you can give a good advice or suggestion

Let's say we have one product, one user, and order table,

Product name is MusicMixes, product price is 50$, product attachment is Music.mp3 (with a size of 50mb)

User name is Juan and user email is juan@mail.com

Here's the current scenario/application that I have. I am using phpmailer() to send an email with attachments to buyers after a successful purchase using this syntax.

$mail->addAttachment("uploads/".$file_name);

Assuming the buyer bought MusicMixes product, the behavior of the process will be:

  1. The web app will get the User's name and email using session.
  2. The web app will fetch the data of the Product (incl. the Attachment) from database based from the Product ID/Name
  3. The web app will then send an email to the buyer, the structure is like this.

if(isset[post]){
  //get user data from session
  $name = $session[name]
  $email = $session[email]
  
  //get data based on id/name
  stmtSelect = select * from product where ID/Name=MusicMixes
  
  //Insert data to db
  stmtInsert = insert into order (name,email,productid,productname,productattachment,productprice)
  
  if(stmt execute){
    //send email with attachment
    $mail->Addattachement....
  }
  
  //Process is done
}

I'm encountering a long loading process before the process is submitted/done because I assume that the web app will fetch the attachment (big size, 50mb) and upload into phpmailer() and send it to the user. Typically, if the user refreshes/cancelled/did something to the browser while it is still loading. There are times that the attachments/email are not sent but the data is being saved into the DB.

My question is...

Is it possible like to save it first to database so the loading will be done instantly.

purchase.php = saves data to DB

then after some time maybe after 3-5 minutes, theres something (I can't figure it out) within the web app that will trigger mailSend.php

mailSend.php = sends the email w/ attachments based on DB.

There is somewhat another PHP file/other instances that will handle the mailing process to get the necessary info from the DB then send the email with attachments to the user? so that it doesn't affect the loading time/UX for the user. Something like its loading an email function, the backend is doing its thing without interrupting the user. I can't really explain myself so Hopefully you guys get what I meant.

Thanks for the big help!

  • 1
    This is what cron jobs are for – ADyson Oct 06 '22 at 06:47
  • Hi @ADyson, can you elaborate more on what you mean in cron jobs? Thank you so much! Need to edit. Something like this? https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – new developer Oct 06 '22 at 06:50
  • 1
    Instead of thinking about it as using a cron job to send the email directly, think about it as getting a cron job that checks to see if there are any scheduled tasks that it needs to do. Then create a database record that represents the thing that needs to be done (download something, send an email – a "job") that includes a status value or timestamp for when it has completed or fails. This way you separate the ** from loading your web pages. Most PHP frameworks have task scheduling built in, such as in [Laravel](https://laravel.com/docs/9.x/scheduling). – Synchro Oct 06 '22 at 07:14
  • Synchro said it for me, I was just about to type pretty much the same thing – ADyson Oct 06 '22 at 07:16
  • 1
    Another alternative would be to not add the file as an attachment, but rather send a link to the user where they can download the file. You could create a unique link (with some generated random token) to prevent people from finding the file. Or you could require them to login to download it (which is not an uncommon practice). That would also help if some user has a mail service that don't allow attachments of that size. – M. Eriksson Oct 06 '22 at 08:00
  • Thank you everyone for the idea! I will do all of this and check whether which is the best. – new developer Oct 06 '22 at 13:15

0 Answers0