Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to send an email: Forbidden (code 401) #35

Open
romainpoirier opened this issue Feb 15, 2024 · 1 comment
Open

Unable to send an email: Forbidden (code 401) #35

romainpoirier opened this issue Feb 15, 2024 · 1 comment
Labels

Comments

@romainpoirier
Copy link

Description

I have a paid Mailgun account, with a verified domain.
I can successfully send the Test Email from /admin/settings/email.
However, I can't send from my Controller, while my config is correct.

This is the error I get:

[web.INFO] [yii\mail\BaseMailer::send] Sending email "Test" to "[email protected]" {"memory":29395064} 
[web.ERROR] [yii\symfonymailer\Mailer::sendMessage] Unable to send an email: Forbidden (code 401). {"memory":29915280} 

Note: of course, I have used my own email address instead of [email protected].

And this is the code to send the email:

$email = '[email protected]';
$template = '_emails/test/test';
$subject = Craft::t('site', 'This is a test');
$htmlBody = Craft::$app->getView()->renderTemplate($template, []);

$view = Craft::$app->getView();
$html = $view->renderTemplate($template, $variables , View::TEMPLATE_MODE_SITE);

$response = Craft::$app
    ->getMailer()
    ->compose()
    ->setTo($email)
    ->setSubject($subject)
    ->setHtmlBody($html)
    ->send();

I also tried this instead:

$message = new Message();
$message->setTo($email);
$message->setSubject($subject);
$message->setHtmlBody($htmlBody);
$response = Craft::$app->getMailer()->send($message);

Steps to reproduce

  1. Setup Mailgun settings
  2. Try to send using the code above

Additional info

  • Craft version: Craft Pro 4.7.2.1
  • PHP version: 8.1.13
  • Database driver & version: MySQL 5.7.39
  • Plugins & versions: 3.0.0
@romainpoirier
Copy link
Author

Fixed: I've found out that the endpoint value was missing from my config/app.php file:

$settings->transportSettings = [
    'domain' => App::env('MAILGUN_DOMAIN'),
    'apiKey' => App::env('MAILGUN_API_KEY'),
    'endpoint' => App::env('MAILGUN_ENDPOINT')
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant