Set "from" field in Emails module link to current logued user


When I create a draft email in any related module, Leads for example, and then  save the draft email, in the from field, sugarCRM sets the current user Link.

I am trying to do the same in a logic hook, but I don't know how to do that. I saw the model in the sidecar and the field name is "from_collection", I try those, but did not work.

global $current_user;
$lead = BeanFactory::retrieveBean("Leads", "5f6172c0-466c-11e9-b85f-02dcb16454fa");
$user = BeanFactory::retrieveBean("Users", $current_user->id);
$mail = BeanFactory::newBean("Emails");
$emailAddres = null;

$emailAddres = $user->email_addresses->getBeans();
$emailAddres = $emailAddres[array_keys($emailAddres)[0]];

$mail->assigned_user_id = $user->id;
$mail->parent_type = "Leads";
$mail->parent_id = "5f6172c0-466c-11e9-b85f-02dcb16454fa";

//firts try
$mail->from_addr = $user->email1;
$mail->from_name = $user->name;
//second try
$mail->from_collection = $user;
$mail->from = $user;

I saw the Email class in sugar and it says

* The name and email address of the email's sender, formatted for use in the email's FROM header.
* @var string
* @deprecated Use {@link Email::$from} to link the sender to the email.
    var $from_addr;

any Idea how to use @link Email::$from

  • Hi Diego Ramses Lopez Gonzalez,

    I have done a similar thing and I used outbound_email_id field in email bean. This setup my 'from' field automatically.

    $email->outbound_email_id = "<outbound_mailbox_uuid>";

    Just a question, does your SMTP allow sending emails by using different email IDs? If no, then you can just use outbound_email_id field and pass your outbound mailbox ID to it.

    If you are using the latest sugar version (sugar 9.x), you can have a look at OutboundEmailConfigurationPeer class in modules/OutboundEmailConfiguration and get ID of system default mail configuration or user based.

    Please let us know if this helps.