Nest your script.
To send an email there are a number of options. I personally favour msmtp for this, you can define the headers yourself "here" style (inline) or use a separate file and cat them together. There are all sorts of alternatives to this involving perl, python etc.
Sender: someone <someone@somehost>
Subject: some subject
Content-Type: text/html; charset="iso-8859-1"
The trailing blank line is important.
cat your assembled message into msmtp thusly :
cat assembled.eml | msmtp firstname.lastname@example.org
A pessimist would loop until success a reasonable number of times.
The payload could also be html, and could be php generated.
A very ugly alternative for "batchy" operation overnight say is to create a cronjob and the output is emailed in lieu of stdout/stderr.