PHP Developers Network

sending email stored in DB
Page 1 of 1

Author:  misch09 [ Thu Jul 09, 2009 4:00 pm ]
Post subject:  sending email stored in DB


Today I started using swiftmailer with symfony and it looks very sexy; it's a Greate lib. But I have the following problem:

I am developing a webapp in symfony. And now I want to add a tell a friend form. My idea was to generate the email when the form is submitted and to safe it in the db.

In a cronjob I want to take the entry from the db and send it. Firstly I thought I can serialize the message object to put it in the db.
In the cronjob I wanted to unserialize the object, but this doesn't work.

Secondly I had the idea to store the mail source code retrieved from Swift_Message::toString() in the db. But now I see no way to send this source code. I see no way to give the finished mail source code to the Swift_Mailer object or to the Swift_Message object.

I want to store the finished email in the db because there are several places in my app which generate different mails. So I don't want to store the email fields(from, to, subject, ...) in the db.

Thankful for every hint,


Author:  jackpf [ Thu Jul 09, 2009 7:53 pm ]
Post subject:  Re: sending email stored in DB


Why don't you just send the email when they submit it?

Author:  misch09 [ Fri Jul 10, 2009 1:50 am ]
Post subject:  Re: sending email stored in DB

1. it' slowing down the response time of the form.
2. What about error management.
3. It's bad softeare design to mix several tasks in one. The job of the form is to take the submitted values, check them and generate the response.
4. What happends if I want the emails to be processed when the server is less in use by site visitors(at night).
5. If you have five places where you generate emails. I am sure you don't want to have five places where mails are sent.


Author:  jackpf [ Fri Jul 10, 2009 6:49 am ]
Post subject:  Re: sending email stored in DB

I honestly doubt a few hundred people using a mailing script would slow down your server at all...unless you've got a site with millions using at the same time or something.

But I'm not entirely sure of your can't retrieve them from the database?

Author:  misch09 [ Fri Jul 10, 2009 6:58 am ]
Post subject:  Re: sending email stored in DB


On my local server the process of sending the mail directly in the form takes two seconds.

I found a solution:

In the script processing the form I build the message object. Here I can also embed images and so on.
To the model of the db entry which has a blob column to store the message, I mad a function putMessage().
In this function I take all parameters from the message object via getTo(), getFrom(), ... and put it in a hash.
This hash gets serialized and saved in the blob.

In my sending batch file I use the retrieveMessage function of this model to get back swift_message. In this function I unserialize the hash and put all values back in a message object. setTo(), ...

It's not the finest solution but it works.


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group