Jump to content
Vayu Robins

Mail sendt too many times

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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? :-)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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! :-)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ngrmm
      i'm trying to attach a .ics-file to a mail
      The Page send sends out the email but only download the ics-file. Email has no attachments.
      // event ID $eventID = $input->get('eventID','int'); $event = $pages->get($eventID); // config $testEmail = $event->event_mail_test_adress; $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // .ics $filename = $event->name; header('Content-Encoding: UTF-8'); header('Content-type: text/calendar; charset=utf-8'); header("Content-Disposition: attachment; filename={$filename}.ics"); // fetch start date $event_start_ts = $event->getUnformatted("date_start"); // build the .ics data $ical_data = 'BEGIN:VCALENDAR'; $ical_data .= "\r\n"; $ical_data .= 'VERSION:2.0'; $ical_data .= "\r\n"; $ical_data .= 'CALSCALE:GREGORIAN'; $ical_data .= "\r\n"; $ical_data .= 'METHOD:PUBLISH'; $ical_data .= "\r\n"; $ical_data .= 'BEGIN:VEVENT'; $ical_data .= "\r\n"; $ical_data .= 'SUMMARY:'.$event->title; $ical_data .= "\r\n"; $ical_data .= 'UID:' . md5(uniqid(mt_rand(), true)) . '@'.$config->httpHost; $ical_data .= "\r\n"; $ical_data .= 'CLASS:PUBLIC'; $ical_data .= "\r\n"; $ical_data .= 'STATUS:CONFIRMED'; $ical_data .= "\r\n"; $ical_data .= 'DTSTART:'.date('Ymd', $event_start_ts).'T'.date("His", $event_start_ts); $ical_data .= "\r\n"; $ical_data .= 'DTSTAMP:'.date('Ymd').'T'.date('His'); $ical_data .= "\r\n"; $ical_data .= 'LOCATION:'.$event->venue; $ical_data .= "\r\n"; $ical_data .= 'URL:'.$event->httpUrl; $ical_data .= "\r\n"; $ical_data .= 'END:VEVENT'; $ical_data .= "\r\n"; $ical_data .= 'END:VCALENDAR'; // HTML BODY ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // send email $m = new WireMail(); $m->to($testEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->attachment($ical_data); $m->send();  
    • By ngrmm
      I have a page which sends out an email based on queries in the url
      <?php // event ID $eventID = $input->get('eventID','int'); $event = $pages->get($eventID); // config $adminEmail = "events@test.com"; $fromEmail = "noreply@test.com"; $fromName = "test"; $emailSubject = "Test Email"; // HTML BODY $emailBody = ""; // HOW TO ??? … // send mail $m = new WireMail(); $m->to($adminEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); ?> The email body is a bit complex: standard html/css tables and some php (pw variables).
      I put my emailbody (html) in a seperate file emailbody.inc but don't know how to include it?
       
    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By abdulqayyum
      hello processwire community,
      i am using LoginRegister module for front-end user registration. i am facing an issue verification email not receiving while WireMailSmtp is working perfectly and user receives message
      "Thank you, a confirmation code has been emailed to you. When you receive the email, click the link it contains, or paste the confirmation code below."
      received this message in front-end but does not receive any email relevant it.
      please help me in this case how i can solve it.
      Please check screen shots for SMTP test and message which receives user.
      Thanks AbdulQayyum,

      First screenshot SMTP test message, second when user receives message after clicking register button.
×
×
  • Create New...