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?

  • Could you just spin of a new UserModel() and assign your non-user email to it before calling sendEmailByKey? – Brad Bell Oct 1 '15 at 22:23
  • Thanks, @BradBell! That seems like it should work and is the same solution Brandon Kelly suggested. – John O Oct 2 '15 at 14:04
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);
  • 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 2 '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: craftcms.stackexchange.com/posts/11701/revisions) – Brandon Kelly Oct 2 '15 at 15:04
  • Thanks for taking care of editing it out. I re-posted the question here: craftcms.stackexchange.com/questions/11728/… – John O Oct 2 '15 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.