Cannot send email in Laravel 5.2 - php

I am trying to send an email with Laravel 5.2. This is my first time of sending email in Laravel. But it is throwing this error
Swift_TransportException in AbstractSmtpTransport.php line 162: Cannot send message without a sender address
This is my code of sending email:
Route::get('test',function(){
$message = "hello";
Mail::send('welcome', ['key' => 'value'], function($message)
{
$message->to('iljimae.ic#gmail.com', 'John Smith')->subject('Welcome!');
});
});
This is my email settings in env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=iljimae.ic#gmail.com
MAIL_PASSWORD=xxxxxx
MAIL_ENCRYPTION=null
My welcome view only has a message "Hello world"
I already configured less secure app settings for my email in Gmail settings. So, what is wrong with my code, and why is that throwing that error?

The error message Cannot send message without a sender address is clear. You just need to add from to the message:
Route::get('test',function(){
$message = "hello";
Mail::send('welcome', ['key' => 'value'], function($message)
{
$message->from('myEmail#test.com')
->to('iljimae.ic#gmail.com', 'John Smith')
->subject('Welcome!');
});
});
In order to be able to send the mail you must to change the mail encryption in the .env file to:
MAIL_ENCRYPTION=tls

Your $message chain has 'To' and 'Subject' fields but is missing 'From' field.
Just add ->from() to the chain:
$message->to('iljimae.ic#gmail.com', 'John Smith')
->subject('Welcome!')
->from(Config::get('mail.from.address'), Config::get('mail.from.name'));
Assuming that 'from' is set in your config/mail.php file (where it may refer or may not refer to environment variables). If it's not, you could just specify it directly:
$message->to('iljimae.ic#gmail.com', 'John Smith')
->subject('Welcome!')
->from('iljimae.ic#gmail.com', 'John Smith');

If your mail was not being sent, make sure to delete this parameter "MAIL_FROM_ADDRESS=null" from your .env file. This worked for me.

Related

No Data in Sent Items when sending email using Laravel and IBM Notes

Good day.
I am currently using Laravel 5.3 Mailer and an IBM notes email account.
I was able to successfully sent emails but when I looked at the sent items of the email that I used for sending, there was none.
I am currently using this configuration in .env
MAIL_DRIVER=smtp
MAIL_HOST=ourhostip
MAIL_PORT=25
MAIL_USERNAME=myaccount
MAIL_PASSWORD=mypass
MAIL_ENCRYPTION=
In my controller I am using this code.
$data = "Testing Data";
$email = "email";
$name = "Name";
Mail::send('emails.reminder', ['data' => $data], function ($mail) use ($name,$email) {
$mail->from('fromemail', 'AI');
$mail->to($email, $name)->subject('Testing Email!');
});
Any help would be appreciated. Thanks.
This is just the way SMTP works. When you use Outlook,Thunderbird, etc. to write an email via SMTP and your Notes/Domino Account you can see the E-Mail in your Notes Sent Folder only if you configure an IMAP Connection too.
I'm afraid this is not posible in Laravel.
If you want to have the E-Mail in your IBM Notes Inbox you can send the mail with BCC to your own address (and move it with a mail rule to a folder).

Swift_TransportException Error: Expected response code 250 but got code "", with message "" - Laravel

I have a problem sending a lot of emails from my Laravel app.
Before explain the issues, this is the server config:
Server: Google Compute Engine, VM Instance (Ubuntu Server) - Standar 1
This is my configuration:
MAIL_DRIVER=smtp
MAIL_HOST=smtp-relay.gmail.com
MAIL_PORT=587
MAIL_USERNAME=my_email#mydomain.com
MAIL_PASSWORD=********
MAIL_ENCRYPTION=tls
So, I use Google App work Account for this purpose and all works very well when i send emails for one recipient!
The problem is when i try to send email to all my customers:
foreach ($users as $key => $user) {
// content construction
if ($have_one){
Mail::queue('emails.contact', $body, function($message) use($email)
{
$message->from('info#mydomain.com', 'My Name Team');
$message->to($email)->subject('Fake Subject!');
});
}
}
It work well the very firsts 100-200 emails sent, but in some point it just crash it!!!
I use a php artisan command to send this emails, and this is the output:
output sending email
Does anyone know how to fix it?
Thanks in advance!
Some person have same as issue you are getting. Try to do the following may be below procedure will solve your issue.
A.) login from gmail and visited the link https://www.google.com/settings/security/lesssecureapps and turned on less secure apps.
B.) Edit .env file as like below:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username //i.e. username#gmail.com
MAIL_PASSWORD=password //Gmail accounts password
MAIL_ENCRYPTION=ssl
C.) In you Controller, Write down as following:
$rawData = request::all();
Mail::queue('program.meeting.emailInvite', $rawData,
function($message) use ($rawData)
{
$message->from('info#echosofts.com',
'Echosofts')->to(array_map('trim', explode(',',
$rawData['all_email_id'])))->subject($rawData['mail_title']);
});
Then email was working fine except the sender email ID was my google account (username#gmail.com) instead of info#cgindians.com.
D.) To overcome the sender email changing problem, I visited my google account and do the following:
"Setting icon"-> Settings -> Accounts and Import->Send mail as->Add another email address your own.
I think sometimes this stuff just happens and are usually network related issues. The best you can do is drop your code into a try/catch block, log when it happens, and attempt the email again.
Sometimes it helps to drop a sleep($n) and let it sleep for a second before continuing.
Also keep an eye on the logs to see if you can spot any patterns in the future that might give you some idea of what's going wrong. If it is a network issue, there isn't a lot besides this that can be done.
Please try first
Go to
vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mailer.php
And add $this->_transport->stop();
just before return $sent;.
The final code looks like this.
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
if (!$this->_transport->isStarted()) {
$this->_transport->start();
}
$sent = 0;
try {
$sent = $this->_transport->send($message, $failedRecipients);
} catch (Swift_RfcComplianceException $e) {
foreach ($message->getTo() as $address => $name) {
$failedRecipients[] = $address;
}
}
$this->_transport->stop();
return $sent;
}
Also check this link https://github.com/mustafaileri/swiftmailer/commit/d289295235488cdc79473260e04e3dabd2dac3ef

"From Email" doesn't work in laravel 5.3

This is my code:
Mail::send('view',$dataView, function($message) use ($user)
{
$message->from('my_email#gmail.com', 'Myname');
$message->subject('This is title');
$message->to(sender_email#gmail.com, $user->user_username);
});
It works! But When I check sender_email#gmail.com then I see "from email" which I config in .env ( MAIL_USERNAME ), it isn't "from email" in code (my_email#gmail.com), How to I can change it to my_email#gmail.com? Thanks and sorry about my english.
In config/mail.php, around line 58, try changing:
'from' => [
'address' => 'hello#example.com',
'name' => 'Example',
],
to:
'from' => [
'address' => env('MAIL_USERNAME'),
'name' => env('MAIL_USERNAME')
],
You are trying to send emails via dynamic senders which is not allowed by Gmail for preventing spamming mails, so automatically Gmail will change your sender address to your default address.
I guess you're misunderstanding the MAIL_USERNAME which is in your .env file.
So basically, Laravel provides simple solution to send emails via dynamic senders. Lets say you've signed up for MailGun or SendGrid to send mails and MAIL_USERNAME is the username for your mail provider not your sender mail address. (Not Gmail, as Gmail doesn't support dynamic senders. Its good if you're testing your mails.).
so, you're .env will look like this.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgridUsername
MAIL_PASSWORD=sendgridPassword
MAIL_ENCRYPTION=tls
Now, you're eligible to send mails using sendgrid. Now Lets say your domain is example.com so you can use admin#example.com support#example or any email address with your domain to send emails.
To Receive messages either you use webmail or Gmail.
Hope this helps.

How to change header information of mail in Laravel?

I am creating a simple contact us form using Laravel 5.1
public function sendMessage(){
$data = [
'name' => Input::get('name'),
'email' => Input::get('email'),
'subject' => Input::get('subject'),
'body' => Input::get('body')
];
Mail::send('emails.contact',$data, function($message) use($data){
$message->from($data['email'], $data['name']);
$message->to('smartrahat#gmail.com','Mohammed');
$message->subject($data['subject']);
});
Session::flash('success_message','Mail sent successfully!');
return redirect('contact');
}
Everything is working fine but the sender email address is not the one it get from the contact page. The email is sent from the address which I configure in .env
I want to have the email from the sender email address which he filled up contact form. Or, you can say I want to change the header information (only from, I can change other information).
Well Laravel will send the mail through the given smtp server. I guess the smtp server (e.g. google doing this) will not let you change your from address to another address then the account belongs to.
If you want to reply to this address directly in your email programm you can add $message->replyTo($data['email'], $data['name']);.
The $message variable inside the Mail::send() function is a SwiftMailer message instance. Therefore, you can work with headers just like the SwiftMailer documentation shows. Just use $message->getSwiftMessage() to get and manipulate the headers.
Also, you absolutely can change the From header, but if you change it to a different domain, you'll have to deal with phishing warnings in the client. You can resolve that by setting up DKIM and SPF records, and you will need to have access to the DNS settings for the domain to do that.

Send email from address with laravel

I have configured my mail.php config file to send emails using the gmail account.
Everything looks great, but when I try to send email from another account (not the address used in the mail.php config file) I get an email from the address set on the configuration file.
Mail::send('emails.contact_mail', array(
'email' => $fromEmail,
'name' => $user,
'subject' => $subject,
'message' => $message_content,
), function($message)use ($fromEmail,$user,$subject) {
$message->to('s111#gmail.com');
$message->from('test#gmail.com', $user);
$message->subject($subject);
});
It seems like the $message->from doesn't work. I get on the s111#gmail.com an email from the address set in mail.php file and not from test#gmail.com.
Is there any solution? How to change the from address?
You will still use the SMTP servers set in your config file. Setting another 'from' header is theoretically possible, but the GMail SMTP will ignore it unless you set an address that has been added to your Google account.
Solution: either don't use another 'from' address, or add that address to your Google account.
There are a couple things you can try:
$message->replyTo('test#gmail.com', $user);
And to my understanding, the "From: " header in emails is what should determine what it will display as the sender. This may work for changing the "From" header:
$message->getHeaders()->addTextHeader('From', 'Your Name <test#gmail.com>');
I think this is best solution for this question.
We can figure out this as follows:
$message->from('sender#email.com', 'sender_name');
And also we should remove the following code block in config/mail.php.
'from' => [
'address' => 'sender#email.com',
'name' => 'sender_name',
],
of course, this will be working on Laravel Framework.

Categories