Laravel - No emails sending using mailgun - php

Server: Digital Ocean
Ubuntu 16.04
Laravel 5.8
I cannot get email to send out of laravel using mailgun.com
In Digital Ocean I have all outgoing ports open on the firewall, I have the correct DNS settings in Digital ocean for TXT and MX records. I have the correct and verified DNS records on my domain registar and mailgun has a green checkmark on all
config/mail.php
return [
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => [
'address' => 'orders#domain.com',
'name' => 'Company Name'
],
'encryption' => 'tls'),
'username' => 'orders#mg.domain.com',
'password' => 'xxxxd663hd02j727bb2eefd1ea38bbe0-58bc211a-670xxxx'
];
config/services.php
'mailgun' => [
'domain' => 'https://api.mailgun.net/v3/mg.domain.com',
'secret' => 'xxxxehbe8v25g3374e5as3ff32a45995-39bc661a-4716xxxx',
],
Controller
use Illuminate\Support\Facades\Mail;
$data = [
'email' => 'email#yahoo.com',
'name' => 'Bob Smith'
];
$body_data = [
'id' => '1234'
];
Mail::send('emails.shipped', $body_data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Test Email');
});
When I change mail driver to log and then check log file it looks great. Everything looks perfect and I have used mailgun before on Laravel 5.5 with no problems.
I have also tried the new laravel build method and same issue.
I get no errors, I checked logs on apache2, no logs are appearing in mailgun and of course no email comes through in inbox or spam.
My question is, am I missing anything? What other troubleshooting can I do? Seems like my app isn't connecting to mailgun correctly.

I think that in your config/services.php the mailgun.domain should be more like
mg.domain.com (or sandboxXXXXXXX.mailgun.org if that's a dev environment), and not a url like the one you've set.

Try to put:
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
in your mailgun array.

I'm using laravel 5.8 and it's working with all default configuration
.env
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox8a408833ad1540e7b3a5d0151f606531.mailgun.org
MAILGUN_SECRET=92df7e85eeeaccaeae3d3b3164600666-87cdd773-8c819599
web.php
Route::get('send_test_email', function(){
Mail::raw('Sending emails with Mailgun and Laravel is easy!', function($message)
{
$message->to('your_test_email#gmail.com');
});
});
services.php
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],

I struggled with this very same problem (laravel + mailgun) for a FULL DAY.
This is what ultimately solved my problem. Hope this helps!
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=mail.mydomain.com
MAILGUN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxx
MAIL_FROM_ADDRESS=kp#mydomain.com
MAIL_FROM_NAME='KP'
In routes/web.php:
Route::get('/tst', function(){
Mail::raw('Sending emails with Mailgun and Laravel is easy!', function($message)
{
$message->to('kp#yahoo.com', 'K P')->subject('Hello there, how are you?');
});
echo "string";
});
Note: you will need to make sure that your MAILGUN_DOMAIN is set and the MX / DNS records exist on your server / domain. This can take up to 24 hrs to propagate (sadly, the most annoying part). But these are all the settings you would need.

You may try Installing SwiftMailer library in your server.
sudo apt install -y php-swiftmailer

Related

Laravel ElasticEmail Config

I'm struggling with configuring Laravel email. This is my first Laravel project, I am WordPress Web Developer.
And this is the error code I'm getting.
Expected response code 250 but got code "", with message ""
What am I doing wrong?
This is .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.elasticemail.com
MAIL_PORT=2525
MAIL_USERNAME=email#managedcloudhostingemail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=TLS
This is mail.php in config folder
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.elasticemail.com'),
'port' => env('MAIL_PORT', 2525),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#bummit.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
I've tried almost every possible combination of ports, ssl, tls and can't figure out why email doesn't work. Email should be sent to new user registration, and I get this error, while user gets saved to database.
I have actuallly got wrong credentials from support, and sonce we're using Cloudways and have subscribed to Elastic Email through them, I had to use API key as username and password. Then everything worked like a charm.
I think you would need some more code to achieve this with ElasticMail.
Take a look at this wrapper, it allows you to easily use your ElasticSearch account.
You might want to use API instead of SMTP, as it is a better way to do that in the long term.
We constantly work on adding new plugins to most popular tools so a ready made solution will show up. I can't give you an exact date just yet.

Laravel Gmail Configuration Error [duplicate]

This question already has answers here:
How to to send mail using gmail in Laravel?
(17 answers)
Closed 3 years ago.
I have been having this issue with Laravel email with Gmail and I have checked and reviewed so many questions on Stack Overflow but none still works in my case. I am using Laravel 5.4 and Xampp.
At first I thought it was my Xampp that cannot allow sending the emails but also when I move to the live mode(hosted it on a shared hosting) still have same issues there as well
But whenever I set the configuration to use Mailtrap, it's working and I know mailtap is just a development mail server, but whenever I use the Gmail it give me error such as "Expected response code 220 but got code "", with message """
Also when I try it on live mode it still gives same error.
My configuration file has the following
.env
MAIL_DRIVER=smtp
MAIL_HOST=gmail-smtp-msa.l.google.com (also tried smtp.gmail.com)
MAIL_PORT=587
MAIL_USERNAME=email#gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=ssl
mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'gmail-smtp-msa.l.google.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => 'email#gmail.com',
'name' => 'Name Here',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'email#gmail.com'),
'password' => env('MAIL_PASSWORD', 'password'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Hey here is the solution
MAIL_DRIVER='smtp
MAIL_HOST='smtp.gmail.com'
MAIL_PORT=587
MAIL_USERNAME='sample#gmail.com'
MAIL_FROM_ADDRESS='sample#gmail.com'
MAIL_FROM_NAME='Some Name'
MAIL_PASSWORD='XXXXX'
MAIL_ENCRYPTION='tls'
As well as the configuration on the Laravel side, you need to enable "Less secure apps" in your Gmail account.
On the Laravel side, this guide shows the settings you need. Use smtp.gmail.com for host, and either 465/ssl or 587/tls.

Unable to send password reset email using Laravel built in mail

I have been trying to get emails to send from Laravel for the past day without success. I have been testing locally using homestead and on a live linux server environment with no luck.
I have removed email settings from the .env file. and directly added the settings in my config/mail file.
so far I have tried office 365 settings, gmail and finally maligun, none of which send an email.
my settings are below.
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'postmaster#sandbox********099b827475b585029.mailgun.org'),
'name' => env('MAIL_FROM_NAME', 'Test account'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('postmaster#sandbox****************099b827475b585029.mailgun.org'),
'password' => env('********************'),
I complete the password reset process via password/reset and it says the password reset link was sent successfully but it is never sent, the mailgun logs confirm no email was sent, the token is updated in the password_reset table
I ssh'd onto homestead and tested sending a email via mailgun using curl and this works.
if I set mail driver to SMTP I still get the same issue.
when setting the drive to 'log' the log files shows the email template etc.
any help would be appreciated.
thanks
What environment you working with? Local or production?
What kind of email you're using? Gmail or testing mail such as mailtrap?
If gmail did you active smtp from settings? Please provide more information.
Update:
let us test other ways to see if it makes any differences:
Go to your routes file web.php and comment this:
// Auth::routes();
Now add following routes:
// Authentication routes
Route::get('/login',['as'=>'login', 'uses' => 'Auth\LoginController#showLoginForm']);
Route::post('/login', ['uses'=>'Auth\LoginController#login']);
Route::get('/logout',['as'=>'logout', 'uses'=>'Auth\LoginController#logout']);
Route::get('/register', ['as' => 'register', 'uses' => 'Auth\RegisterController#showRegisterForm']);
Route::post('password/email', ['as'=>'password.email', 'uses'=>'Auth\ForgotPasswordController#sendResetLinkEmail']);
Route::get('password/reset', ['as'=>'password.request', 'uses'=>'Auth\ForgotPasswordController#showLinkRequestForm']);
Route::post('password/reset', ['as'=>'password.request', 'uses'=>'Auth\ResetPasswordController#reset']);
Route::get('password/reset/{token}', ['as'=>'password.reset', 'uses'=>'Auth\ResetPasswordController#showResetForm']);
Route::post('logout', ['as'=>'logout', 'uses'=>'Auth\LoginController#logout']);
Try and see if it works or not.

sending mail in Laravel 5.4 using Mailgun get error code " 401 UNAUTHORIZED` response: Forbidden "

I'm trying to send mail in Laravel 5.4 project with Mailgun. I think I set the configuration correctly. But, I got this error message such as
ClientException in RequestException.php line 111:
Client error: POST https://api.mailgun.net/v3/sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org/messages.mime >resulted in a 401 UNAUTHORIZED response:
Forbidden
Here is my configuration:
in .env file
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org
MAILGUN_SECRET=pubkey-1767e**********
in mail.php file
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'richi.htoo#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'Richi Htoo'),
],
in services.php file
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
and I wrote mail sending code in default route such as
Route::get('/', function () {
//return view('welcome');
$data = [
'title' => 'Hi student I hope you like the course',
'content' => 'This laravel course was created with a lot of love and dedication for you'
];
Mail::send('emails.test', $data, function($message){
$message->to('white.nuzzle#gmail.com', 'White Nuzzle')->subject('Hello student how are you?');
});
});
And I also installed Laravel Package "guzzlehttp/guzzle" version 6.2 to send mail.
But when I call that default home route, I got an error message as I mention above.
I can't find any solution for my error in any where including this forum "stackoverflow.com".
Can anyone help me please?
Ok found it,
In the file "vendor\laravel\framework\src\Illuminate\Mail\Transport\MailgunTransport.php", the endpoint used is US one.
As said in documentation, https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions, you have an endpoint for US and EU.
If you are european, you must use "api.eu.mailgun.net" or you get a 401.
Just change the endpoint: Laravel is powerful and they think about that. You can add an 'endpoint' key to the config/services.php/mailgun entry.
Frankly it was quite an ordeal, I made the sandbox work as following
Added authorized recipient - Apparently sandbox can't send email to
anyone, you need to add them as authorized recipient.
Use proper credentials in .env File added MAILGUN_DOMAIN and MAILGUN_SECRET too as services.php uses them. Remember MAILGUN_SECRET is the private key and starts with key-, don't use public key here
Put
MAIL_DRIVER=mailgun MAIL_HOST=smtp.mailgun.org MAIL_PORT=587 MAIL_USERNAME=postmaster#sandboxcc*****************.mailgun.org MAIL_PASSWORD=****************** MAIL_ENCRYPTION=tls
!!!MOST IMPORTANT!!!! RESTART YOUR SERVER to load the new .env file.
If you are not using the United States Mailgun region, you can define your region's endpoint in the services configuration file:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),
],
Alternatively, one can define the MAILGUN_ENDPOINT in the .env file:
MAILGUN_ENDPOINT="api.eu.mailgun.net"
Make sure to check your credentials for the mailgun, make sure that those are correct.
Dont copy the Public Validation Key.
please copy the Private API Key
Default Mail Driver: Mailgun
MAILGUN_DOMAIN=*your-domain*.mailgun.org
MAILGUN_SECRET=pubkey-*your-public-key*
Default Mail Driver: SMTP
MAIL_USERNAME=postmaster#*your-domain*.mailgun.org
MAIL_PASSWORD=*your-password*
Don't forget to php artisan config:clear
I had the same issue. Check your DNS settings in the Mailgun Sending domain checklist. I found I could not get an EU MG setting to work so I suggest you use a US MG server
Ok, I just want to summarize what I had to do to make Mailgun work in Laravel 8.
1. Install guzzle
composer require guzzlehttp/guzzle
2. EU or US?
I'm from EU. The Laravel Doc for Mailgun says to configure mailgun for usage in EU. This is WRONG if you're using the sandbox because it is located in the US apparently. I have a small US flag next to my sandbox url and it took me ages to figure this out.
If you want to use the sandbox put this into \config\services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],
If you want to use a domain that is registered for EU you will need to make this change:
'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),
If you put the wrong region, your log should throw 401 UNAUTHORIZED response
3. Authorize Recipients
If you're using the sandbox you need to authorize recipients. Go to your mailgun account and to your sandbox domain and authorize recipients in the email field. If you try to send an email to non-authorized recipients your log should throw an 403 FORBIDDEN response
4. Default Mailer
If you want mailgun to be your default mailer you can change this in your \config\mail.php:
'default' => env('MAIL_MAILER', 'mailgun'),
5. Credentials
These credentials made it work for me, hopefully they will make it work for you too. Put them in .env:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org
MAILGUN_SECRET=2dfXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAILGUN_SECRET is your private api key which can be found when go to your mailgun account and click on your profile logo -> api keys.
MAILGUN_DOMAIN is your (I believe currently used) domain and can be accessed in your mailgun account either through the dashboard or through sending->domains in the navpanel. The only not messy reference field for your domain seems to be in sending->overview-> select SMTP and copy the end part of the Username field (without postmaster#)
I hope this saves time for someone because it took forever to get it to work.
Personally I never got the sandbox account to work. I just did this a little over a month ago. Sandbox never worked, but the live account I created did. Try switching to the live account and let me know if that works for you.
Just go to the config folder. and made some changes..
'mailgun' => [
'domain' => env('your_domainxxxxxxx.mailgun.org'),
'secret' => env('key-xxxxxxxx_private_API_keyxxxx'),
],
error will be resolved.
In case if you use API base mailgun, check if the domain exists.
I removed my testing domain and after it tried to send a new email message from that domain. I got 401 error.
I had the same problem in Symfony. Yes, it's not Laravel but they're close.
In .env file I had
MAILER_DSN=mailgun://KEY:DOMAIN#default
The error disappeared after I changed it to
MAILER_DSN=mailgun://KEY:DOMAIN#default?region=eu

Mailgun in Laravel not working?

I am looking around for some answers to my problem. I really do not know what in my settings or code is wrong but i can't make the sending of email work.
Here is my mail.php values
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => array('address' => 'here is my email', 'name' => 'Some name here'),
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
Here is the services.php
'mailgun' => array(
'domain' =>'my domain here' ,
'secret' => 'my secret key here',
),
Here is my code for Sending.
$data = array(
'msg' => 'You have successfully registered ',
'url' => 'someURL'
);
try{
Mail::send('emails.REGISTRATION_SUCCESS', $data, function($message){
$message->from('admin#something.com', 'y System');
$message->to('myemail#gmail.com')->subject(' Registration Successful!');
});
}catch(Exemption $e){echo $e->getMessage();}
echo 'hello';
I am having this error whenever I run the code.
Class 'GuzzleHttp\Client' not found
I really do not know where in this code i am wrong. I am currently stuck right now. it still not sending any email. I really dont know now. please hep me. I am looking at some tutorials and almost same question post but nothing is working. any suggestions?
From the Laravel mail documentation:
Driver Prerequisites
The API based drivers such as Mailgun and SparkPost are often simpler and faster than SMTP servers. If possible, you should use one of these drivers. All of the API drivers require the Guzzle HTTP library, which may be installed via the Composer package manager:
composer require guzzlehttp/guzzle
in you composer.json file add this "guzzlehttp/guzzle": "~5.3|~6.0" in require portion then composer update for more info click here
or just in your terminal on the laravel root folder run composer require "guzzlehttp/guzzle": "~4.0"
Just do as #Martin suggested
$ composer require guzzlehttp/guzzle
from command-line on your project path.
This will install the required packages. Also refer guzzle github link
Just in case, you are doing this directly on production (which is really very dangerous), you may follow #bashar's answer.

Categories