Welcome, Guest
Username: Password: Remember me

TOPIC: Mail Publishers only after form submission.

Mail Publishers only after form submission. 17 Feb 2014 03:20 #12210

  • jonathanbell
  • jonathanbell's Avatar
  • Offline
  • Premium Member
  • Posts: 147
  • Thank you received: 5
  • Karma: 1
Hi all, I see how to add a mail function into a component, and thanks for those who helped the user in the post below. I am still learning php code and joomla core and I must admit I am learning slowly.

Forum Post 6821- send mail after saving
// Init mail send
$mail =& JFactory::getMailer();
$config =& JFactory::getConfig();
$mail->isHTML(true);
$mail->addRecipient('This email address is being protected from spambots. You need JavaScript enabled to view it.');
$body = "Here is my text";
$mailSubject = "This is my subject"
$mail->setBody($body);
$mail->setSubject($mailSubject);
$mail->Encoding = 'base64';

if ($mail->Send()) {
echo "Mail sent successfully.";
$app = JFactory::getApplication();
$app->enqueueMessage('Mail sent');
} else {
echo "An error occurred. Mail was not sent.";
}

What I am trying to determine is if the mail code in the above example could be modified to mail to everyone that has publisher access on the joomla site and how you would also provide a link in the email back to the specific entry that was submitted, regardless of what form the mail has been sent from.

I really wish I knew how to do this myself. Appreciate any help.

Thanks in advance..
Last Edit: 17 Feb 2014 03:22 by jonathanbell. Reason: URL wrong in post
The administrator has disabled public write access.

Mail Publishers only after form submission. 17 Feb 2014 11:55 #12212

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Firstly, replace the '=&' per '=' only. (Better php syntax because it is already a pointer)

$mail->addRecipient()

is the function you must use to add more recipients.
If you want to get all publishers, you need to do it manually with a SQL proper query.
It is possible to check ACL of a user, but not to load all users FROM an ACL.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: jonathanbell

Mail Publishers only after form submission. 18 Feb 2014 09:31 #12220

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
This should get you your authors:
jimport( 'joomla.access.access' ); // import access
$authors = JAccess::getUsersByGroup(3); // get all users by group
Simply replace the argument (3) with your authors group id and you're good to go.

This will give you an indexed array of user ids like:
array(
    '0' =>  71
    '1' =>  75
    '2' =>  79
);
Therefore, you can easily iterate it in a foreach loop to grab the user's email like:
$authorEmails = array();                        // To store emails 
foreach($authors as $userId){                   // Loop through all user ID's we just got
    $email = JFactory::getUser($userId)->email; // Get the user email from JUser Object
    if($email !== '')                           // If it's not an empty string
        $authorEmails[] = $email;               // Push the value into the array
}

This will result in another indexed array like
array(
    '0' =>  "user71@domain.com"
    '1' =>  "user75@domain.com"
    '2' =>  "user79@domain.com"
);
So now you can easily access all of the author emails to pipe in to your email.

PUTTING IT ALL TOGETHER
The whole thing should look something like:
// Get the authors 
jimport( 'joomla.access.access' );                  // import JAccess
$authors = JAccess::getUsersByGroup(2);             // replace the argument (3) with your authors group id
$app = JFactory::getApplication();                  // Get application
// iterate $authors to get all authors user ID's
if(!empty($authors)){
    // Init mail send
    $mail   = JFactory::getMailer();                   // New mailer object instance
    $config = JFactory::getConfig();                   // get the config (don't know why because this is not referenced anywhere below)
    $mail->isHTML(true);                               // set the mode to HTML
    
    // iterate $authors to get all authors user ID's
    foreach($authors as $userId){                       // Loop through all user ID's we just got
        $email = JFactory::getUser($userId)->email;     // Get the user email from JUser Object
        if($email !== '')                               // If it's not an empty string
            $mail->addRecipient($email);                // Add the authors email as a recipient
    }// End foreach

    // Continue setting email properties
    $body = "Here is my text";                          // Build the Message Body string
    $mailSubject = "This is my subject";                // Build the Subject string
    $mail->setBody($body);                              // Set body in the mail object
    $mail->setSubject($mailSubject);                    // Set subject in the mail object
    $mail->Encoding = 'base64';                         // Set mail encoding

    // Send email(s)
    if($mail->Send())                                   // If success         
        $app->enqueueMessage('Mail sent');              // Output a success message
    else                                                // Otherwise,
        $app->enqueueMessage(                           // Output a failure message
            "An error occurred. Mail was not sent.",    // Add Message
            "error"                                     // Set message type to error
        ); 
}else{
    $app->enqueueMessage(                           // Output a failure message
            "Error: No recipients. Mail cancelled. ",    // Add Message
            "error"                                     // Set message type to error
        ); 
}
N.B. When you put this into production, it would be a good idea to remove the last else statement as you wouldn't really want to output this message to your users. (
else{
    $app->enqueueMessage(                           // Output a failure message
            "Error: No recipients. Mail cancelled. ",    // Add Message
            "error"                                     // Set message type to error
        ); 
}
) The same could also be said about all other messages too since there's no real reason to let your users know that an email has been sent/not sent if it is simply for the purpose of informing publishers that new content is waiting for them to publish.

Hope it helps,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 19 Feb 2014 09:07 by JoomGuy. Reason: updated to output error if no authors
The administrator has disabled public write access.
The following user(s) said Thank You: admin, jonathanbell

Mail Publishers only after form submission. 23 May 2015 01:26 #13165

  • jonathanbell
  • jonathanbell's Avatar
  • Offline
  • Premium Member
  • Posts: 147
  • Thank you received: 5
  • Karma: 1
Sorry I did not reply earlier. Gez, thanks heaps. I have so much to learn.
The administrator has disabled public write access.
Time to create page: 0.077 seconds

Get Started