4

I'm building a Symfony 2 web application. My e-mails are sent via Swiftmailer. Since in the last version of my web app, I logged all e-mails sent by the mailer class/function in the database to keep track (see if the system works and have a certain proof that my e-mail was at least sent), I wanted to do the same in this version. At that time, I was using PHPMailer which I wrapped in a function to include the PDO calls. Using Symfony and Swiftmailer now, I wondered how I could easily log all the e-mails in my database with, of course, doctrine ORM. I guess the easiest thing would be to log it manually each time I send an e-mail, but I want it to be done automatically since I will be sending a whole lot of e-mails. I also want my class afterwards to be as flexible as Swiftmailer is, so wrapping everything in a "simple function" is not an alternative.

A first idea I had, was to extend the Swiftmailer class and add a custom send method that internally calls the herited send()-method. The issue with that is, that I do not exactly know where to place that class and also, I would need to connect or call it via custom services since the build-in service uses the Swiftmailer itself, wouldn't I?

In addition to that, there is the issue that e-mails are maybe spooled and in that case, send() does not give you feedback, if the e-mail has really been send. Or do I have a misunderstanding of way that works?

Did anybody else have a similar issue/request? If so, how did you solve it?

Thank you.

Lozik
  • 149
  • 1
  • 9
  • 2
    maybe interesting? http://stackoverflow.com/questions/18033210/logging-swiftmailer-send-activity-in-symfony2. Symfony provides `events` to quite a lot of things? – Ryan Vincent Sep 24 '16 at 15:38
  • 1
    Try to see if [this](http://symfony.com/doc/current/service_container/service_decoration.html) approach can be usefull – Matteo Sep 24 '16 at 18:55
  • Thank you, both, for your answers. Events seem to be a good idea, but only if the event listener gains full access to the mailer information (the Swiftmail object). Otherwise I do not have all information to save. Also interesting idea with the decoration. I will have a detailed look at your links soon. From what I've seen, there seems not to have been any use with doctrine yet. If I have a concrete solution to my issue, I will post a detailed answer for others to understand the idea. – Lozik Oct 01 '16 at 12:35

2 Answers2

2

Ok, I've now found a bundle, the Swiftmailer-logger-bundle, that solves my issue. https://github.com/tweedegolf/swiftmailer-logger-bundle

For those who have a similar issue: Have a look at that bundle. If it does not fit your needs, it will at least explain how to use the swiftmailer events.

Lozik
  • 149
  • 1
  • 9
1

There's a couple of approaches you could take here. First approach is as you said, persist email to the database and send email from the database and see if it was sent that way. The Automailer bundle does that for you.

I wouldn't recommend that approach as you'd need to maintain that table of data that can expand quickly and easily. You're also probably going to need to maintain an MTA.

What you're probably more interested in is if the email was received by the end user. If that's what you're trying to find out I would recommend using a transactional mail service such as Mandrill or Sendgrid. The reasons I'd recommend this are.

  1. You don't need to operate an MTA
  2. You don't need to worry about storing your email locally.
  3. They have an API that makes sending tractional email very simple
  4. They have API's that make it trivial to find out if the message you sent was received.
Rob Jensen
  • 261
  • 2
  • 6
  • Thank you for your answer but that is not exactly what I am trying to do. I do want to use Swiftmail for a number of reasons. I do not need an external API or anything. I'm perfectly fine with my server and my working mail server. For your idea with persisting/spooling e-mails in the database would be a way to do it but I need a possibility to send e-mails instantly. But I will keep that idea in mind, maybe as fallback method. – Lozik Oct 01 '16 at 12:25