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.