Answers.Body: <p>They're both valid options.</p>
  
  <p><code>$message-&gt;send()</code> is a wrapper for <code>Mailer</code>:</p>
  
  <p><a href="https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMessage.php#L41-L50" rel="nofollow noreferrer">https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMessage.php#L41-L50</a></p>
  
  <p>It will take an optional Mailer parameter.  If one is not passed it, it will default to the default Yii/Craft::$app->mailer and ultimately just call <code>send()</code> on it.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-18T22:46:58.470Z
Answers.LastActivityDate: 2018-09-18T22:46:58.470Z
Answers.UserDisplayName: Brad Bell
Body: <p>In the Craft 3 Class Reference (<a href="https://docs.craftcms.com/api/v3" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3</a>), there are two classes under <code>craft\mail</code>:</p>
  
  <ul>
  <li>Mailer</li>
  <li>Message</li>
  </ul>
  
  <p>I found that I was able to send an email using this:</p>
  
  <pre><code>&lt;?php 
  
  use craft\mail\Message;
  
  $message = new Message();
  $message-&gt;setTo('john@doe.com');
  $message-&gt;setSubject("Hello world.");
  $message-&gt;setHtmlBody("Hello world.");
  $message-&gt;send();
  </code></pre>
  
  <p>But I found in some examples, like <a href="https://craftcms.stackexchange.com/questions/23804/how-to-send-email-from-craft-3-custom-plugin">this one</a>, that the email could be send using <code>Craft::$app-&gt;mailer-&gt;send($message)</code> instead of <code>$message-&gt;send()</code>.</p>
  
  <p>What's the difference between the <strong>Mailer</strong> and the <strong>Message</strong> classes? When should I use one instead of the other?</p>
  
Tags: <plugin-development><documentation><classes>
Title: What's the differences between the Mailer and the Message classes?
ThreadText: # What's the differences between the Mailer and the Message classes?
  
  (Asked by: username_0 on 2018-09-18)
  
  In the Craft 3 Class Reference (<https://docs.craftcms.com/api/v3>), there are two classes under `craft\mail`:
  
  * Mailer
  * Message
  
  I found that I was able to send an email using this:
  
  ```
  <?php 
  
  use craft\mail\Message;
  
  $message = new Message();
  $message->setTo('john@doe.com');
  $message->setSubject("Hello world.");
  $message->setHtmlBody("Hello world.");
  $message->send();
  
  ```
  
  But I found in some examples, like [this one](https://craftcms.stackexchange.com/questions/23804/how-to-send-email-from-craft-3-custom-plugin), that the email could be send using `Craft::$app->mailer->send($message)` instead of `$message->send()`.
  
  What's the difference between the **Mailer** and the **Message** classes? When should I use one instead of the other?
  
  **Tags:** plugin-development, documentation, classes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-18. Score: 0 | Accepted answer
  
  They're both valid options.
  
  `$message->send()` is a wrapper for `Mailer`:
  
  <https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMessage.php#L41-L50>
  
  It will take an optional Mailer parameter. If one is not passed it, it will default to the default Yii/Craft::$app->mailer and ultimately just call `send()` on it.
  
  
  ---
  
  