Vayu Robins Posted June 10, 2015 Share Posted June 10, 2015 Hi. I have a setup that sends an email to all users that have subscribed to a category. When a post i published in a category, emails needs to be sent to all those users. For instance, I have 3 user subscribed, so 3 emails should be sent. However, 6 emails are sent and some of the emails are sent to 2 and 3 person at the same time. Please see attached file for what I mean. if( $page->template == "post" && !$page->isTrash() ){ if( $page->id && $page->isChanged( 'status' ) && !$page->is( Page::statusUnpublished ) ) { $from = wire('config')->adminEmail; foreach( wire( 'users' )->find( "roles=partner, location=$page->location, branch=$page->branch" ) as $user_data ){ $mail = wireMail(); $mail->to( array( $user_data->email => $user_data->contact_name ) ); $mail->from( $from, 'My Company' ); $mail->subject( 'Hi '.$user_data->contact_name.'<br>My subject' ); $mail->bodyHTML( 'My message' ); $mail->send(); } } } 1 Link to comment Share on other sites More sharing options...
tpr Posted June 10, 2015 Share Posted June 10, 2015 At first look, at this point you only add items (consecutively) to the existing mail object: $mail = wireMail(); So on first run: 1 email, on second run 1+1, on third 2+1, etc. Does it work using "new wireMail()" instead of "wireMail()"? Link to comment Share on other sites More sharing options...
Vayu Robins Posted June 10, 2015 Author Share Posted June 10, 2015 Thanks. I have now tested your suggestion, but now it doesn't send any emails. Link to comment Share on other sites More sharing options...
tpr Posted June 10, 2015 Share Posted June 10, 2015 It's perhaps this then: If no arguments are specified it simply returns a WireMail object https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/Functions.php#L751 So you should call wireMail with arguments (at the end inside the foreach). Link to comment Share on other sites More sharing options...
Vayu Robins Posted June 10, 2015 Author Share Posted June 10, 2015 Okay, I have now tried many different things, but no luck. I tried calling WireMail() and wireMail() at the end inside the foreach. Any other ideas? Link to comment Share on other sites More sharing options...
tpr Posted June 10, 2015 Share Posted June 10, 2015 This simplified example runs fine here, no double emails (using PW 2.6.1): $users = array( array( "contact_name" => "Mail-1", "email" => "mail1@yourmail.com" ), array( "contact_name" => "Mail-2", "email" => "mail2@yourmail.com" ) ); foreach( $users as $user_data ) { $to = $user_data['email']; $from = wire('config')->adminEmail; $subject = 'Hi '.$user_data["contact_name"].' My subject'; $body = 'My message'; $bodyHTML = 'My message'; wireMail($to, $from, $subject, $body, $bodyHTML); } Also, I can't figure out what was your goal with this line: $mail->to( array( $user_data->email => $user_data->contact_name ) ); Link to comment Share on other sites More sharing options...
Stikki Posted June 10, 2015 Share Posted June 10, 2015 It should reset $to on each iteration as you initialize wireMail over and over again, but if it doesn't try: $mail->to(); which should reset recipients list before adding more recipients into list. Link to comment Share on other sites More sharing options...
Vayu Robins Posted June 11, 2015 Author Share Posted June 11, 2015 Thanks tpr. I have testet with your code, but it still sends 3 emails. Two emails are sent to mail1@yourmail.com and one email is sent to mail2@yourmail.com. Please see attached screenshot. For some reason the $to variable is not reset on each loop. I cannot for the life of me understand why though. Could it be that the loop runs faster than wireMail() handles the send function? So maybe the loop runs twice before wireMail sends and the email adresses are added inside that function? Stikki: I have also testet with $mail->to(); but it doesn't help. Link to comment Share on other sites More sharing options...
Vayu Robins Posted June 11, 2015 Author Share Posted June 11, 2015 Okay, so maybe I have found the cause. I have been using the WireMailMandrill module to send mails with. So after uninstalling this module and instead installed the WireMailSmtp module, then the problem disappeared. I guess that it must have something to do with the WireMailMandrill module? What, I have no idea. Link to comment Share on other sites More sharing options...
tpr Posted June 11, 2015 Share Posted June 11, 2015 You should mention WireMailMandrill earlier, see this: https://processwire.com/talk/topic/5693-new-module-type-wiremail/?p=96011 Link to comment Share on other sites More sharing options...
Vayu Robins Posted June 11, 2015 Author Share Posted June 11, 2015 Yeah, I didn't think of mentioning it and had of course no clue that this is where the problem was. Anyways, thanks for helping, I appreciate it! Link to comment Share on other sites More sharing options...
Craig Posted June 11, 2015 Share Posted June 11, 2015 Hi, WireMailMandrill author here! Sorry about that - I'll have a look at this to try and find the problem Link to comment Share on other sites More sharing options...
Noel Boss Posted December 18, 2017 Share Posted December 18, 2017 Hi @Craig A Rodway Thank you for this magnificent module! Using it on a c9.io developer instance where I can not send mails directly and it works like a charm! One problem I've found tough – not sure if it's your module, how php inheritance works (not so much a php-guru myself) or the way pw handles hooks or just me being stupid; If $mail->send(); gets called inside module-code (like Ryans LoginRegister.module) the mails get sent trough your modiles ___send() method. When I call $mail->send inside a template file, it does not call your function but instead (i think) the standard wireMail->send method. I need to call $mail->___send() from the template. Then it works. My setup: # Template.php simplified: function invite() { // … // same result with wireMail(); $mail = wire('modules')->get('WireMailMandrill'); $mail->to($input->whitelist('email'), $invite->firstname.' '.$invite->lastname); $emailFrom = wire('config')->adminEmail; $mail->from($emailFrom, 'ICF Equip'); $mail->subject($subject); $mail->bodyHTML($message); // $mail->send(); does not call your method but instead wireMail->send // this works: $count = $mail->___send(); if ($count){ // … } } invite(); Any ideas why this might happen? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now