• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Set recipients mail address in a php plugin for groupmail

Hello,

On the website we have a function to send group mails to employees, users or customers. I did a lot of rewrite and now its possible to send emails only to people who have the same company_id (bedrijf_id) of the logged in employee / user. So thats very good. This is created in a list element calc.

Now in the groupmail form there is a plugin php in where there is a lot of code to send the emails out in a good way. I was looking for a way to add a recipient email adress what should be the email address of the company (bedrijf_id) of the sender.

I did find this piece of code: "

// Maak een mailobject
$mailer = JFactory::getMailer();
// Voeg afzender, ontvanger, reply-to toe in het object
$mailer->addRecipient("somename@gmail.com");
$mailer->setSender($mail_van);
$mailer->addReplyTo('somename@gmail.com', "Stichting Jasper");


So when the email is sended and the receiver presses reply, then this address is showing in the receiver field of the email to send. In a fancy way as you can see at the enclosed image. groupmail.png

I wrote a query to grab the email and i want to put it in the $mailer fields. So :

$q = $db->getQuery(true);
$q
->select('email_adres_bedrijf')
->from('egocentralis_bedrijven')
->where('bedrijf_id IN (SELECT bedrijf_id FROM g4e_orakel_personeel WHERE userid = {$my->id})');
$db->setQuery($q);
$mails = $db->loadObjectList();


$mails should return the company email address of the logged in user.

  1. I am not totally sure, if the php in the form, will notice that for example I am the logged in User. The list on the frontend notice it. Does the php notice its the logged in user?
So I tried to put the $mails into the place of the email addresses.

$mailer = JFactory::getMailer();
// Voeg afzender, ontvanger, reply-to toe in het object
$mailer->addRecipient("
return $mails");
$mailer->setSender($mail_van);
$mailer->addReplyTo('return $mails', "Stichting Jasper");


Unfortunately that is not working. So i tried to use "" and '' and () and [] but with no luck :(

How can i get the value of $mails into that position, or what for approach is better to use?
And if the php plugin in the form settings off the groupmail, cannot recognise the logged in user... how can i solve that matter??

Hopefully someone can point me in the good direction.
 
The addRecipient function takes one or two parameters. The first is the actual email address and the optional second is the text name of the recipient. You can call this function once for each address you wish to add. So if $mails is an array then you need to loop through each element and add them as recipients.
 
I am sorry but i do not understand what you try to explain to me.

Can you tell me what you try to explain?

Because when i have to add all different recipients manually we speak about hundreds of recipients in the future... so I need it to be automated by a query.
 
$mails is an array of objects based on using the loadObjectList function. You will need to do something like this:
Code:
foreach ($mails as $mail) {
  $mailer->addRecipient($mail->email_adres_bedrif);
}
 
Ofcourse.. an array... stupid, missed this another time also.. Thank you.. works like a charm. I am very Happy.. and i promisse never forget this array thing again.
 
ok,

If i set $mailer->addReplyTo($mail->email_adres_bedrijf, $bedrijfsnaam); and after receiving the email and i press on reply, the email address is transformed in the company name. company name.png

But if I set $mailer->setSender($mail->email_adres_bedrijf, $bedrijfsnaam); and after receiving the email the sender information is showing the email and not the company name. company name 2.png

How can i change this?
 
ok,

If i set $mailer->addReplyTo($mail->email_adres_bedrijf, $bedrijfsnaam); and after receiving the email and i press on reply, the email address is transformed in the company name. View attachment 19007

But if I set $mailer->setSender($mail->email_adres_bedrijf, $bedrijfsnaam); and after receiving the email the sender information is showing the email and not the company name. View attachment 19009

How can i change this?
Have you checked whether $bedrijfsnaam actually has what you expect it to have? Try doing an echo "bedrijfsnaam is: ".$bedrijfsnaam; die; and see what displays on the screen.
 
Yes $bedrijfsnaam shows what expected.

First the settings for the php mail plugin where different.


$mail_van = array(
$config->get('mailfrom'),
$config->get('fromname')
);

And then:


$mailer = JFactory::getMailer();
// Voeg afzender, ontvanger, reply-to toe in het object
$mailer->addRecipient("jaspervannuijs@gmail.com");
$mailer->setSender($mail_van);
$mailer->addReplyTo('jaspervannuijs@gmail.com', "Jasper");

When sended the groupmail with this settings :

$mail_van (from mail) in gmail:
from name 1.png not in address book. But no email address but a word "beheer"

$mail_van (from mail) in siteground webmail

from name 1 a.png not in address book. But no email address but a word "beheer"

$mailer->addReplyTo('jaspervannuijs@gmail.com', "Jasper"); in gmail:

from name 2.png nice name plus email address

$mailer->addReplyTo('jaspervannuijs@gmail.com', "Jasper"); in gmail:
from name 3.png

So then i change te code back into:

$mailer->addRecipient($mail->email_adres_bedrijf);
$mailer->setSender($mail->email_adres_bedrijf, $bedrijfsnaam);
$mailer->addReplyTo($mail->email_adres_bedrijf, $bedrijfsnaam);

but then i have this:

$mailer->addReplyTo($mail->email_adres_bedrijf, $bedrijfsnaam);

Screenshot_65.png bedrijfsnaam in the reply to field not in address book.

$mailer->setSender($mail->email_adres_bedrijf, $bedrijfsnaam);
from name 4.png not the bedrijfsnaam but only the email address.

I think this is because the $mail_van makes an array of the default website email address and sender name via


$mail_van = array(
$config->get('mailfrom'),
$config->get('fromname')
);

But i dont know how to make an array from:


$q = $db->getQuery(true);
$q
->select('email_adres_bedrijf')
->from('egocentralis_bedrijven')
->where('bedrijf_id IN (SELECT bedrijf_id FROM g4e_orakel_personeel WHERE userid = {$my->id}) OR bedrijf_id IN (SELECT bedrijf_id FROM egocentralis_bedrijven WHERE userid = {$my->id})');
$db->setQuery($q);
$mails = $db->loadObjectList();
$bn = $db->getQuery(true);
$bn
->select('bedrijfsnaam')
->from('egocentralis_bedrijven')
->where('bedrijf_id IN (SELECT bedrijf_id FROM g4e_orakel_personeel WHERE userid = {$my->id}) OR bedrijf_id IN (SELECT bedrijf_id FROM egocentralis_bedrijven WHERE userid = {$my->id})');
$db->setQuery($bn);
$bedrijfsnaam = $db->loadResult();
// Splits de adressen in groepen van 20, zodat de mail niet ineens naar honderden mensen gaat
$adres_chunks = array_chunk($adressen, 20);
// Voor elke groep van 20 mailadressen
foreach($adres_chunks as $groep) {
// Maak een mailobject
$mailer = JFactory::getMailer();

foreach($mails as $mail) {
// Voeg afzender, ontvanger, reply-to toe in het object
$mailer->addRecipient($mail->email_adres_bedrijf);
$mailer->setSender($mail->email_adres_bedrijf, $bedrijfsnaam);
$mailer->addReplyTo($mail->email_adres_bedrijf, $bedrijfsnaam);

The bedrijfsnaam part.

Its working in the addReplyTo($mail part..

Any idea's?
 
Ok, I made this approach and it did work:

bedrijfsnaam.png

foreach($mails as $mail) {
$mail_van = array(
$mail->email_adres_bedrijf,
$mail->bedrijfsnaam
);

So I had to select email_adres_bedrijf and bedrijfsnaam from the database..

Then inserted mail_van into the setSender part.

This is the code to achieve the data:

$q = $db->getQuery(true);
$q
->select('email_adres_bedrijf, bedrijfsnaam')
->from('egocentralis_bedrijven')
->where('bedrijf_id IN (SELECT bedrijf_id FROM g4e_orakel_personeel WHERE userid = {$my->id}) OR bedrijf_id IN (SELECT bedrijf_id FROM egocentralis_bedrijven WHERE userid = {$my->id})');
$db->setQuery($q);
$mails = $db->loadObjectList();

$bn = $db->getQuery(true);
$bn
->select('bedrijfsnaam')
->from('egocentralis_bedrijven')
->where('bedrijf_id IN (SELECT bedrijf_id FROM g4e_orakel_personeel WHERE userid = {$my->id}) OR bedrijf_id IN (SELECT bedrijf_id FROM egocentralis_bedrijven WHERE userid = {$my->id})');
$db->setQuery($bn);
$bedrijfsnaam = $db->loadResult();

// Splits de adressen in groepen van 20, zodat de mail niet ineens naar honderden mensen gaat
$adres_chunks = array_chunk($adressen, 20);

// Voor elke groep van 20 mailadressen
foreach($adres_chunks as $groep) {
// Maak een mailobject
$mailer = JFactory::getMailer();

foreach($mails as $mail) {
$mail_van = array(
$mail->email_adres_bedrijf,
$mail->bedrijfsnaam
);
// Voeg afzender, ontvanger, reply-to toe in het object
$mailer->addRecipient($mail->email_adres_bedrijf);
$mailer->setSender($mail_van);
$mailer->addReplyTo($mail->email_adres_bedrijf, $bedrijfsnaam);

Is this the right approach!!?
 
It would be helpful if you placed the code in a codeblock so it formats correctly. Use the insert button (third from the right) and select code, then do the insert.

Otherwise it looks OK at this point. I would move the getMailer outside of the loop though, but, that then means you have to clear the addresses each time so maybe better inside the loop. I assume there is further code after the addReplyTo as I do not see where you actually issue the send.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top