Laravel mail from not set - laravel

For my website I try to send emails from an alias (created alias from google for business). My mail function:
Mail::send('emails.tracktrace', ['text'=>$mailtext,'tracking' => $code,'email' => $email, 'name' => $name],
function ($m) use ($code, $email, $name) {
$m->from('info#mydomain.eu', 'Mydomain');
$m->to($email, $name)->subject('Track your package!');
});
Also in my config/mail.php I have:
'from' => ['address' => 'noreply#mydomain.eu', 'name' => 'mydomain'],
But both are ignored and the MAIL_USERNAME value set in my .env file (with the mail configuration) is used.

I ran into the same problem.. Turned out I tried to use a Gmail account which doesn't let you change the 'from' headers.
(see problem with php mail 'From' header)

Related

Laravel 9 Symfony mailer not working - mail sent from app but not received in gmail

In Laravel 8 I was able to send an email verification email. And now in Laravel 9 email sent message appears but I don't receive an email.
I have used my correct mail host provided by domain with port, username, and password as previously and checked enough times. I have also changed ssl to tls but no success.
.env file
MAIL_MAILER=smtp
MAIL_HOST=mail.getalow.com
MAIL_PORT=465
MAIL_USERNAME=********
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS="support#getalow.com"
MAIL_FROM_NAME="${APP_NAME}"
I also added 'verify_peer' => false in mail.php config file but no success.
mail.php
'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,
'verify_peer' => false, // <------ IT HAS TO BE HERE
],
my User.php model also has implements MustVerifyEmail as I do everytime.
class User extends Authenticatable implements MustVerifyEmail
In registration controller event(new Registered($user)); is presented
RegistrationController.php
// Register a new user
public function register(RegistrationRequest $request) {
$user = User::create($request->validated())->assignRole(['User', 'Commenter']);;
event(new Registered($user));
Auth::login($user);
return to_route('home')
->with(Helpers::Toast(
'Welcome ' . $user->name,
'Your account has been created.',
'success',
'5000'
));
}
I tried it from localhost to mailtrap it works. But when I upload it to server with all details of server in env file as above. I receive success message as in registration controller, but I don't receive activation email on my registered email.
I am using shared hosting, cpanel as hosting provider
What am I missing please help. I stucked on it for 4 days.
You said you are using cPanel shared hosting server.
There is no problem in your Laravel 9 code. The problem is on your DNS setting of your email in cPanel. You have to change CNAME, MX, TXT records on cPanel as provided by your hosting provider.
For example:
If you are using ohodomain hosting provider.
Go to account settings.
Select your domain from list of your domains.
Go to Manage Email.
There you will see the error with its solution that you have to solve.
Add the provided CNAME, MX, TXT records on your cPanel's Zone Editor.

OctoberCms send message using SMTP server

I have some problem. I need send message using SMTP, because I want to set up а password when sending. I read this manual https://octobercms.com/docs/services/mail
But I didn't find how I can set up my password, in this manual sets up only from adress
$message->from('us#example.com', 'October');
Can your help me?
Thanks for your answer. But this solution does not solve my problem. In my system I use two address when sending. I use plugin for mail setting from admin panel. This plugin overwrites my settings from config. Accordingly, I must specify all the settings for the second mail dynamically so that they overwrite the settings of the first mail that the administrator specifies in the admin panel.
Sorry for my English
You need to set SMTP details inside config folder mail.php file config/mail.php * (you can find config folder in root directory where you setup your october CMS)
<?php
return [
'driver' => 'smtp', // drive need to be smtp
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => ['address' => 'noreply#domain.tld', 'name' => 'OctoberCMS'],
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
];
set all details here as you like and start using SMTP
it will automatically use that driver details when you send mail and it will send mail using that SMTP Details
To use multiple mail setting you can use this snippet
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
// Backup your default mailer so we replace it after
// sending our custom mail
$backup = Mail::getSwiftMailer();
// Setup your gmail mailer
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_password');
// other config if needed...
$gmail = new Swift_Mailer($transport);
// Set the mailer as gmail
Mail::setSwiftMailer($gmail);
// Send your message
Mail::send(-- function arguments --);
// Restore your original mailer
Mail::setSwiftMailer($backup);
try this and let me know
$backup = Mail::getSwiftMailer();
$smtp_host_ip = gethostbyname('my_host');
$transport = SmtpTransport::newInstance($smtp_host_ip, 465, 'ssl')->setUsername('my_username')->setPassword('my_password');
$mail = new Swift_Mailer($transport);
Mail::setSwiftMailer($mail);
Mail::send($templateMessage, $params, function($message) use ($userSendMes){
$message->from('my_address_from', 'my_name');
$message->to($userSendMes["attributes"]["email"], $userSendMes["attributes"]["name"]);
});
all connection settings are correct

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.

Sending Email in Laravel Using Swift Mailer without Gmail

Hello i'm new to laravel and still getting familiar with certain features, i would like to know how i can send an email from my laravel application without using gmail smtp settings. Like the way the mail function works in basic PHP. Is this possible? I have tried googling it but i have been unable to find a solution, they all use gmail.
Go to your app/config/mail.php
change driver, host and from
'driver' => 'mail',
'host' => '',
'from' => array('address' => 'us#example.com', 'name' => 'Laravel'),
or you can set 'from' within
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});

Sending mail with SwiftMailer in Laravel 4

I am receiving a the following error:
Cannot send message without a recipient
This is while trying to send a email using swiftmailer. My code works in localhost and has all the parameters needed from sender to receiver but it throws an error saying it cannot send without recipient.
Here is my code:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message){
$message = Swift_Message::newInstance();
$email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $msg = $_POST['msg'];
$message = Swift_Message::newInstance()
->setFrom(array($email => $name))
->setTo(array('name#gmail.com' => 'Name'))
->setSubject($subject)
->setBody($msg);
$transport = Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl');
//$transport->setLocalDomain('[127.0.0.1]');
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
if($result){
var_dump('worked');
}else{
var_dump('Did not send mail');
}
}
}
You can do this without adding your the SMTP information in your Mail::send() implementation.
Assuming you have not already, head over to app/config/mail.php and edit the following to suit your needs:
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your_username',
'password' => 'your_password',
Then your code should be as simple as:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message)
{
$message->from( Input::get('email'), Input::get('name') );
$message->to('name#gmail.com', 'Name')->subject( Input::get('subject') );
});
}
So, hopefully the issue is one of configuration. Do you have other environments setup that might be over-riding the app/config/mail.php configuration settings in your server where it's not working?
Please make sure you have configured all the necessary configuration correctly on
/app/config/mail.php. Ensure all the configuration is correct for the environment where the email is not working correctly.
If you want to use your Gmail account as an SMTP server, set the following in app/config/mail.php:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email#gmail.com',
'password' => 'your-password',
When switching to online server, you want to protect this file or switch provider so as not to expose your gmail credentials. Port 587 is for mailgun not gmail.

Resources