Save copy of outgoing messages in pickup directory

Discussion in 'General discussions' started by morpheus93, Feb 11, 2019.

  1. morpheus93

    morpheus93 Member

    Joined:
    May 5, 2017
    Messages:
    50
    Likes Received:
    3
    S.E:
    2019-11-24 22:51:43
    L.T:
    Regular
    L.C:
    2
    Hi,

    I need the possibility to save a copy of all outgoing messages (at least per campaign basis) in the MW pickup directory. The (autoresponder-) campaigns are live and sent out via SES delivery server. So I need a copy of them (as .eml file like I set up "Pickup Directory" as regular DS for his campaign) in the pickup directory to get some testfiles for developing an external app.

    Thank you for your appreciated help.
     
  2. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,264
    Likes Received:
    2,383
    @morpheus93 - knowing that this will affect performance, since it does write an email on disk, you can do this, create /apps/init-custom.php file with this content:
    PHP:
    <?php

    Yii
    ::app()->hooks->addFilter('console_command_send_campaigns_before_send_to_subscriber', function($emailParams$campaign$subscriber$customer$server){

       
    // create the .eml message
       
    $message  $server->getMailer()->getEmailMessage($emailParams);
       
       
    // this is the path on disk where we will store it, make sure the dir exists, change accordingly, etc
       
    $filePath '/tmp/emails/' $server->getMailer()->getEmailMessageId() . '.eml';

       
    // and store it
       
    @file_put_contents($filePath$message);
       
       
    // and return the data back to send campaign command
       
    return $emailParams;
    });
     
  3. morpheus93

    morpheus93 Member

    Joined:
    May 5, 2017
    Messages:
    50
    Likes Received:
    3
    S.E:
    2019-11-24 22:51:43
    L.T:
    Regular
    L.C:
    2
    @twisted1919

    Thank you very much for your appreciated help.

    I added the code from above to a file named init-custom.php within my MW /apps folder (where also the init.php is located). As $filePath for saving the emails to .eml files I added the absolute path $filePath = '/var/www/web5/files/mw_out_copy/' . $server->getMailer()->getEmailMessageId() . '.eml'; (beginning with my server's mainpath /). MailWizz is installed in a folder named 'mw' under web5 so the absolute path is /var/www/web5/html/mw. But it doesn't save any email message (.eml file) in the "mw_out_copy" folder.

    Maybe I got something wrong or could it be that your solutions doesn't work with the 1.4.3 version of MW I'm currently using?

    Would be great if you can help me to get this working.
     
  4. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,264
    Likes Received:
    2,383
    @morpheus93 - Add the code here so i can have a look.
    Also, try to add some echo statements in it to see what happens when you send the campaign, to see exactly how your code is getting executed.
     
  5. morpheus93

    morpheus93 Member

    Joined:
    May 5, 2017
    Messages:
    50
    Likes Received:
    3
    S.E:
    2019-11-24 22:51:43
    L.T:
    Regular
    L.C:
    2
    @twisted1919

    Okay thank you.

    Here is the code I added:

    PHP:
    <?php

    Yii
    ::app()->hooks->addFilter('console_command_send_campaigns_before_send_to_subscriber', function($emailParams$campaign$subscriber$customer$server){

       
    // create the .eml message
       
    $message  $server->getMailer()->getEmailMessage($emailParams);
     
       
    // this is the path on disk where we will store it, make sure the dir exists, change accordingly, etc
       
    $filePath '/var/www/web9/files/mw_out_copy/' $server->getMailer()->getEmailMessageId() . '.eml';

       
    // and store it
       
    @file_put_contents($filePath$message);
     
       
    // and return the data back to send campaign command
       
    return $emailParams;
    });
    As I see from the /backend/index.php/misc/campaigns-delivery-logs when I add the code above in the init-custom.php file MW seems not to send emails at all, so simply stops sending out (I didn't change anything regarding DS).
     
  6. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,264
    Likes Received:
    2,383
    Here's the correct version, my bad, i hooked in the wrong filter :-s
    PHP:
    <?php

    Yii
    ::app()->hooks->addFilter('delivery_server_before_send_email', function($emailParams$server) {
       
       
    // create the .eml message
       
    $message  $server->getMailer()->getEmailMessage($emailParams);
       
       
    // this is the path on disk where we will store it
       
    $filePath '/var/www/web9/files/mw_out_copy/' $server->getMailer()->getEmailMessageId() . '.eml';
       
       
    // and store it
       
    @file_put_contents($filePath$message);
       
       
    // and return the data back to send campaign command
       
    return $emailParams;
    });
     
    morpheus93 likes this.
  7. morpheus93

    morpheus93 Member

    Joined:
    May 5, 2017
    Messages:
    50
    Likes Received:
    3
    S.E:
    2019-11-24 22:51:43
    L.T:
    Regular
    L.C:
    2
    @twisted1919

    Thank you for your appreciated help. Now it seems to work.
     
  8. frm.mwz

    frm.mwz Well-Known Member

    Joined:
    Mar 8, 2016
    Messages:
    3,707
    Likes Received:
    677
    S.E:
    2019-11-27 02:17:39
    L.T:
    Regular
    L.C:
    7
    Perhaps make this a feature?
    Writing to disk these days with most servers having ssd is a snap ;)
     
  9. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,264
    Likes Received:
    2,383
    @frm.mwz - it is a feature already, is called pickup directory delivery server type.
     
  10. frm.mwz

    frm.mwz Well-Known Member

    Joined:
    Mar 8, 2016
    Messages:
    3,707
    Likes Received:
    677
    S.E:
    2019-11-27 02:17:39
    L.T:
    Regular
    L.C:
    7
    Of course, generally, the DS pickup dir is a feature...
    ...but the thread was about "Save copy of outgoing messages in pickup directory", as in copy for backup purposes, not to be sent (and then it is gone, hence no copy)...
    ...and that is why you had the extra code above...
    ...and !this! is what i addressed when i wrote "Perhaps make this a feature?"
    ;)
     
  11. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,264
    Likes Received:
    2,383
    gotcha. well, i don't know, as you can see, implementing for whom needs it is very simple, so i don't think we'll make a feature out of this.
     
  12. frm.mwz

    frm.mwz Well-Known Member

    Joined:
    Mar 8, 2016
    Messages:
    3,707
    Likes Received:
    677
    S.E:
    2019-11-27 02:17:39
    L.T:
    Regular
    L.C:
    7
    ...i thought since the code is ready above, just throw in the option to choose a folder and viola, another feature added...thx in any case for the above code! :)
     

Share This Page