Schedule an automatic email sent every week to inform a person


Before starting development I wonder how to create a small feature.

Should I create a plugin or code directly? If yes I place my code in the site.php controller?

The context: it’s a cycling club that goes out every Sunday. The idea is to send an email to the city hall, automatically (for example on Mondays) so that it displays the correct text on the city’s illuminated panel each week.

Currently this manipulation is done by hand. There are risks of error and forgetfulness.

We have a base of the next outings, we should take the next Sunday outings.

Example: Course on Sunday February 5, 2023 (60 km, 90 km).

I think we also have to think about managing the cases where there would be no course for the coming Sunday. Either not to send an email, or to send an email to say that there is nothing to display.

For the email to be stored, the ideal would be to be able to modify it from the panel, in the event of a change (even if it shouldn’t happen) and provide people’s emails in copy.

I’m thinking of using a cron to send the message automatically every week.

Thank you for your ideas.

Yes, that would be the way to go. Your cron can then call a custom route that sends the email. This route can get the data needed from your content. You can send the email conditionally, depending on whether an event is scheduled for the upcoming sunday or exit if not.

Thank you for confirmation.

So I’m thinking of doing 2 things:
create in the panel, a field A for sending to the right person. Then a CC field for sending a copy to one or more people in the club.

It might be good if I write the template of the message in the panel to show the template of the next send (maybe just an “info” field that would be personalized).

Can I make a query to fetch the right info on the right date from the content folder? It should automatically browse the “content/6_sorties/3_prochaines-sorties/1_dominicales” folder and get me the data for the date of the previous Monday.

The data is stored, if I take an example in:

As we plan outings in advance, they are already planned until May 2023.

I have a date in

Do I have to go through all the folders in descending order so that it looks for the next output based on the upcoming Sunday?

To date I would have to browse 14 folders to read the files

But there could very well be 100 Sundays for the whole year, if the club decides to plan the whole year at once.

Isn’t that too resource intensive?

I’d use findBy('date', '2023-02-05'), with the date to be searched calculated as the next Sunday from the day of sending the mail (or if send on Monday, that would be current date + 6). You can also filter by date > today first.

ok, thank you, i will put my code when i start the development