I am wondering: How to use regular sendmail with Laravel 5. When I google this I get very little information. Who can help me out here?
My mailer script now uses gMail smtp and I would rather use sendmail:
$send = Mail::send('emails.main', $data, function($message) use ($data) {
$message->to($data['to']);
$message->to($data['cc']);
$message->subject($data['subject']);
});
Related
Basically i'm trying to send by email a link that lets you login with a specific account and then redirects you to a page.
I can seccessfully generate link and send them via email using URL functionalities in laravel using this code:
Generating the link:
$url = "some/page/".$travel_id;
$link = URL::temporarySignedRoute(
'autologin', now()->addDay(), [
'user_id' => 3,
'url_redirect' => $url,
]
);
And sending the mail:
Mail::send('emails.travel', $data, function ($message) use ($data) {
$message->from('mail#mail.com', 'blablabla');
$message->to('reciever#mail.com', 'blablabla')->subject('test');
});
There is a route that catches the link sent by mail that is supposed to log you in with the user (in this case, the one with the id '3') and redirect you to some page but when it redirects, it prompts you to the login page, as if you are not logged.
Here is the route:
Route::get('/autologin', function (Request $request) {
$user = User::findOrFail($request->user_id);
if (! $request->hasValidSignature()) {
abort(403);
}
Auth::login($user);
return redirect($request->input('url_redirect'));
})->name('autologin');
When i try to do a Auth::check() after the Auth::login($user); it returns true, so is the user logged in?
I also tried to use Auth::loginUsingId($request->user_id); with no different results.
Any idea of what's happening?
So i found the problem,
I was logging in with a backpack user but i was using the default auth feature of laravel.
Turns out i need to use: backpack_auth()->login($user); instead of Auth::login($user); if i want to login using a backpack user.
Also use backpack_auth()->check() instead of Auth::check().
The following IS working when I use Mail::send
$email = 'my#email.com';
$name = 'My Name';
$invoice = InvoicePdf::generate($invoice_id); // generates PDF as raw data
Mail::send('mail.template', null, function($message) use ($name, $email, $invoice) {
$message->to($email, $name);
$message->subject('Thank you for your order!');
$message->attachData($invoicePdf, 'invoice.pdf', ['mime' => 'application/pdf']);
});
It works fine and an email is generated with the correct PDF attachment.
However, if I change Mail::send to Mail::queue then I receive the following error:
Unable to JSON encode payload. Error code: 5
/var/www/html/october/vendor/laravel/framework/src/Illuminate/Queue/Queue.php
line 90
If I take the $message->attachData(); line out then it works even with Mail::queue so it seems like the raw data from the attachment is causing issues with the queue but there's nothing in the relevant October or Laravel docs about how to deal with this.
May be its because $invoicePdf data is raw data of PDF file and php can not process that data (attachData) when saving to database.
hmm, alternative you can generate file and then just attach file path to mail and then add to queue.
// generate tempfile name
$temp_file = tempnam(sys_get_temp_dir(), 'inv');
// this pdf is generated by renatio plugin but you can
// use your data and save it to disk
PDF::loadTemplate('renatio::invoice')
->save($temp_file);
Mail::queue('mail.template', null, function($message) use ($name, $email, $temp_file) {
$message->to($email, $name);
$message->subject('Thank you for your order!');
$message->attach($temp_file, ['as' => 'Your_Invoice.pdf', 'mime' => 'application/pdf']);
});
it should work.
#Joseph pointed that in Laravel 5.5 there is mailable which can be used. #Joseph pointed this solution and it seems working so you can also use this solution if your laravel version is >= 5.5
https://laracasts.com/discuss/channels/laravel/sending-email-with-a-pdf-attachment
Thanks #Joseph
I took an example from the Laravel 5.1 documentation for Mail and replaced it with my send and receiver email ids.
Mail::raw works in the controller and if I use Mail::send in tinker it works. However, if I use Mail::send in the controller it doesn't work.
Everything is set up as described on the Laravel 5.1 mail page. https://laravel.com/docs/5.1/mail#sending-mail . I have also cleared app cache, config cache and view cache.
public function sendEmailReminder(Request $request, $id)
{
$user = User::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
}
The error could be from the email you are trying to send from, in order to send the email successfully this email hello#app.com has to be a valid email address and the one register as your MAIL_USERNAME
It was a permissions issue for the storage/frameworks directory. Once I changed the permissions it worked fine..
I have a laravel 5 app and need to send a reset password link through a service sendinblue. How can I change the core functionality to use sendinblue in PasswordBroker.php?
public function emailResetLink(
CanResetPasswordContract $user,
$token,
Closure $callback = null
) {
$mailin = new Mailin(
'https://api.sendinblue.com/v2.0',
'0TYSSJBSKERNDKW'
);
$view = $this->emailView;
return $this->mailer->send(
$view,
compact('token', 'user'),
function($m) use ($user, $token, $callback)
{
$m->to($user->getEmailForPasswordReset());
if ( ! is_null($callback))
{
call_user_func($callback, $m, $user, $token);
}
});
}
Did you try to add Sendinblue as a mail driver? This github repo can help (https://github.com/agence-webup/laravel-sendinblue)
Here all your emails will be sent by Sendinblue and you will send as a regular mail in Laravel (https://laravel.com/docs/5.1/mail)
If is just for this, you can change the driver just for this kind of mail, i think that you can change the driver on runtime like this
Config::set('mail.driver', 'driver_name');
(new Illuminate\Mail\MailServiceProvider(app()))->register();
Also, you can try to listen to 'mailer.sending' event that is fired just before sending mail messages, but this is not a good approach.
when i am trying to send Mail through Contact Us Form receiving this Error
"Address in mailbox given [] does not comply with RFC 2822, 3.6.2."
I try search to find solution but I cannot find one. I edited config/mail.php
public function sendContactInfo(ContactMeRequest $request)
{
$data = $request->only('name', 'email');
$emailto="******#gmail.com";
$data['messageLines'] = explode("\n", $request->get('message'));
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});
return back()
->withSuccess("Thank you for your message. It has been sent.");
}
with configuration file
i am following this tutorial
Laravel Send Mail
use $data['email']
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto,$data['email']) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});