Send email without env variables in Laravel 8 - laravel

Laravel version - 8
What Am I trying to do?
Sending email without env variable.
Error Details
Connection could not be established with host mailhog :stream_socket_client(): php_network_getaddresses: getaddrinfo failed: No such host is known.
Am i missing anything?
Code
config('MAIL_DRIVER', 'smtp');
config('MAIL_USERNAME', "email username");
config('MAIL_HOST', "smtp.gmail.com");
config('MAIL_PASSWORD', "password");
config('MAIL_PORT', "port");
config('MAIL_ENCRYPTION', "tls");
$invitedUser = new Admin();
$invitedUser->email = "recipient email address";
$invitedUser->notify(new TestingNotification());

config(['mail.mailers.smtp.host' => "host"]);
config(['mail.mailers.smtp.encryption' => "tls"]);
config(['mail.mailers.smtp.username' => "username"]);
config(['mail.mailers.smtp.password' => "password"]);
config(['mail.mailers.smtp.port' => "port"]);
config(['mail.mailers.smtp.from' => "from"]);
$invitedUser = new Admin();
$invitedUser->email = "recipient";
$invitedUser->notify(new TestingNotification());

It seems like there's something wrong with your configuration. The error mentions mailhog as a host, but that probably is not what you wanted.
Maybe you wrote things like config('MAIL_HOST', "smtp.gmail.com"); as pseudo code, but usually you'd set this up in your mail.php config file. For example:
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.gmail.com'),`.
...
]
]

Related

How to set the laravel conf to use mailgun

I am using Laravel 8 + queues + Mailgun API to send emails from my local machine.
I can't send emails. I have this error :
[2020-12-19 11:50:26] local.ERROR: Connection could not be established with host smtp.mailgun.org :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:1408F10B:SSL routines:ssl3_get_record:wrong version number {"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.mailgun.org :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:1408F10B:SSL routines:ssl3_get_record:wrong version number at C:\\Users\\Dominique\\Documents\\Workspace\\market-gardener\\market-gardener-back\\vendor\\swiftmailer\\swiftmailer\\lib\\classes\\Swift\\Transport\\StreamBuffer.php:269)
[stacktrace]
I think I have a configuration issues somewhere. I tried a lot of things without success. My conf :
.env :
MAIL_PORT=2525
MAILGUN_ENDPOINT=api.mailgun.net
MAILGUN_DOMAIN=sandbox___xxxxx_____________.mailgun.org
MAILGUN_SECRET=secret___xxxxxxxxxxxxxxxxx______
MAIL_ENCRYPTION=ssl
config/email :
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
Is this kind of conf correct? Where are my errors? Of course I already tried to clear the cache and config.
Here's a working version of the Mailgun setup I use:
MAIL_HOST=smtp.eu.mailgun.org
MAIL_PORT=587
MAIL_FROM_ADDRESS=address#example.com
MAIL_FROM_NAME=example
MAIL_USERNAME=address#example.com
MAIL_PASSWORD="123abc456def"
MAIL_ENCRYPTION=tls
The keys are coupled to the default config/mail.php file as seen here:
https://github.com/laravel/laravel/blob/8.x/config/mail.php
Note the usage of tls and port number 587. The MAIL_HOST may also differ. Mailgun should probably have a page on their website where this configuration is outlined.
A parameter was missing in the.env (see the doc here)
I only added MAIL_MAILER=mailgun in the .env , and it works fine now.
My .env is now :
MAIL_MAILER=mailgun
MAIL_PORT=2525
MAILGUN_ENDPOINT=api.mailgun.net
MAILGUN_DOMAIN=sandbox___xxxxx_____________.mailgun.org
MAILGUN_SECRET=secret___xxxxxxxxxxxxxxxxx______
MAIL_ENCRYPTION=ssl
You have to make sure you provide all the values of the environment variable
if not it won't work. in my own case, I added this line in my env file
MAILGUN_SECRET=6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd

Send Mail Using Lumen

Mail.php
return [
'driver' =>'smtp',
'host' => 'smtp.gmail.com',
//'port' => 587,
'port' =>465,
//'encryption' =>'tls',
'encryption' =>'ssl',
'username' => 'xxxxxxxx#gmail.com',
'password' => 'xxxxxxx',
// 'sendmail' => '/usr/sbin/sendmail -bs',
'sendmail' => '/usr/sbin/sendmail -t',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Controller
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Error
Swift_TransportException Connection could not be established with host
smtp.gmail.com [A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
I Tried...
Change ssl / tls
Change the ports
Add "guzzlehttp/guzzle": "~5.3|~6.0" in composer.json
Add a new line in StreamBuffer.php
$options = array_merge($options, array('ssl' => array('verify_peer' =>
false,'verify_peer_name' => false,'allow_self_signed' => true )));
Please help .
Thank you.
1. Require illuminate/mail
Make sure you’re using the same version as your underlying framework (i.e. if you’re on Lumen v. 5.3, use composer require illuminate/mail "5.3.*").
composer require illuminate/mail "5.5.*"
2. Set up Lumen bootstrap/app.php
First, open your bootstrap.php and uncomment the following lines:
$app->withFacades();
$app->register(App\Providers\AppServiceProvider::class);
Also, add the following line below the last line you uncommented:
$app->configure('services');
This will allow you to define a ‘services’ config file and setup your mail service. Now I know that normally configuration is done in the .env file with Lumen, and we’ll use that shortly, but first we’ll need to write a small config file to map to the .env file.
3. Create your configuration files
Create a new folder at the root level of your install called config(if it doesn’t already exist). In the config folder, create two new files, one named services.php and another named **mail.php**.
In the services.php file paste the following:
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
];
Lastly, add the following to your .env file:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=<your-mailgun-domain>
MAILGUN_SECRET=<your-mailgun-api-key>
Make sure you replace those sneaky placeholders with your actual key and domain. If you’re not using Mailgun, you can always use the other mail providers Mail comes with; have a look at the docs if you plan on using a different provider, they are all easy to set up once you’re at this point.
4. Send Email!
To send an email, use one of the following in your classes (depending on your preference):
use Illuminate\Support\Facades\Mail;
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Finally, don’t forget to read the Laravel Mail docs for more info on how to use this great library.
Have you turned on 2 layers security in your Google account (email address you config in .env file) which uses to send email.

Laravel 5 Mail - Gmail - AbstractSmtpTransport.php line 399:Uninitialized string offset: 3

so i'm trouble getting a fairly difficult time getting my mail up and i'm not finding a comparable solution out there. I’m trying to send a basic email through gmail from my website using SMTP. Everything seems correct. Thanks for your help
• my account is xxx#gmail.com. i've set up the two step verification on this account. the xxx#gmail.com account has password a123
• i'm trying to send it from my email marketing#y.com. for xxx#gmail.com
• under gmail-settings-account and import- send mail as
• i have
o Marketing -Not an alias.
o Mail is sent through: smtp.gmail.com
o Secured connection on port 587
o using TLS
• clicking on edit info- Name:
o Marketing
o Email address: marketing#y.com
• Click Next - Edit email address - Send mail through your SMTP server
o Configure your mail to be sent through SamsSocial.com SMTP servers Learn more
o You are currently using: secured connection on port 587 using TLS
o To edit, please adjust your preferences below.
o SMTP Server: smtp.gmail.com
o Port: Username: xxx#gmail.com
o Password: OTHERPASS
o Secured connection using TLS (recommended)
i'm getting the following error and it's taking an incredibly long time
ErrorException in AbstractSmtpTransport.php line 399:
Uninitialized string offset: 3
in AbstractSmtpTransport.php line 399
at HandleExceptions->handleError('8', 'Uninitialized string offset: 3', 'C:\wamp\www\d\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php', '399', array('seq' => '8', 'response' => '�334 VXNlcm5hbWU6 L', 'line' => 'L')) in AbstractSmtpTransport.php line 399
at Swift_Transport_AbstractSmtpTransport->_getFullResponse('8') in AbstractSmtpTransport.php line 277
at Swift_Transport_AbstractSmtpTransport->executeCommand('AUTH LOGIN ', array('334'), array()) in EsmtpTransport.php line 270
at Swift_Transport_EsmtpTransport->executeCommand('AUTH LOGIN ', array('334')) in LoginAuthenticator.php line 40
controller
public function sendEmailReminder()
{
$user = User::findOrFail(1);
// dd(Config::get("mail"));
Mail::send('admin.marketing.emails.test', ['user' => $user], function ($m) use ($user) {
//i've had this with and without the from
$m->to('test#yahoo.com', 'peter')->subject('This is how we do it');
});
return redirect('admin/marketing');
}
Test.blade.php
Hi {{$user['name']}}. this is the first email we've sent
Config/mail.php
<?php
return [
'driver' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'Marketing#t.com', 'name' => 'Marketing'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME',xxx#gmail.com'),
'password' => env('MAIL_PASSWORD','OTHERPASS’),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xxx#gmail.com
MAIL_PASSWORD=OTHERPASS
MAIL_ENCRYPTION=tls
When I do dd(Config::get("mail")); - I get the following which appears correct
array:9 [▼
"driver" => "smtp"
"host" => "smtp.gmail.com"
"port" => "587"
"from" => array:2 [▼
"address" => "Marketing#y.com"
"name" => "Marketing"
]
"encryption" => "tls"
"username" => "xxx#gmail.com"
"password" => "OTHERPASS"
"sendmail" => "/usr/sbin/sendmail -bs"
"pretend" => false
]
The error might be misleading, but it is actually caused because authentication against gmail smtp servers produced an unexpected output. Google has changed the way authorization works for client apps.
Now it is necessary to retrieve an App Password for the app that is accessing Mail or Calendar in your behalf, and use that App Password instead of the usual password that you use for web.
More info here: https://support.google.com/accounts/answer/185833?hl=en

laravel 5.2 and mailgun

This should be something really simple but all the tutorials are too old and I really don't know what half of these things are:
In config/mail.ph
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
So in my .env file I have:
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=(Default SMTP Login <- found in mailgun)
MAIL_PASSWORD=(Default Password <- found in mailgun)
I beleive these are all good. Next in config/services.php I have
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
And further in my .env file:
MAILGUN_DOMAIN=(API Base URL <- found in mailgun)
MAILGUN_SECRET=(API Key <- found in mailgun)
but when I run the artisan queue:listen I get error:
Client error: `POST https://api.mailgun.net/v3/-API Base URL-/messages.mime` resulted in a `404 NOT FOUND` response.
where -API Base URL- is the value of the MAILGUN_DOMAIN variable in my .env file
I tried putting my domain name mail.mydomain.com in the MAILGUN_DOMAIN but then I got the message to activate my account although the account was already verified and hence, I believe, active.
I tried leaving the MAILGUN_DOMAIN empty and then I got the
`POST https://api.mailgun.net/v3//messages.mime` resulted in a `404 NOT FOUND`
So, my conclusion is there should be something between ...v3/ and /messages.mime but I can't figure out what.
EDIT:
I found this:
https://laracasts.com/discuss/channels/laravel/laravel-and-mailgun-1
and it seems i can remove the MAIL_USERNAME and MAIL_PASSWORD but I don't know where to find this sandboxxxxxxxx code.
The right thing to enter is your domain name that you set up in mailgun. In my case it was mail.mydomain.com.
However, I never bothered to activate the account (you need to give them your phone number so that's most likely the reason I skipped it) so I was getting this message. Problem is solved now.

Sending Email with mailgun in laravel error

Hello i've been simply trying to send an email in laravel i read the documentation and they made it seem so easy but whenever i try i keep getting error after error, i tried sendgrid didn't work and now i'm trying to use mailgun but i'm having issues with it as well.
This is my code::
$data = array();
Mail::send('emails.auth.activate', $data, function($message)
{
$message->to('xxxxxxxxx#gmail.com', 'John Doe')->subject('This is a demo!');
});
This is the error i get:
GuzzleHttp \ Exception \ ClientException (400)
Client error response [url] https://api.mailgun.net/v2/mail.xxxxxxx.com/messages.mime [status code] 400 [reason phrase] BAD REQUEST
Mail Config:
<?php
return array(
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
|
*/
'driver' => 'mailgun',
'host' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org',
'port' => 587,
'from' => array('address' => 'mail#xxxxxx.com', 'name' => 'Xxxxxxxx'),
'encryption' => 'tls',
'username' => 'xxxxxxx#gmail.com',
'password' => 'xxxxxxxxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => true,
);
Follow these steps
First of all, install guzzle package by adding "guzzlehttp/guzzle": "~4.0" line inside composer.json
Update composer using composer update
Create your account on mailgun from http://www.mailgun.com/. After creating account, kindly note the mail gun subdomain created like sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org and API key created like key-65c33f1xxxxxxxxxxxxxxxxxxxx
Go to config/services.php file and replace
'mailgun' => array(
'domain' => '',
'secret' => '',
),
with
'mailgun' => array(
'domain' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org',
'secret' => 'key-65c33f1xxxxxxxxxxxxxxxxxxxx',
),
If you want to create your own sub-domain, you can create and assign to domain (as an alternative)
Configure config/mail.php like this
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => array('address' => 'mail#xxxxxx.com', 'name' => 'Xxxxxxxx'),
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false
Note that you do not need to supply username and password for this. Mailgun will handle this.
Try sending email now. Hope this helps. Good luck!
Just wanted to add one possible reason for the error. I received this error while using sandbox mode when I hadn't set the authorized recipient yet. When I logged into Mailgun and added the intended recipient to the "Authorized Recipient" list the error went away.
I had the same issue and kept getting the following error:
Client error response [url] https://api.mailgun.net/v3//messages.mime 404 not found
There isn't much written about this error written online for Laravel 5.1 which I'm using. It turns out that in the config->services the default Laravel 5.1 installation comes with :
'domain' => env('');
'secret' => env('');
for some reason if you keep your domain and secret wrapped in env as per default installation, the MailGunTransport doesnt pick it up. So just set it to the following:
domain' =>'yourdomain';
'secret' => 'yoursecret';
Hope this helps since I'm sure i'm not the only one who probably run into this issue.
I had this problem as I hadn't activated my mailgun account. Once activated all worked fine
I also stucked once in configuring the Mailgun in Laravel 5.1 and what worked for me is the following process. Hope its helps someone:
1) Install guzzle package by adding "guzzlehttp/guzzle": "5.0" line inside composer.json like this:
"require": {
"guzzlehttp/guzzle": "~5.0"
},
2) Update composer using sudo composer update in the terminal.
3) Restart Apache.
4) Create the account in http://www.mailgun.com. It will create a Sub Domain and API Key.
5) Add the Sub Domain and API Key in the .env like this:
MAILGUN_DOMAIN=sandbox8...........3b.mailgun.org
MAILGUN_SECRET=key-9..................04
6) And in the services.php add these line:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
7) Now the mail.php as follows:
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => ['address' => null, 'name' => null],
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
I hope this works for you all.
You will be able to send mail to the persons in the same domain unless you have added them in the authorised recipient category.
If you add someone to authorised recipient category then he/she would have to approve the request and then only would be able to receive the emails.

Resources