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

Parents
  • 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:

    $mailer = MailerFactory::getSystemDefaultMailer();

    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:

    getMailerForUser(User $user)

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

    getMailer(OutboundEmailConfiguration $config)

    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:

    $mailer->setSubject($subject);
    $mailer->setHtmlBody($html_body);
    $mailer->addAttachment($attachment);
    $mailer->addRecipientsTo(new EmailIdentity($contact->email1));

    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

Reply
  • 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

Children
No Data