2

In a custom plugin, I would like to use Craft's ability to register email message keys in order to keep emails consistent, and I would like to be able to send these emails to users as well as non-users. My understanding is that I would need to use the sendEmailByKey method of the EmailService to send emails using registered keys, which is defined like this:

public function sendEmailByKey(UserModel $user, $key, $variables = array())

This method only accepts a UserModel for the recipient, so it doesn't look like there's a way to send an email by key to an arbitrary email address. Is there a different way to send these registered emails to non-users?

Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
John O
  • 391
  • 1
  • 9
  • Could you just spin of a new UserModel() and assign your non-user email to it before calling sendEmailByKey? – Brad Bell Oct 01 '15 at 22:23
  • Thanks, @BradBell! That seems like it should work and is the same solution Brandon Kelly suggested. – John O Oct 02 '15 at 14:04

1 Answers1

4

Just because EmailService::sendEmailByKey() requires you to pass in a UserModel, doesn’t mean that the model has to represent an actual saved user. You can create one on the spot for them:

$user = new UserModel();
$user->email = 'sergster@abc.xyz';
$user->firstName = 'John'; // optional
$user->lastName = 'Doe'; // optional

craft()->email->sendEmailByKey($user, 'my_key', $variables);
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
  • Oh yeah, that totally makes sense, thanks! Do you have any insight on the secnd part of my question, namely which version of the email message (control panel vs. translation file) takes precedence? – John O Oct 02 '15 at 14:03
  • @JohnO I just edited your question to remove that second part; can you please post it as a new question? (You can see what you originally typed here: http://craftcms.stackexchange.com/posts/11701/revisions) – Brandon Kelly Oct 02 '15 at 15:04
  • Thanks for taking care of editing it out. I re-posted the question here: http://craftcms.stackexchange.com/questions/11728/do-email-messages-use-control-panel-or-translation-file-template-when-sending-by – John O Oct 02 '15 at 15:51