Open db connection 1
get data for receipt
Close db connection 1
generate receipt
send email
open db connection 2
write success to database
close db connection 2
I guess you could speed this up a lot by doing it in bulk instead of opening and closing db connections twice for every email. Anyway, the version you wrote sounds reasonable for everything but really big operations to me, but then again I'm fairly junior.