Answers.Body: <p>For the use case where the email is sent from a logged in user, one could use the function <code>CRM_Core_Session::singleton()-&gt;getLoggedInContactID()</code> to get the logged in contact ID.</p>
  <p>This would fall down for those cases where the email is generated by a 'system call', e.g. an email that was triggered by a cron job, but that might not be a deal breaker for your use case..</p>
  <p>You could always add a fallback behaviour for those situations where it has not been possible to find a contact by either your method or the above method, e.g. populate the token with 'your friendly membership officer' or provide a default contact id or similar.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-15T17:13:41.240Z
Answers.LastActivityDate: 2021-03-25T17:03:05.960Z
Answers.LastEditDate: 2021-03-25T17:03:05.960Z
Answers.UserDisplayName: michaelmcandrew
Body: <p>One of our staff team has recently requested a new token be added to civi mailings that allows the name of the sender of the email to be automatically added (for use in the signature at the foot of the email). This would mean that they did not have to regularly change the name at the bottom of their mailing templates when new staff members left or arrived.</p>
  <p>I see that <strong>hook_civicrm_tokenValues</strong> is the standard hook used to create custom tokens for civi.</p>
  <p>I can see that one of the parameters to this function is <strong>$job</strong>.</p>
  <h2>Mass Mailings Token</h2>
  <p>I have successfully managed to create a token that returns the sender of an email for mass mailings, using this variable $job to call the API and retrieve the required info. Here's the API call that retrieves the first and last names of the creator of such a mass mailing:</p>
  <pre><code>    if (!empty($job)) {
  
        //Create a token to put in the name of the creator of the mailing
  
        // given the job id, retrieve the mailing id.
        // given the mailing id, retrieve the contact id of the creator of the mailing.
        // given the contact id, retrieve the first and last name of the creator of the mailing.
        $result = civicrm_api3('MailingJob', 'getsingle', [
          'sequential' =&gt; 1,
          'return'     =&gt; [&quot;mailing_id.created_id.first_name&quot;, &quot;mailing_id.created_id.last_name&quot;],
          'id'         =&gt; $job,
        ]);
  }
  </code></pre>
  <h2>'Personal' / Activities Mailings Token</h2>
  <p>However, for mailings that are not mass mailings, this technique does not work, since such mailings appear not to have a job id, and are stored not in the civicrm_mailings table, but the civicrm_activities table.</p>
  <p>I have examined the incoming contents into the hook_civicrm_tokenValues hook. They are $values, $tokens, and $context.</p>
  <p><strong>$tokens</strong> appears to list which tokens are being used in the particular mailing being processed.</p>
  <p><strong>$context</strong> appears to show in what context the hook is being called - in this case 'CRM_Activity_BAO_Activity'.</p>
  <p><strong>$values</strong> appears to show the values that are available to be used to construct tokens. These all appear to be values directly related to the contact being emailed e.g. first name, last name, display name, etc... There do not seem to be any in the list that I could use to call the API (or indeed query the database) to get the contact name of the person sending the email.</p>
  <p>Is this simply not possible, or is there  a way of doing so?</p>
  <p>Ideally, one would be able to access the activity_id of the activity that the mailing was part of. If that was available, calling the API to retrieve the email creator (using the civicrm_activities_contact table) would be trivial.</p>
  
Tags: <email><activities><tokens><hooks>
Title: Is it possible to create a token in hook_civicrm_tokenValues that retrieves the name of the creator of a non-mass-mailing?
ThreadText: # Is it possible to create a token in hook_civicrm_tokenValues that retrieves the name of the creator of a non-mass-mailing?
  
  (Asked by: username_0 on 2021-03-15)
  
  One of our staff team has recently requested a new token be added to civi mailings that allows the name of the sender of the email to be automatically added (for use in the signature at the foot of the email). This would mean that they did not have to regularly change the name at the bottom of their mailing templates when new staff members left or arrived.
  I see that hook_civicrm_tokenValues is the standard hook used to create custom tokens for civi.
  I can see that one of the parameters to this function is $job.
  Mass Mailings Token
  I have successfully managed to create a token that returns the sender of an email for mass mailings, using this variable $job to call the API and retrieve the required info. Here's the API call that retrieves the first and last names of the creator of such a mass mailing:
      if (!empty($job)) {
  
        //Create a token to put in the name of the creator of the mailing
  
        // given the job id, retrieve the mailing id.
        // given the mailing id, retrieve the contact id of the creator of the mailing.
        // given the contact id, retrieve the first and last name of the creator of the mailing.
        $result = civicrm_api3('MailingJob', 'getsingle', [
          'sequential' => 1,
          'return'     => ["mailing_id.created_id.first_name", "mailing_id.created_id.last_name"],
          'id'         => $job,
        ]);
  }
  
  'Personal' / Activities Mailings Token
  However, for mailings that are not mass mailings, this technique does not work, since such mailings appear not to have a job id, and are stored not in the civicrm_mailings table, but the civicrm_activities table.
  I have examined the incoming contents into the hook_civicrm_tokenValues hook. They are $values, $tokens, and $context.
  $tokens appears to list which tokens are being used in the particular mailing being processed.
  $context appears to show in what context the hook is being called - in this case 'CRM_Activity_BAO_Activity'.
  $values appears to show the values that are available to be used to construct tokens. These all appear to be values directly related to the contact being emailed e.g. first name, last name, display name, etc... There do not seem to be any in the list that I could use to call the API (or indeed query the database) to get the contact name of the person sending the email.
  Is this simply not possible, or is there  a way of doing so?
  Ideally, one would be able to access the activity_id of the activity that the mailing was part of. If that was available, calling the API to retrieve the email creator (using the civicrm_activities_contact table) would be trivial.
  
  **Tags:** email, activities, tokens, hooks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-15. Score: 3 | Accepted answer
  
  For the use case where the email is sent from a logged in user, one could use the function `CRM_Core_Session::singleton()->getLoggedInContactID()` to get the logged in contact ID.
  
  This would fall down for those cases where the email is generated by a 'system call', e.g. an email that was triggered by a cron job, but that might not be a deal breaker for your use case..
  
  You could always add a fallback behaviour for those situations where it has not been possible to find a contact by either your method or the above method, e.g. populate the token with 'your friendly membership officer' or provide a default contact id or similar.
  
  
  ---
  
  