Emails keep being sent from Example <hello#example.com> - laravel

Already read a lot and even did php artisan config:clear
Checked thourghout all files and hello#example.com is nowhere.
It works from local w/o problems.
Also, have another app in plain PHP that uses PHPMailer and this issue does not happen so I don't think it would be something related to the server.
Any help appreciated hence this script is only waiting this issue to be solved so it can be live.
UPDATE: The issue was with the mail server. We changed the mail server
to a different machine but didn't configure completely the DNS.
Thanks to both for answering.

Email address is configured in config/mail.php file
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
but how you can see it is using environment vars configured in .env in root directory
MAIL_FROM_ADDRESS=info#blabla.com.ar
MAIL_FROM_NAME="San Marino Pastas"
Remeber if you are using artisan serve or queue listining restart the services to take the new config.
Please try this and let me know how it works :)

1)First of all you have to set the environment variable in .env file
MAIL_DRIVER=mail
MAIL_HOST=mail.abc.com
MAIL_PORT=465
MAIL_USERNAME=info#abc.com
MAIL_PASSWORD=abc123
MAIL_ENCRYPTION=null
2) Then import this in your controller,
use Illuminate\Support\Facades\Mail;
3) Then add this function in your controller,
public function basic_email() {
$data = array('name'=>"Virat Gandhi");
Mail::send(['text'=>'mail'], $data, function($message) {
$message->from('info#abc.com');
$message->subject('Testing');
$message->to('abc#gmail.com');
echo "Basic Email Sent. Check your inbox.";
});
4) I hope this helped you.

Related

Laravel 8 sending mailgun via api Client error: Unauthorized response: Forbidden

I recently upgraded my Laravel from 5.7.29 to 8.51. I have been using the Mailgun API to send emails for years. The old version of the site is still able to send via mailgun, but the new version keeps returning this message:
Client error: `POST https://api.mailgun.net/v3/mg.clstracking.com/messages.mime` resulted in a `401 Unauthorized` response: Forbidden
I found similar posts that indicate that kind of response if you're working in the EU and fail to change the MAILGUN_ENDPOINT in the services config. The server and site are all in the USA. I have verified I have the correct settings in my .env and that those are passed into my services.php config file and cached using artisan config:cache. I verified this by looking through the /bootstrap/cache/config.php and everything is there - I even checked this against another site with a different domain that is working the same way. I even tried hard-coding my domain and secret into the services.php.
If I change
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
with my username and password, I get no error, but the email isn't sent and checking the logs on mailgun, there is no record. I'm at a loss what else to try.
In env I have:
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=mg.mydomain.com
MAILGUN_SECRET=key-##########################
In config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],
In config/mail.php:
'default' => env('MAIL_MAILER', 'mailgun'),
'mailers' => [
'mailgun' => [
'transport' => 'mailgun',
],
],
I use this config in my projects which are in production with gmail
MAIL_MAILER='sendmail'
MAIL_HOST='sendmail.googlemail.com'
MAIL_PORT=587
MAIL_USERNAME='email#gmail.com'
MAIL_PASSWORD="*********"
MAIL_ENCRYPTION='tls'
MAIL_FROM_ADDRESS='email#gmail.com'
MAIL_FROM_NAME="${APP_NAME}"
Maybe helps you.

Laravel 5 Mail::raw works in tinker but doesn't in app

I've a very strange issue sending SMTP email from Laravel
Mail::raw('Email body', function ($message) use ($filename) {
$message->from('sender#email.com', 'Ordini')
->to('user#email.com')
->subject('subject')
->attach(storage_path($filename));
});
This code actually works within server's tinker environment but it doesn't when it's executed within controller.
I've double checked .env and config/mail.php, everything is fine
The server mail is my client's smtp mail server
I also tried without any attachment
The response is "Expected response code 250 but got an empty response"
I can't figure out why it works in tinker while it doesn't in app. What's the difference?
Thanks in advice
Make sure your you have not enabled two step authentication on your gmail and you have allowed less secured apps on your google account
After Checking all that Just try to run command
php artisan config:clear
php artisan cache:clear
php artisan view:clear
Make sure your env file contain these info
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
And then
Instead of your mail code code try like this
Mail::raw('Email body', function ($message) use ($filename) {
$message->from('sender#email.com', 'Ordini');
$message->to('user#email.com');
$message->subject('subject');
$message->attach(storage_path($filename));
});
Check in the config/mail.php
If it is getting every thing from env or not if not make changes in config file

SOLUTION for: Expected response code 250 but got code "530", with message "530 5.71 Authentication required

This first code was what solved everything. Use this code exactly inside your config/mail.php file. With your own full Gmail address and Google app password.
Where I got the solution
'username' => env('MAIL_USERNAME','full-gmail-address-here'),
'password' => env('MAIL_PASSWORD','Google-app-password-generated-when-setting-up-2-factor-auth'),
You might have written this 2nd code but here it is ... Goes inside your env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587 (or use 465 for ssl on the sixth line below)
MAIL_USERNAME=Your-full-Gmail-address-here
MAIL_PASSWORD=Your-Google-App-Password-here
MAIL_ENCRYPTION=tls (or ssl)
MAIL_AUTH=YES (<= I made this up, if you're a pro you can check if it's necessary)
MAIL_FROM_ADDRESS=Email-address-of-sender
MAIL_FROM_NAME=Name-of-sender
Thirdly, don't forget to include this inside the config/mail.php file
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
Please pass it on to whoever may need it. Thanks
When change in .env you just clear cache & config
php artisan cache:clear
php artisan config:clear
Then test your .env data
php artisan tinker
config('mail')
check your data is correct or not.Your process is correct here.

laravel with mailgun set up

I read everything I can
and looks like I set it up and fill in everywhere according to the docs (and internet) but still cant receive any emails.
I need to use MailGun API so I can send emails from my localhost as well as from the test and prod + because 25 port usually closed.
I filled in everything I can just in case some miracle happen (was changing between smtp/mailgun in .env)
I have tried both my real server settings and when I fail tried without any success sandbox as well.
my mailgun data access (some letters hidden):
my mailgun sandbox server
http://i.imgur.com/XCi8Hkn.png
when i click to it i have an option to choose between API and SMTP
2.1 in API tab i have API KEY: http://i.imgur.com/xGSq7aF.png
2.2 in SMTP box i have smtp host, port, login, passwd: http://i.imgur.com/t5Pi6lH.png
my .env:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox8ffe89553e224c468f4ad0cf6b4da3c2.mailgun.org
MAILGUN_SECRET=b3ff30bxxxxxxxxxxxxxxxx087d-f877bd7a-01ecceaa
somewhere in internet found that MAILGUN_DOMAIN & MAILGUN_SECRET
could be added in .env so tried them here as well
config/mail.php
'driver' => env('MAIL_DRIVER', 'mailgun'),
app/mail/Test.php
public function build()
{
return $this->from('support#sandbox8ffe89553e224c468f4ad0cf6b4da3c2.mailgun.org')
->subject('HELLO')
->view('test');
}
sandbox "to" email verified http://i.imgur.com/tDhgLcT.png
TestController
use App\Mail\Test;
use Illuminate\Support\Facades\Mail;
class TestController extends Controller
{
public function test()
{
echo 'hello';
try {
Mail::to('myaddress#gmail.com')->send(new Test());
} catch (\Exception $e) {
dump($e);
}
echo 'hello2';
}
}
resources/views/test.blade.php
hello
was using even so just in case
> artisan cache:clear
> artisan config:cache
> artisan cache:clear
9
please help, what is missing ? what i did wrong ?
The _HOST, _PORT, _USERNAME, _PASSWORD and _ENCRYPTION environment variables apply to SMTP only.
For that, your MAIL_DRIVER should be set to SMTP, with values for the five I just mentioned pointing to your mailgun values.
The _DOMAIN and _SECRET apply only to when you have set MAIL_DRIVER=mailgun (which means the mailgun api), and that means you are not using SMTP driver anymore.
Basically you have a choice: pick SMTP and point it to mailgun (set MAIL_DRIVER=smtp, OR mailgun API (set MAIL_DRIVER=mailgun).
I think if you think carefully till you understand the difference between those two, then you should be able to work it out.
Also, on the sandbox domain, you can't send real email unless you add the recipient address as a verified email. You do that on the mailgun dashboard. That might be what is causing the problem here (it is a sandbox after all - it's meant to be safe so you can't accidentally send to live email addresses you haven't specifically opted in with via an email verification by the specific address).
Another tip, go in to tinker and type config('mail') and you'll see what is getting picked up by the framework. Then you won't have to adjust the actual config files, if you see the values you want to put there are definitely there.
I also just noticed your config file you listed above is wrong. You have 'driver' => env('mailgun', 'mailgun'), That won't work (as you'll see if you do config('mail') in tinker). the env() function takes the name of an environment variable (like MAILGUN_DOMAIN or MAILGUN_SECRET as its first param, the second param is a default value if the shell environment variable of that name doesn't exist).
You need to use the name of your ENV setting. In your case you are for example for the username trying to get an env variable named 'postmaster#sandbox8ffexxxxxxxxxxd0cf6b4da3c2.mailgun.org' that of course does not exist.
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('SUPPORT', 'hello#example.com'), //if SUPPORT env does not exist, default to hello#example.com
'name' => env('NAME', 'Example'),
],
'username' => env('MAIL_USERNAME'),
'password' => env('PASSWORD'),
when using env , first parameter ie. ENV_NAME is the name you have set in your .env file and second (optional) parameter is the value to which it defaults if the first value is not found.

Laravel keeps using hello#example.com with changed conf

In mail.php I have changed from fields but I still get mails with demo values. There is no reference to hello#example.com in project, yet I still get this in mail.
'from' => [
'address' => 'do-not-reply#mysite.net',
'name' => 'MySite.net',
],
Does anyone have an idea what might be wrong?
Clear the config cache by running the following Artisan command:
php artisan config:clear
If you are using supervisor, ensure you set the right user in the conf file, otherwise you will see the emails are being sent as hello#example.com.
In my case the user is www-data - I set it up as root before and was getting this issue.

Resources