EmailService

Type
Class
Namespace
Craft
Inherits
Craft\EmailService » Craft\BaseApplicationComponent » CApplicationComponent » CComponent
Implements
IApplicationComponent
Since
1.0

EmailService provides APIs for sending email in Craft.

An instance of EmailService is globally accessible in Craft via {@link WebApp::email craft()->email}.

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$behaviorsarray – The behaviors that should be attached to this component.

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
getEventHandlers()Returns the list of attached event handlers for an event.
getIsInitialized()Checks if this application component has been initialized.
getSettings()Returns the system email settings defined in Settings → Email.
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
init()Initializes the application component.
isInitialized()Checks if this application component has been initialized yet, or not.
onBeforeSendEmail()Fires an 'onBeforeSendEmail' event.
onSendEmail()Fires an 'onSendEmail' event.
onSendEmailError()Fires an 'onSendEmailError' event.
raiseEvent()Raises an event.
sendEmail()Sends an email based on the attributes of the given {@link EmailModel}.
sendEmailByKey()Sends an email by a given key.
sendTestEmail()Sends a test email using the given settings.

getSettings()

Returns the system email settings defined in Settings → Email.

View source

Returns

array – The system email settings.

Signature

public array getSettings ( )

onBeforeSendEmail()

Fires an 'onBeforeSendEmail' event.

View source

Arguments

Signature

public void onBeforeSendEmail ( Craft\Event $event )

onSendEmail()

Fires an 'onSendEmail' event.

View source

Arguments

Signature

public void onSendEmail ( Craft\Event $event )

onSendEmailError()

Fires an 'onSendEmailError' event.

View source

Arguments

Signature

public void onSendEmailError ( Craft\Event $event )

sendEmail()

Sends an email based on the attributes of the given {@link EmailModel}. Emails sent with sendEmail() will have both plain text and HTML bodies, leaving it up to the email client to decide which one to show.

The plain text body’s template is defined by the EmailModel’s {@link EmailModel::body body} attribute, and the HTML body’s template is defined by the {@link EmailModel::htmlBody htmlBody} attribute. If the htmlBody attribute is not set, then the HTML body will be automatically generated by running the plain text body through {@link StringHelper::parseMarkdown() Markdown}.

Both the plain text and HTML bodies of the email will be rendered with Twig. A user variable will be available to them, which will be set to a {@link UserModel} representing the user that the email is getting sent to (identified by the EmailModel’s {@link EmailModel::toEmail toEmail} attribute). Any variables passed into sendEmail()’s $variables argument will alse be made available to the templates.

$email = new EmailModel();
$email->toEmail = 'hello@example.com';
$email->subject = 'Heyyyyy';
$email->body    = 'How you doin, {{ user.name }}?';

craft()->email->sendEmail($email);

View source

Arguments

  • $emailModel (Craft\EmailModel) – The EmailModel object that defines information about the email to be sent.
  • $variables (array) – Any variables that should be made available to the email’s plain text and HTML bodies as they are being rendered with Twig, in addition to the user variable.

Returns

boolean – Whether the email was successfully sent.

Signature

public boolean sendEmail ( Craft\EmailModel $emailModel, $variables = [] )

sendEmailByKey()

Sends an email by a given key. Craft has four predefined email keys: account_activation, verify_new_email, forgot_password, and test_email.

Plugins can register additional email keys using the registerEmailMessages hook, and by providing the corresponding language strings.

craft()->email->sendEmailByKey($user, 'account_activation', array(
    'link' => $activationUrl
));

View source

Arguments

  • $user (Craft\UserModel) – The user that should receive the email.
  • $key (string) – The email key.
  • $variables (array) – Any variables that should be passed to the email body template.

Returns

boolean – Whether the email was successfully sent.

Throws

Signature

public boolean sendEmailByKey ( Craft\UserModel $user, $key, $variables = [] )

sendTestEmail()

Sends a test email using the given settings.

View source

Arguments

  • $settings (array) – The email settings to test.

Returns

boolean – Whether the email was successfully sent.

Signature

public boolean sendTestEmail ( $settings )