"Reply-to" field in Laravel mail is not working - php

I need help to figure out how to set the reply-to field in app/config/mail.php. I'm using Laravel 4 and it's not working. This is my app/config/mail.php:
<?php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => [
'address' => 'sender#domain.com',
'name' => 'E-mail 1'
],
'reply-to' => [
'address' => 'replyto#domain.com',
'name' => 'E-mail 2'
],
'encryption' => 'tls',
'username' => 'sender#domain.com',
'password' => 'pwd',
'pretend' => false,
);

Pretty sure it doesn't work this way. You can set the "From" header in the config file, but everything else is passed during the send:
Mail::send('emails.welcome', $data, function($message)
{
$message->to('foo#example.com', 'John Smith')
->replyTo('reply#example.com', 'Reply Guy')
->subject('Welcome!');
});
FWIW, the $message passed to the callback is an instance of Illuminate\Mail\Message, so there are various methods you can call on it:
->from($address, $name = null)
->sender($address, $name = null)
->returnPath($address)
->to($address, $name = null)
->cc($address, $name = null)
->bcc($address, $name = null)
->replyTo($address, $name = null)
->subject($subject)
->priority($level)
->attach($file, array $options = array())
->attachData($data, $name, array $options = array())
->embed($file)
->embedData($data, $name, $contentType = null)
Plus, there is a magic __call method, so you can run any method that you would normally run on the underlying SwiftMailer class.

It's possible since Laravel 5.3 to add a global reply. In your config/mail.php file add the following:
'reply_to' => [
'address' => 'info#xxxxx.com',
'name' => 'Reply to name',
],

I'm using mailable and in my App\Mail\NewUserRegistered::class on the build function I'm doing this,
public function build()
{
return $this->replyTo('email', $name = 'name')
->markdown('emails.admin_suggestion');
}

Another option is to customizing SwiftMailer Message - https://laravel.com/docs/8.x/mail#customizing-the-swiftmailer-message.
->withSwiftMessage(function ($message) use ($senderEmail) {
$message->getHeaders()
->addTextHeader('Return-Path', $senderEmail);
})

Related

Send blade template mail with mailgun

I am trying to send mail with Mailgun. If I write this:
$mg = Mailgun::create('xxxx');
$mg->messages()->send('xxxx', [
'from' => 'dmt.akyol#gmail.com',
'to' => 'dmt.akyol#gmail.com',
'subject' => 'Your Link To Login!',
'text' => 'hello',
]);
it works but I want to send a view (blade) and I don't know how to do.
My code is:
public function build(array $customer)
{
return view('link')->with([
'customer'=> $customer,
]);
}
public function sendContactForm(array $customer)
{
$aaa=$this->build($customer);
$mg = Mailgun::create('xxxxxx')
$mg->messages()->send('xxxx'), [
'from' => $customer['customerEmail'],
'to' => ' dmt.akyol#gmail.com',
'subject' => 'Contact Message',
'html' => $aaa,
]);
}
This does not work when I write html or text.
What should I do?
Add ->render() to your build call to store the contents of the view as a string:
public function build(array $customer)
{
return view('link')->with([
'customer'=> $customer,
])->render();
}

Laravel Mail::send returns zero with no specific error in Mail::failures()

I am using smtp driver and this is my code to send email in laravel 5.2:
public function Sendmail()
{
$data["mail_message"] = "Hello!";
if(Mail::send('Emails.email', $data, function($message)
{
$message->from('webmaster#example.com', Input::get('name'));
$message->to('amirhasan.hesam#gmail.com')->subject('Welcome to My Laravel app!');
}))
{
return "success";
}
else
{
return Mail::failures();
}
}
the Mail::failures() returns ["amirhasan.hesam#gmail.com"] with no specific error!
and this is my config on mail.php :
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', '*******'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => "****#*****", 'name' => "Diling"],
'encryption' => env('MAIL_ENCRYPTION', ''),
'username' => env('*****#*****'),
'password' => env('*************************'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
and I am using xamp right now to test the email. Any thoughts?
I've had troubles with using variables inside the mail::send .. and im also not sure if mail::send returns boolean or such... I've used something like what I wrote down in the past.
$nameSend = Input::get('name');
Mail::send('Emails.email', $data, function($message) use ($nameSend){
$message->from('webmaster#example.com', $nameSend);
$message->to('amirhasan.hesam#gmail.com')->subject('Welcome to My Laravel app!');
});
.
.
if( count(Mail::failures()) > 0 ) {
$output = "There was one or more failures. They were: \n";
foreach(Mail::failures as $email_address) {
$output = $output. $email_address ."\n";
}
return $output;
}
return "Success!";
you just need to use Mail facade
use Illuminate\Support\Facades\Mail;

Mandrill queue in php

Have you any idea how to control the queue of Mandrill send emails in PHP ?
Well, I'm using Symfony as a Framework, & this is my send function in the Mandrill class :
public function sendMandrill()
{
$listTo = array();
foreach ($this->contacts as $contact)
{
$listTo[] = [
'name' => $contact->getFname(),
'email' => $contact->getEmail()
];
}
var_dump($listTo);
$email = array(
'html' => $this->message->getBodyHtml(),
'text' => $this->message->getBodyText(),
'subject' => $this->message->getSubject(),
'from_email' => $this->apiDom,
'from_name' => $this->message->getFromName(),
'to' => $listTo,
"preserve_recipients"=> false,
);
$this->senderAPI = new \Mandrill("$this->apiKey");
return $this->senderAPI->messages->send($email);
}
Now, I want to create a function so I can pause the sending for a while so I can change such things, then resume it whenever I want or may be to stop it at all !
which mean there will be 3 functions as following :
public function pauseMandrill()
{
...
}
public function resumeMandrill()
{
...
}
public function stopMandrill()
{
...
}

Mail::send() not working in Laravel 5

I am continuously getting this error 'Class 'App\Http\Controllers\Mail' not found' error in my UserController.php
public function store(CreateUserRequest $request)
{
$result = DB::table('clients')->select('client_code','name','email')
->where('client_code','=',$request->code)
->where('email','=',$request->email)
->first();
if($result){
$tmp_pass = str_random(10);
$user = User::create([
'username' => $result->name,
'email' => $request->email,
'password' => $tmp_pass,
'tmp_pass' => '',
'active' => 0,
'client_code' => $request->code
]);
if($user){
Mail::send('emails.verify',array('username' => $result->name, 'tmp_pass' => $tmp_pass), function($message) use ($user){
$message->to($user->email, $user->username)
->subject('Verify your Account');
});
return Redirect::to('/')
->with('message', 'Thanks for signing up! Please check your email.');
}
else{
return Redirect::to('/')
->with('message', 'Something went wrong');
}
}
else{
Session::flash('message', "Invalid code or email.");
return redirect('/');
}
}
Mail function used to work in Laravel 4 but I am getting errors in Laravel 5. Any help would be appreciated.
Mail is an alias inside the global namespace. When you want to reference it from inside a namespace (like App\Http\Controllers in your case) you have to either:
Prepend a backslash:
\Mail::send(...)
Or add a use statement before your class declaration:
namespace App\Http\Controllers;
use Mail; // <<<<
class MyController extends Controller {
The same goes for the other facades you use. Like Session and Redirect.
Another way is to use Mail facade
use Illuminate\Support\Facades\Mail;
In your controller
In Laravel 5.8 I solved it by also adding this in the controller :
THIS:
use App\Mail\<<THE_NAME_OF_YOUR_MAIL_CLASS>>;
use Illuminate\Support\Facades\Mail;
INSTEAD OF:
use Mail;
setup your app/config/mail.php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'from' => array('address' => 'your#gmail.com', 'name' => 'Welcome'),
'encryption' => 'ssl',
'username' => 'your#gmail.com',
'password' => 'passowrd',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
than setup in controller:
use Mail;
\Mail::send('tickets.emails.tickets',array('ticketsCurrentNewId'=>
$ticketsCurrentNewId->id,'ticketsCurrentSubjectId'=>$ticketsCurrentNewId->subject,'ticketsCurrentLocationsObj'=>$ticketsCurrentLocationsObjname), function($message)
{
//$message->from('your#gmail.com');
$message->to('your#gmail.com', 'Amaresh')->subject(`Welcome!`);
});
after this setup mail are send emails if any permission error have showing then click this url and checked this radio button
https://www.google.com/settings/security/lesssecureapps
after configuration it is working fine in #laravel,#symfony and any php framework
thank you

Passing data to queued Mail object in Laravel 4, using Iron.io

I am trying to set up a queued email in Laravel 4 using the Iron.io driver. I would like to pass some details to the email's Subject and From attributes but they seem to not be making it into the queue request and their presence causes the email to not be sent (not sure where to look for a log with errors). However, simply using Mail::Send() works fine.
Here is the code in question:
public function handleFeedbackForm()
{
$data = array(
'name_f' => Input::get('name_f'),
'name_l' => Input::get('name_l'),
'email' => Input::get('email'),
'commentType' => Input::get('commentType'),
'testimonialPublish_answer' => Input::get('testimonialPublish_answer'),
'comment' => Input::get('message')
);
$rules = array(
'name_f' => 'required',
'name_l' => 'required',
'email' => 'required|email',
'message' => 'required'
);
$v = Validator::make(Input::all(), $rules);
if ($v->passes())
{
$emailInfo = array('name_f' => Input::get('name_f'),
'name_l' => Input::get('name_l'),
'email' => Input::get('email'));
Mail::queue('emails.feedback', $data, function($message) use($emailInfo)
{
$recipients = array();
$form = MailType::find(1);
foreach ($form->users as $user)
{
$recipients[] = $user->email;
}
if (count($recipients) == 0)
{
// Nobody for this field, send to webmaster
$recipients[] = 'someone#somewhere.com';
}
$message->to($recipients)
->from($emailInfo['email'])
->subject('Foobar Feedback Form Message - ' . $emailInfo['name_f'] . ' ' . $emailInfo['name_l']);
});
return Redirect::to('contact')->with('feedbackSuccess', true);
}
else
{
return Redirect::to('contact')->with('feedbackError', true);
}
}
Any ideas? Thanks!

Categories