Trying to send email via workflow to all of a contacts email addresses

Hello

I have created a workflow to send a reminder email. It is sending to the contacts first listed email address but I need it to go to all email addresses that they have listed under their contact record. Is there any way to achieve this scenario?

Please see screenshot for my current configuration:

suitecrm

Regards

Jason

It is sending only to the primary email address of the contacts. If you are a developer you need to update some codes in workflow action files to send emails to other email address of that contact.

1 Like

Thanks, you pointed me in the right direction.

For anyone interested, here is my working workflow code which emails all email addresses for the specified contact record. Email addresses which are marked as invalid or opted out are skipped.

    <?php

    class emailTest 
    {

            function emailTest()
            {
                    // include Email class
                    include_once('include/SugarPHPMailer.php');
                    include_once('include/utils/db_utils.php'); // for from_html function
                    require_once("modules/EmailTemplates/EmailTemplate.php");
                    include_once("include/SugarEmailAddress/SugarEmailAddress.php");

                    $mail = new SugarPHPMailer();
                    // Add details
                    $mail->From = "your@sending-email-address.com"; 
                    $mail->FromName = "Your Sending Name";
                    // Clear recipients
                    $mail->ClearAllRecipients();
                    $mail->ClearReplyTos();

                    $oContact = new Contact();
                    $oContact->retrieve("put your contact ID here");  //Contact ID such as a79a5e6b-6172-f2aa-ecb0-55d8c68ecbbe

                    foreach ($oContact->emailAddress->addresses as $address)
                    {
                            $send = true;   
                            // Check if invalid_email or opt_out
                            foreach ($address as $key=>$value)
                            {
                                    if (($key == "invalid_email") or ($key == "opt_out"))
                                    {
                                            if ($value == 1)
                                            {
                                                    $send = false;
                                            }
                                    }
                            }
                            // Add recipient if send is still true
                            if ($send == true)
                            {
                                    foreach ($address as $key=>$value)
                                    {
                                            if ($key == "email_address")
                                            {
                                                    $mail->AddAddress($value, 'Your Name');
                                            }
                                    }
                            }
                    }

                    // Add subject
                    $mail->Subject = "Welcome";
                    // Add mail content
                    $mail->Body_html = from_html("<YOUR_EMAIL_CONTENT_HERE>");
                    $mail->Body = wordwrap("<YOUR_EMAIL_CONTENT_HERE>",900);
                    $mail->isHTML(true); // set to true if content has html tags

                    // Prepare for sending
                    $mail->prepForOutbound();
                    $mail->setMailerForSystem();

                    //Send mail, log if there is error
                    if (!$mail->Send())
                    {
                            $GLOBALS['log']->fatal("ERROR: Mail sending failed!");
                    }
            }
    }

    ?>

How exactly do you run that?

I mean, how does that connect to the workflow?

Sorry, I think workflow may the incorrect term, I think I should have used the term Logic Hook - I was unable to find a way to achieve this using workflows.

Ok. If you can include in your solution the exact way in which this ties in to the logic hooks, then other people coming here in the future will be able to more easily use your work.

Thanks!

Ok will do - I am struggling to see how I can edit the post to add in detail? The Edit pencil seems to only give me a list of the editing history, not the ability to actually edit?

You can just post an additional answer, maybe it is easier.

If you really prefer to edit, use the pencil button at the bottom of the post (after pressing the “…” button), not the one on the top.

(I just made an edit to add syntax highlighting to your post, have a look to see how it is done)

I think I will have to do a new post, asyou can see, clicking the dots expands the menu but no pencil icon is showing:

:thinking: hhmm ok. I have no idea why I can edit posts and you can’t - I don’t think it’s a privilege of moderators. you should be able to edit your own posts. Maybe it’s because it’s already locked as a “Solution” or something like that.

I will un-mark it as a “solution” and then you can try to see if it let’s you edit, ok?

Scenario: I have reminder calls scheduled as every 6 months I call my clients to ensure all is well and see if they require any services. I like to initially send a reminder email first and if I get no response, I follow up with the telephone call.

The problem: I want to automate these reminder emails so each day SuiteCRM will draw up a list of reminder calls with a start date of today and email a template to all associated email addresses (excluding those marked as opted out and invalid email addresses). the template should be customised with the first name of the client.

My solution: I was unable to find a way to achieve this with workflows. I have instead used a scheduler custom job which I have configured via the SuiteCRM scheduler to fire at 08.30am every morning.

My custom script located in custom/Extension/modules/Schedulers/Ext/ScheduledTasks/checkupReminders.php:

    <?php
            /*
            * We add the method name to the $job_strings array.
            * This is the method that jobs for this scheduler will call.
            */

            $job_strings[] = 'getCurrentCalls';

            function getCurrentCalls()
            {
                    include_once('include/SugarPHPMailer.php');
                    include_once('include/utils/db_utils.php'); // for from_html function
                    require_once("modules/EmailTemplates/EmailTemplate.php");
                    include_once("include/SugarEmailAddress/SugarEmailAddress.php");

                    // Database connection details
                    $servername = "localhost";
                    $username = "your database user";
                    $password = "your database password";
                    $dbname = "sugarcrm";

                    // Create email template and populate with existing structure
                    $emailtemplate = new EmailTemplate();
                    $emailtemplate->disable_row_level_security = true;
                    $emailtemplate->retrieve("8634c421-2aac-b7ae-2ec5-600078bce25e");  // Email template ID change to your own

                    // Create new email and populate with details
                    $mail = new SugarPHPMailer();
                    $mail->From = "your from email address";
                    $mail->FromName = "your from name";

                    // Create connection to database
                    $conn = new mysqli($servername, $username, $password, $dbname);

                    // Check connection to database
                    if ($conn->connect_error)
                    {
                            die("Connection failed: " . $conn->connect_error);
                    }

                    // Run database query to retrieve all "Checkup due..." calls which have a start date of today
                    $sqlcalls = "SELECT id, name, date_start FROM calls WHERE name LIKE 'Checkup due%' AND DATE(date_start) = CURDATE()";
                    $resultcalls = $conn->query($sqlcalls);

                    // If there is at least one "Checkup due..." call for today, start preparing to send the reminder email
                    if ($resultcalls->num_rows > 0)
                    {
                            // Loop over each "Checkup due..." call result 
                            while($row = $resultcalls->fetch_assoc())
                            {
                                    $sqlcallcontact = "SELECT contact_id FROM calls_contacts WHERE call_id = '" . $row["id"] . "'";
                                    $resultcallcontact = $conn->query($sqlcallcontact);
                                    $rowcallcontact = mysqli_fetch_array($resultcallcontact);

                                    $oContact = new Contact();
                                    $oContact->retrieve($rowcallcontact["contact_id"]);  //Contact ID

                                    // Clear recipients
                                    $mail->ClearAllRecipients();
                                    $mail->ClearReplyTos();

                                   foreach ($oContact->emailAddress->addresses as $address)
                                    {
                                            $send = true;

                                            // Check if invalid_email or opt_out
                                            foreach ($address as $key=>$value)
                                            {
                                                    if (($key == "invalid_email") or ($key == "opt_out"))
                                                    {
                                                            if ($value == 1)
                                                            {
                                                                    $send = false;
                                                            }
                                                    }
                                            }

                                            // Add recipient if send is still true
                                            if ($send == true)
                                            {
                                                    foreach ($address as $key=>$value)
                                                    {
                                                            if ($key == "email_address")
                                                            {
                                                                    $mail->AddBCC($value, 'Test test');
                                                            }
                                                    }
                                            }
                                    }

                                    // Add subject
                                    $mail->Subject = $emailtemplate->subject;
                                    // Add mail content
                                    $mail->Body_html = from_html($emailtemplate->body_html);
                                    $beanArray = ['Contacts' => $rowcallcontact["contact_id"]];
                                    $mailtext = $emailtemplate->parse_template($emailtemplate->body_html,$beanArray);
                                    $mail->Body = wordwrap($mailtext,900);
                                    $mail->isHTML(true); // set to true if content has html tags

                                    // Prepare for sending
                                    $mail->prepForOutbound();
                                    $mail->setMailerForSystem();

                                    // Send mail, log if there is error
                                    if (!$mail->Send())
                                    {
                                            $GLOBALS['log']->fatal("ERROR: Mail sending failed!");
                                    }

                            }
                    }

                    // If no "Checkup due..." calls start today, do nothing
                    else
                    {
                            echo "0 results";
                    }

                    // Close connection to database
                    $conn->close();

                    // Return true once completed
                    return true;
            }

    ?>

A language file located in custom/Extension/modules/Schedulers/Ext/Language/en_us.checkupReminders.php:

    <?php
            //We add the mod string for our method here.
            $mod_strings['LBL_GETCURRENTCALLS'] = 'Send checkup due reminder emails for all calls that start today.';
    ?>

Once you have created the two files above, you need to perform a “Quick Repair and Rebuild” so that the custom script will appear as an option when you “Create Scheduler” as shown below:

1 Like

Thanks for sharing your solution :clap: