Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

beforeSendPerformed event is called twice with memory spool #139

Closed
craue opened this issue Dec 16, 2011 · 10 comments
Closed

beforeSendPerformed event is called twice with memory spool #139

craue opened this issue Dec 16, 2011 · 10 comments

Comments

@craue
Copy link

craue commented Dec 16, 2011

Using the memory spool added in d44b7fc, beforeSendPerformed of a Swift_Events_SendListener is called twice.

@craue
Copy link
Author

craue commented Apr 16, 2012

@fabpot: Ping. Enabling it as mentioned in symfony/symfony-standard@4ab6de1, the issue still persists.

@craue
Copy link
Author

craue commented Apr 16, 2012

Here's a listener which demonstrates the issue:

<?php

class MyListener implements \Swift_Events_SendListener {

    private $timesCalled = 0;

    public function beforeSendPerformed(\Swift_Events_SendEvent $evt) {
        ++$this->timesCalled;
        $message = $evt->getMessage();
        $message->setBody($message->getBody() . $this->timesCalled);
    }

}

Because it's called twice, 12 will be appended to each message sent. Expected would be (and actually is, without memory spool) 1.

fabpot added a commit that referenced this issue Mar 14, 2015
…olas Appriou)

This PR was merged into the 5.3-dev branch.

Discussion
----------

fix issue #443 #139 spool send event result

Here is a PR for issues #443 and #139.

We've added a new result status in SendEvents with code 0x11 (RESULT_PENDING | RESULT_SPOOLED). It seems logical to us because this status lies between those two status.

The SpoolTransport now set RESULT_SPOOLED instead of RESULT_SUCCESS in order to be able to make the difference between a spooled mail and a sent mail.

Unit tests fails, but it wasn't due to anything that we've added. The master branch unit tests was already broken when we cloned the project.

Commits
-------

7b20f44 empty commit
33fa890 fix issue #443 #139 spool send event result
@craue
Copy link
Author

craue commented May 6, 2015

I'm also having this issue with file spool (using the current version 5.4.0). The event is triggered the 1st time when generating the mail and storing it in a file, then the 2nd time when it's actually being sent. I'm using this event to append some footer text to each mail, which is unfortunately done twice.

@fabpot: It's a rather old issue. Do you even consider this to be a bug (which will be fixed some day) or do I need to make up a workaround?

@craue
Copy link
Author

craue commented May 6, 2015

The status is RESULT_PENDING in both invokations, so I don't see a way to distinguish between spooling and sending.

@chris-testing-github
Copy link

I concur with @craue, in the beforeSendPerformed method the status is RESULT_PENDING in both instances so there is no way to differentiate.
I'm using a custom plugin with beforeSendPerformed to emogrify an email before it's sent. Unfortunately because of this (bug?) the process is happening twice and characters are encoded twice leading to undesirable characters.

@fabpot
Copy link
Member

fabpot commented Jun 16, 2015

Anyone willing to submit a PR?

@samdjstevens
Copy link

I am also experiencing this issue - would an acceptable fix be changing the event target that is dispatched from beforeSendPerformed to say beforeSpoolPerformed (and similar for sendPerformed) or would this be a BC?

@anyx
Copy link

anyx commented Mar 16, 2019

Do anyone knows how to avoid this issue now?

@cesurapp
Copy link

Do anyone knows how to avoid this issue now?

Look, maybe it helps https://github.com/appaydin/pd-mailer/blob/master/SwiftMailer/SendListener.php

@kiwidood
Copy link

I came across this issue today, the easiest solution I found was to create an array with the key/or value of $message->getId() and checking against that before doing anything with the message. Not sure if it's fool proof, or if it would work in all use cases but maybe this will help someone.

@fabpot fabpot closed this as completed Nov 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants