Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Sending E-Mails from Plugin

Hello everyone,
I wrote a Plugin. It's createing a checkbox inside the 'new discussion' form. If this checkbox is checked, a notification email should be send to all forum members (We're only 50 people, it's a small private project). This was working well, but since Version 2.6 the forum does only send this email to only one user (the first one of the database query result) - but I'm using a foreach, this confuses me.
My code is:

        $Users = Gdn::SQL()
        ->select('Email')
        ->from('User')
        ->where('Deleted',false)
        ->where('Verified',true)
        ->get();

            foreach ($Users as $User){
                $emailer = new Gdn_Email();
                $emailer->subject('Testsubject');
                $email = $emailer->getEmailTemplate();
                $email->setTitle('TestTitle')
                ->setLead($Discussion->Name)
                ->setMessage('Test Text.' . $out)
                ->setButton(externalUrl($DiscussionUrl), 'Read more', '#fff', '#ff0000')
                ->setFooter('Test Footer Text ');
                $emailer->setEmailTemplate($email);
                $emailer->to($User->Email);
                $emailer->send();
                $emailer->to('');
            }

Does anyone know, whats wrong here?

Thank you in advance
Tim

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    No, I do not know but I would change the content of the loop to make it more effective and maybe it already helps - who knows? You do not need to instantiate the mail class again and again.

                $emailer = new Gdn_Email();
    
                $emailer->subject('Testsubject');
                $email = $emailer->getEmailTemplate();
                $email->setTitle('TestTitle')
                    ->setLead($Discussion->Name)
                    ->setMessage('Test Text.' . $out)
                    ->setButton(externalUrl($DiscussionUrl), 'Read more', '#fff', '#ff0000')
                    ->setFooter('Test Footer Text ');
                $emailer->setEmailTemplate($email);
    
                foreach ($Users as $User){
                    $emailer->to($User->Email);
                    $emailer->send();
                }
    

    But if you do not use personal mail texts, I would do it without a loop:

    $Users = ... 
        ->get()
        ->resultArray();
    $recipients = array_column('Email', $Users);
    ...
    $emailer->to('[email protected]');
    $emailer->bcc($recipients);
    $emailer->send();
    
  • @R_J said:
    No, I do not know but I would change the content of the loop to make it more effective and maybe it already helps - who knows? You do not need to instantiate the mail class again and again.

    Hi R_J,
    yes I already did it like your suggestion and than I modified it to the way shown, but both ways are not working...

    But if you do not use personal mail texts, I would do it without a loop:

    $Users = ... 
        ->get()
        ->resultArray();
    $recipients = array_column('Email', $Users);
    ...
    $emailer->to('[email protected]yourforum');
    $emailer->bcc($recipients);
    $emailer->send();
    

    This is also not working. :-( The first user from the users table will get the E-Mail in BCC, but no one else.
    When I print the array to a page, it includes all user email adresses. :(

    Thanks, BR Tim

  • x00x00 MVP
    edited July 2

    have you consider this may be a restriction of the mail sever?

    many mail delivery companies and hosts are not happy to deliver transactional mail and use a number of strategies to prevent. Controlling how many email can be sent within n second is one strategy.

    What mail server are you using?

    grep is your friend.

  • This could be possible, but it was working great for a time... That's why i'm confused. I'm using a smtp server from my web hoster, nothing special I guess. I'll ask them if they're preventing that since a few weeks.

  • x00x00 MVP

    If you haven't changed anything that is the most likely. Many hosts do you support transactional mall. Even common email services like Gmail do not. This is common mistake people make, they assume that because they are give a SMTP solution or find a service they accept volumes of transactional mail.

    grep is your friend.

  • x00x00 MVP

    Even the services that do specialise in it can be tricky. Spark post is good but there can be a couple of false flags before they confirm you are legit. That mean they lock the account down including logging into the backend.

    grep is your friend.

  • x00x00 MVP

    Spark post will look at things like external URLs I'm emails

    grep is your friend.

  • timme2707timme2707 New
    edited July 3

    Ok, I understand, but if I use the bcc method this have to work in my opinion. Because that's only one E-Mail for the SMTP Server, or not?

    @timme2707 said:

    $Users = ... 
        ->get()
        ->resultArray();
    $recipients = array_column('Email', $Users);
    ...
    $emailer->to('[email protected]');
    $emailer->bcc($recipients);
    $emailer->send();
    

    This is also not working. :-( The first user from the users table will get the E-Mail in BCC, but no one else.
    When I print the array to a page, it includes all user email adresses. :(

    Sorry to bother you, but no one in BCC will get an email, only the one in $emailer->To
    Also tried to build a string with all adresses inside

    foreach($Users as $User)
    {
        $bcc .= $User['Email'].';';
    }
    

    If I print the var $bcc, the addresses will appear, so $bcc is not empty, but still no one will get a mail.
    If I set $emailer->Bcc('[email protected]'); I'll get an mail in bcc. >:)

    Summary:

    • send with foreach -> Not working
    • use array_column for bcc -> Not working
    • use string for bcc -> Not working
    • set a bcc-address manually -> working

    My Webhoster told me the following limits:

    • up to 15 connections at one time to the smtp server
    • up to 250 mails per hour

    I guess I'm far away from both limits!

  • You can just call call $emailer->bcc(); multiple times, no need to concatenate

    My themes: pure | minusbaseline - My plugins: CSSedit | HTMLedit | InfiniteScroll | BirthdayModule | [all] - PM me about customizations

Sign In or Register to comment.