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.
Reply-To: someone@somehost
Sender: someone <someone@somehost>
To: [email protected]
Subject: some subject
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
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 [email protected]
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.