Answers.Body: <p>Your emails are being sent via Mailgun, that's a system-wide setting so all emails will be sent using that email transport.</p>
  <p>The issue could be with the HTML markup of your email. Craft's system emails are very simple markup-wise, but if your custom email is more complex, that could explain why system emails arrive as expected but ones from your module do not.</p>
  <p>The markup might be invalid or there's something in the content that's triggering the spam filters. If you can, it's worth checking the email in a tool like <a href="https://www.litmus.com/" rel="nofollow noreferrer">Litmus</a> or <a href="https://www.htmlemailcheck.com/" rel="nofollow noreferrer">HTML Email Check</a> to make sure it's going to display correctly in your target email platforms. These are paid-for apps, though, and Litmus in particular isn't cheap.</p>
  <p>Alternatively, you could try breaking up your email markup into Twig partials, commenting out virtually everything and then adding sections back one at a time to try and identify which part of the email could be causing it to be classified as spam.</p>
  <p>You should also make sure that the domain you're using with Mailgun is fully set up with the appropriate entries for SPF, DKIM and DMARC. (<a href="https://www.mailgun.com/blog/deliverability/email-authentication-your-id-card-sending/" rel="nofollow noreferrer">more here</a>)</p>
  <p>Finally, the IP address that an email is sent from can have an effect on an email's spam score. Mailgun is a well-known ESP, but when I've used them in the past I ended up upgrading to use a static IP to gain better deliverability. This could be worth a try if nothing else helps.</p>
  <p>HTML email is a PITA at the best of times, and I've had similar experiences where emails have been classified as spam. I found that a combination of the methods above helped enormously. Best of luck!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-04T09:09:17.323Z
Answers.LastActivityDate: 2024-10-04T09:09:17.323Z
Answers.UserDisplayName: Martin Spain
Body: <p>System emails from Craft (&quot;activation&quot;, &quot;forgot password&quot; etc) are coming into my inbox fine (I'm using the Mailgun plugin), but emails I send from my module controller using the Mailer service are going to spam. Should I be using a different method to send the emails? Or is there some way I can route them through Mailgun? This is the function I'm using to send them:</p>
  <pre><code>private function sendMail(string $html = '', string $subject = '', string $address, array $from = [], string $cc = ''): bool
      {
        return Craft::$app
          -&gt;getMailer()
          -&gt;compose()
          -&gt;setTo($address)
          -&gt;setFrom($from)
          -&gt;setCc($cc)
          -&gt;setSubject($subject)
          -&gt;setHtmlBody($html)
          -&gt;send();
      }
  </code></pre>
  
Tags: <emails><modules>
Title: Emails from my module controller are going to spam
ThreadText: # Emails from my module controller are going to spam
  
  (Asked by: username_0 on 2024-10-03)
  
  System emails from Craft ("activation", "forgot password" etc) are coming into my inbox fine (I'm using the Mailgun plugin), but emails I send from my module controller using the Mailer service are going to spam. Should I be using a different method to send the emails? Or is there some way I can route them through Mailgun? This is the function I'm using to send them:
  
  ```
  private function sendMail(string $html = '', string $subject = '', string $address, array $from = [], string $cc = ''): bool
      {
        return Craft::$app
          ->getMailer()
          ->compose()
          ->setTo($address)
          ->setFrom($from)
          ->setCc($cc)
          ->setSubject($subject)
          ->setHtmlBody($html)
          ->send();
      }
  
  ```
  
  **Tags:** emails, modules
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-10-04. Score: 3 | Accepted answer
  
  Your emails are being sent via Mailgun, that's a system-wide setting so all emails will be sent using that email transport.
  
  The issue could be with the HTML markup of your email. Craft's system emails are very simple markup-wise, but if your custom email is more complex, that could explain why system emails arrive as expected but ones from your module do not.
  
  The markup might be invalid or there's something in the content that's triggering the spam filters. If you can, it's worth checking the email in a tool like [Litmus](https://www.litmus.com/) or [HTML Email Check](https://www.htmlemailcheck.com/) to make sure it's going to display correctly in your target email platforms. These are paid-for apps, though, and Litmus in particular isn't cheap.
  
  Alternatively, you could try breaking up your email markup into Twig partials, commenting out virtually everything and then adding sections back one at a time to try and identify which part of the email could be causing it to be classified as spam.
  
  You should also make sure that the domain you're using with Mailgun is fully set up with the appropriate entries for SPF, DKIM and DMARC. ([more here](https://www.mailgun.com/blog/deliverability/email-authentication-your-id-card-sending/))
  
  Finally, the IP address that an email is sent from can have an effect on an email's spam score. Mailgun is a well-known ESP, but when I've used them in the past I ended up upgrading to use a static IP to gain better deliverability. This could be worth a try if nothing else helps.
  
  HTML email is a PITA at the best of times, and I've had similar experiences where emails have been classified as spam. I found that a combination of the methods above helped enormously. Best of luck!
  
  
  ---
  
  