Jump to content

Recommended Posts

Posted

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();
		}	
	}
}

post-3258-0-07319200-1433965890_thumb.jp

  • Like 1
Posted

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()"?

Posted

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 ) );
Posted

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.

Posted

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.

post-3258-0-07600300-1434011440_thumb.jp

Posted

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.

Posted

Hi, WireMailMandrill author here! Sorry about that - I'll have a look at this to try and find the problem :)

  • 2 years later...
Posted

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?

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...