You don't mention what $mailprog is, but
assuming it's sendmail you might want to investigate the
-odq option. If sendmail is running as a
daemon on your system (look for something like
/usr/lib/sendmail -bd -q15m in the process
list), adding that option will not let the mail be
delivered directly but instead queues it up for the
next regularly scheduled time that sendmail will run the
queue (e.g. -q15m means every 15 minutes).
I'm not a sendmail guru, but it's my
understanding that this will be more resource-friendly
than starting up a synchronous sendmail process for each
outgoing email.