Use the console component of Yii2. Here is a decent intro for using the console
You set up your function as you would if it were on the web application. Build and generate your email and then set a bash script to run on a cron, which calls your function and generates and sends the email. You can log errors and success of the script also. I send them to my email.
Set up you confiruation for DB and anything else using config/console.php
that's important.
Your controllers are in commands
folder.
Take a look at https://github.com/yiisoft/yii2-app-basic/blob/master/commands/HelloController.php
Call this file through the console and it will return the message param.
When you build your function to send emails, get it to send you an email with success/failure. I always build my controllers in the application first to check that they work, then migrate them into the commands.
More help on the console is here