Mailgun in Laravel not working? - php

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.

Related

Laravel - No emails sending using mailgun

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

Laravel 5.7 change verification mail sender

Currently, when a Laravel user verification mail is sent, the sender is hello#example.com with a name of Example. How can I modify these values without changing files in the vendor directory (e.g. php artisan vendor:publish, but what?). I´ve looked at this question, \Illuminate\Auth\Notifications\VerifyEmail.php and \Illuminate\Auth\MustVerifyEmail.php, but while a markdown template is declared (which I´ve published to my resources to tweak it a little), there´s no code which actually sends the mail with sender and sender name!?
It can be found in config/mail.php, see the snippet:
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
Add the values to your .env file and it should be good.

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.

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

Sending email with swiftmailer using the Yii2 extension

I'm working on a project where I'd like to test the emails being sent out to users, but so far i got no luck setting it up. I'm using Codeception and running it inside Vagrant. What I'm trying to do now:
return \Yii::$app->mailer->compose()
->setFrom($from)
->setTo($to)
->setSubject('Welcome')
->send();
I've got a main-local.php, which looks like this:
'mailer'=> ['class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => 1025,
'useFileTransport' => false,
],
At first I left out 'useFileTransport' => false, then I added it before 'transport' and tried moving it inside 'transport', as you can see now, but it's the same. The file/email gets created in the project as .eml file, but no mail is sent. I tried setting up mailcatcher (http://mailcatcher.me/), and actually got that working, but it would be nice if I could just change in the config, instead of using a different method and changing code, and the mail would be sent where I want it. (I also tried with port 1080, so that's not it.)
Any help would be appreciated. If you need more info, please let me know!
Update:
I found out that I got a different config file, where I'm actually able to use 'useFileTransport' => false, and then it won't create the file/email in the project, but still not sending it. I copied the settings shown from main-local.php into this config file, moved 'useFileTransport' => false up before 'transport', but I'm still looking for a way to send these emails
$useFileTransport is a property of the yii\swiftmailer\Mailer class, so it does not belong into transport part.
If your mailer is configured like the following, it will try to deliver mails via SMTP to the mail server running on your localhost on port 1025:
'mailer'=> [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => 1025,
],
],
This sounds somehow unusual to me as the default port for SMTP is 25.
Make sure the SMTP server is actually running on that port. E.g. by using the telnet command:
telnet localhost 1025
which should give you something like this:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 your.servername ESMTP Postfix
Exit the SMTP session by typing quit and hit enter.
You may also check the mailserver log file which is usually located at /var/log/mail.log. You can view messages being logged using
tail -f /var/log/mail.log
while you are trying to send them.
Took me a few days, but finally found the problem!
So the problem was the other config i mentioned in the update section. Hidden inside codeception/config/config.php was missing a line
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
//For email creation in project
//'useFileTransport' => true,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => 1025,
'encryption' => '',
],
],
All i needed was the encryption, which just needs to be empty. Emails are now being sent how they're supposed to, and looking good!
Hope this helps somebody out there, happy testing!

Categories