[Email Notifications] Adding Salutations to "Comments, Messages & Milestones" Emails
Submitted by pmMadeEasy on Thu, 02/14/2008 - 03:55.
Hi all,
I've another question about modifying email notifications.... referencing "language>en_us>emails.php" and the files in "application>views>notifier>...".
I'd like to be able to inject a salutation, such as "Hi John Doe," before all email notifications. I see that this is the default for "forgot_password.php" and "new_account.php". However, I try adding "
<?php
echo lang('hi john doe', $user->getDisplayName())
?>," to "milestone_assigned.php", "new_comment.php", and "new_message.php", but they return errors. So, it doesn't seem it's that easy. :-)
What files do I need to modify to have a personal salutation be added to those particular emails notifications?
Thanks!
- Chris

Hi Chris,
I would think that the problem is that the variable user is not set in the template milestone_assigned.php.
If you have a look at the functions in application/notifier/Notifier.class.php you will see that forgotPassword has the line tpl_assign('user', $user);
The variable $user is then used in the template.
So, you will need to modify the function that calls the template for milestone so that it defines the variable $user and then assign it to the template.
Tim
This one's a little more complicated... Here's what I have code-wise. What specifically do I need to change in "Notifier.class.php > milestoneAssigned Function" to have the user's name appear in the text of the email as a salutation?
[Code from Notifier.class.php]
>>>>>>>>>> forgotPassword Function
static function forgotPassword(User $user) {
$administrator = owner_company()->getCreatedBy();
$new_password = $user->resetPassword(true);
tpl_assign('user', $user);
tpl_assign('new_password', $new_password);
return self::sendEmail(
self::prepareEmailAddress($user->getEmail(), $user->getDisplayName()),
self::prepareEmailAddress($administrator->getEmail(), $administrator->getDisplayName()),
lang('your password'),
tpl_fetch(get_template_path('forgot_password', 'notifier'))
); // send
} // forgotPassword
>>>>>>>>>> milestoneAssigned Function
function milestoneAssigned(ProjectMilestone $milestone) {
if($milestone->isCompleted()) {
return true; // milestone has been already completed...
} // if
if(!($milestone->getAssignedTo() instanceof User)) {
return true; // not assigned to user
} // if
tpl_assign('milestone_assigned', $milestone);
return self::sendEmail(
self::prepareEmailAddress($milestone->getAssignedTo()->getEmail(), $milestone->getAssignedTo()->getDisplayName()),
self::prepareEmailAddress($milestone->getCreatedBy()->getEmail(), $milestone->getCreatedByDisplayName()),
lang('milestone assigned to you') . $milestone->getProject()->getName() . $milestone->getName(),
tpl_fetch(get_template_path('milestone_assigned', 'notifier'))
); // send
} // milestoneAssigned
>>>>>>>>>> Is the code below what I need to add to "milestone_assigned.php"?
<?phpecho lang('hi john doe', $user->getDisplayName())
?>
,
Thanks,
Chris
You need to modify the template application/views/notifier/milestone_assigned.php
the way you mention indeed.
But to make it work you need to attribute a value to the variable $user, otherwise the function call $user->getDisplayName() won't work. You add
<?php$user = $milestone->getAssignedTo()
?>
in the function milestone_assigned(), then call the function tpl_assign for that variable and value by typing
<?phptpl_assign('user', $user);
?>
I admit that I haven't tested it but I'm pretty sure that's how it works.
Let me know if that works or if you have more questions.
Tim
...using the same process for messages and comments, I couldn't get those particular email messages with added-in salutation to work. Any additional insight into what needs to be added to Notifier.class.php to get these last 2 to work? This is what I tried below, but didn't work.
$user = $message->getAssignedTo();
tpl_assign('user', $user);
$user = $comment->getAssignedTo();
tpl_assign('user', $user);
and this added to the templates
<?phpecho lang('hi john doe', $new_account->getDisplayName())
?>
,
Thanks again with the help for the milestone version. It worked great!
- Chris
Hi pmMadeEasy,
instead of what you added, you need to add:
<?phpecho lang('hi john doe', $user->getDisplayName())
?>
The problem in your code is that you assign the variable $user to the template but the template is still using $new_account.
Let me know if that works.
-
Tim
... but still seem to not have it connecting.
I'm trying to get this to work with Comments and Messages.
/application/models/notifier/Notifier.class.php
< Message >
<?php
static function newMessage(ProjectMessage $user, $message, $people) {
$user = $message->getAssignedTo();
if (!is_array($people) || !count($people)) {
return; // nothing here...
} // if
tpl_assign('user', $user);
tpl_assign('new_message', $message);
$recepients = array();
foreach ($people as $user) {
$recepients[] = self::prepareEmailAddress($user->getEmail(), $user->getDisplayName());
} // foreach
return self::sendEmail(
$recepients,
self::prepareEmailAddress($message->getCreatedBy()->getEmail(), $message->getCreatedByDisplayName()),
lang('my company') . lang('new message') . $message->getProject()->getName() . ' - ' . '"' . $message->getTitle() . '"',
tpl_fetch(get_template_path('new_message', 'notifier'))
?>
< Comment >
<?php
static function newMessageComment(Comment $user, $comment) {
$user = $comment->getAssignedTo();
$message = $comment->getObject();
if (!($message instanceof ProjectMessage)) {
throw new Error('Invalid comment object');
} // if
$all_subscribers = $message->getSubscribers();
if (!is_array($all_subscribers)) {
return true; // no subscribers
} // if
$recepients = array();
foreach ($all_subscribers as $subscriber) {
if ($subscriber->getId() == $comment->getCreatedById()) {
continue; // skip comment author
} // if
if ($comment->isPrivate()) {
if ($subscriber->isMemberOfOwnerCompany()) {
$recepients[] = self::prepareEmailAddress($subscriber->getEmail(), $subscriber->getDisplayName());
} // if
} else {
$recepients[] = self::prepareEmailAddress($subscriber->getEmail(), $subscriber->getDisplayName());
} // of
} // foreach
if (!count($recepients)) {
return true; // no recepients
} // if
tpl_assign('user', $user);
tpl_assign('new_comment', $comment);
return self::sendEmail(
$recepients,
self::prepareEmailAddress($comment->getCreatedBy()->getEmail(), $comment->getCreatedByDisplayName()),
lang('my company') . lang('new comment') . $message->getProject()->getName() . ' - ' . '"' . $message->getTitle() . '"',
tpl_fetch(get_template_path('new_comment', 'notifier'))
?>
< Code for Notifiers >
/application/views/notifier/new_comment.php
/application/views/notifier/new_message.php
<?phpecho lang('hi john doe', $user->getDisplayName())
?>
,
Any ideas? When submitting, I get the blank white page, indicating that it didn't quite go through (the new messages and comments do show up once I go back and visit the main page for messages, but the email never makes it).