can we use to send email notification programmatically from using email address which is configured in email settings of email module.?

Hey,

I would like to send an email notification programmatically using a different email address that has been configured under the Email module settings, rather than using the default system outbound email address configured in Admin > Email Settings.

Is there a way to send such notifications by referencing the personal or other configured email address using its record ID or name, similar to how the system default address is used?

For reference, I have followed the standard process for configuring these email addresses via the Email module as outlined in the link below:

https://support.sugarcrm.com/knowledge_base/email/configuring_the_from_address_for_outbound_email/#Personal_Emails

https://support.sugarcrm.com/documentation/sugar_versions/25.1/ent/application_guide/emails/#Outgoing_Email_Accounts

Looking forward to your guidance on this.

Thanks,

Manisha Narwade

  • Hello  ,

    You can do this using Process Definition.

    Use either the Send Message event, or an End event can also be send message.

    • Select the Process Definition Email Template (you woudl have already created)
    • Then in FROM if you go to 'search and select' it will show you the emails you have configured already in email settings.

    Screenshot below showing only the send message event settings (it's just an empty process definition other then that)

    I have done this in one scenario where I needed to send emails from Sales directly, rather than our system email.

    Hope that helps.

  • Hello y,

    Thank you for your suggestion.

    However, my requirement is to send notifications programmatically. I would like to know if there's a way to send email notifications using a configured email address from the Email module, instead of the default outbound email address defined in the system settings.

    Is it possible to send such notifications through code by referencing the configured email address—perhaps by its record ID or name?

    Looking forward to your guidance.

    Thanks,

    Manisha Narwade.

  • Ah sorry Manisha,

    Think I assumed 'automatically' instead of 'programatically'.

    I am sure you will get the response you need in here.  Good luck with your project.

  • Hi  ,

    You don't give any examples of how you are programmatically sending the email at present so I will assume you are using the MailerFactory object as we do.

    The usual way we would send an email is to grab an instance of the Mailer using code like this:

    Fullscreen
    1
    $mailer = MailerFactory::getSystemDefaultMailer();
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    which will get a Mailer which is configured to use the System Email account as you describe.

    If you check in the code for that object at: ./modules/Mailer/MailerFactory.php you will find there are some other functions you can use to alter the email account settings used for the Mailer object. There is:

    Fullscreen
    1
    getMailerForUser(User $user)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    which takes a User record as the source of the email account to use and

    Fullscreen
    1
    getMailer(OutboundEmailConfiguration $config)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    which uses a supplied configuration to override the settings of the Mailer it will return.

    Once you have this Mailer you can use the normal functions such as:

    Fullscreen
    1
    2
    3
    4
    $mailer->setSubject($subject);
    $mailer->setHtmlBody($html_body);
    $mailer->addAttachment($attachment);
    $mailer->addRecipientsTo(new EmailIdentity($contact->email1));
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    to set up the email content, attachments, recipients etc. using the parameters we have defined elsewhere - these should be straightforward to work out.

    Hopefully this is along the lines of what you are asking for as a programmatic way to choose the email account to use for sending an email..

    Thanks,

    JH.

  • Hello ,

    Thanks for your feedback. Yes its working. 

    Below is latest working code:

    try{
    $user = BeanFactory::getBean("Users", 'user_id');
    $phpMailer = MailerFactory::getMailerForUser($user);
    $mailTransmissionProtocol = $phpMailer->getMailTransmissionProtocol();
    $phpMailer->setSubject($emailSubject);
    $phpMailer->addRecipientsTo(new EmailIdentity('email.address@.com', 'email_addressName'));
    $phpMailer->setHtmlBody($emailBody);
    $phpMailer->send();
    echo "\n Email sent successfully.";
    }catch(MailerException $me) {
    $message = $me->getMessage();
    echo "\n Test Email Notification from Ambit Dev Team!!: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})";
    }

    Thanks,

    Manisha Narwade