Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getMessage method to access Swift_Message #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

markwilde
Copy link

I've added a method for read access to the Swift_Message instance. I needed it so I could reach into the transport adapter and pick out the Message-ID from the message once it had been delivered. Might be useful if there are any other headers etc to read once message has been sent.

I then was able to use the following to get what I needed from my controller once I'd delivered the message:

$message_id = null;

Mailer::applyFilter('deliver', function($self, $params, $chain) use (&$message_id) {
    extract($params);
    $result = $chain->next($self, $params, $chain);
    $message_id = $transport->getMessage()->getHeaders()->get('Message-ID')->getFieldBody();

    return $result;
});

Needed to store the message ID separately as Swiftmailer changes the ID on the message object after send.
@markwilde
Copy link
Author

I've slightly updated this as I noticed the MessageID recorded was different to the one received on email. This is because SwiftMailer changes it once sent. There is now a new method called getMessageId() which stores the ID before the send. It allows you to access it using the following update to the code above.

$message_id = null;

Mailer::applyFilter('deliver', function($self, $params, $chain) use (&$message_id) {
    extract($params);
    $result = $chain->next($self, $params, $chain);
    $message_id = $transport->getMessageId();
    return $result;
});

@mariuswilms
Copy link

Why not have deliver() return the final ID on success and false if delivery failed? This way there's no additional method needed.

@eLod
Copy link
Owner

eLod commented Jul 31, 2015

@markwilde not sure if i fully understand everything, but by looking at your code you set the $messageId before actually sending. also i don't really like the idea of having the last sent message sticked onto the transport. i would rather add filtering for creating the messages, also i'm open to discussion about the returned values from $transport->send(). i originally thought it would be nice to have back whatever the transport would return (e.g. this library is just a glue for the different adapters, which in turn depend on the mail sending php libraries - afaik swift for example returns the number of recipients), but it presents these kind of problems.

@markwilde
Copy link
Author

@eLod I needed to collect the message ID before the send in SwiftMailer because it seems to generate the message id on the object before send, and once sent it changes it to a new id. I personally think that @DavidPersson idea of setting the message id on return for each adapter would probably be better for now on each adapter as li3_mailer needs to have a standard return from the send function so it's predictable what is being returned. Maybe even the li3_mailer Message object being returned with all the information would be better?

@eLod
Copy link
Owner

eLod commented Aug 5, 2015

@markwilde what is changing the id? i don't think it's a good idea that the adapters return the message id, as you said, the message itself would be better (so you can look at the message id and any other attribute as well). that said the return value of the transport/send is still important, how else would you know if your message got sent actually (or know what error happened, if the return value indicates that, etc.)? the adapter could return you other information as well. so i would go with a composite return value.

@markwilde
Copy link
Author

@eLod I think it is SwiftMailer and from what I could find it's to do with how it handles bulk message sending. Maybe the li3_mailer message object could contain attributes for error handling to that similar to how models handle errors? That way you could interrogate the message object to find out if it's been sent etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants