Here's the code I'm using to send an e-mail to multiple recipients:
$recipients = ['myoneemail#esomething.com', 'myother#esomething.com','myother2#esomething.com'];
Mail::send(['admin.emails.email.html', 'admin.emails.email.text'], ['content' => $request->input('message')],
function ($message) use ($request, $recipients) {
$message
->to($recipients)
->subject($request->input('subject'))
->replyTo('xxx#xxx.com', 'Me');
});
I'm using Mailtrap.io as a fake inbox to catch the e-mails. I only get the mail sent to "myoneemail#esomething.com", not the other ones.
But why ?
Related
i want to use config that i get from html form and i dont want to get it from config/mail.php or .env file
how can i do that in laravel 8
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from($address = 'contact#example.com', $name = $this->data['from'])
->subject($this->data['subject'])
->view('mail.view')
->smtp('mail.example.com')
->port('587')
->username('username')
->password('pass')
->smtp('mail.example.com')
->with(['message' => $this->data['message']]);
}
you can use swiftmailer to send any type of mail you want (laravel use this under the hood):
$transport = (new Swift_SmtpTransport("smtp.gmail.com", "587", "tls"))
->setUsername('username')
->setPassword('pass');
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message("subject")) // email subject
->setFrom(['contact#example.com' => "sender name"]) // sender mail and display name
->setTo("receiver#gmail.com") // replace with your receiver mail variable
->setBody("<h2>Test</h2>", 'text/html');
$result = $mailer->send($message); // send the mail
if you want to send from queue instead of in controller directly, you can create queue and pass mail parameter to queue construct
I am trying to determine how to send an email to a group of recipients (as an array) through Laravel's Mail facade, but I would like the emails sent as individual emails as opposed to one bulk email in which all recipients' emails will be viewable.
I have an array of email addresses, $attendees, to whom the email is sent once validation passes. One way I could resolve this is to send to $attendees as 'bcc' vs. 'to,' but ideally I would like the email to be sent 'to' each recipient.
First and foremost, this following block of code DOES work, and is successful at emailing the array of $attendees -- but again, they all receive the email together and are able to view the collection of email addresses/emails of other recipients in the 'to' field when they receive the email, which I do not want to be the case.
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::to(URL::previous())
->withErrors($validator)
->with('subject', $mailDetails['subject'])
->with('body', $body)
->with('eventId', $eventId);
} else {
Mail::send('emails.event_attendees', $mailBody, function($m) use ($mailDetails) {
$m->from('noreply#helpinghabit.com', $mailDetails['orgName']);
$m->to($mailDetails['attendees']);
$m->bcc($mailDetails['sender']);
$m->subject($mailDetails['subject']);
});
return redirect()->back()->with('message', "Your email has been sent!");
};
I tried to accomplish this using a foreach loop like so:
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::to(URL::previous())
->withErrors($validator)
->with('subject', $mailDetails['subject'])
->with('body', $body)
->with('eventId', $eventId);
} else {
foreach ($attendees as $k) {
Mail::send('emails.event_attendees', $mailBody, function($m) use ($mailDetails) {
$m->from('noreply#helpinghabit.com', $mailDetails['orgName']);
$m->to($attendees[$k]);
$m->bcc($mailDetails['sender']);
$m->subject($mailDetails['subject']);
});
};
return redirect()->back()->with('message', "Your email has been sent!");
};
... but this returns an empty response error and tells me that no data was sent.
Any ideas? Thanks in advance for your help!
When the user of my website used the contact form, i want send a mail with the confirmation to the user and send to me other message with the created ticket.
I´m using Laravel 5.2 + Mailgun
But only sent the last mail to my email hello#frikinow.com, the user confirmation email is not sent.
Laravel not returns any error. This is my code:
// for the client mail
Mail::send('emails.success_email', ['name' => Input::get('nombre'), 'content' => $content], function ($message)
{
$message->subject('FrikiNow ha recibido su consulta');
$message->from('hello#frikinow.com', 'FrikiNow Support');
$message->to(Input::get('email'));
});
// for me
Mail::send('emails.ticket_email', ['email'=> Input::get('email'),'name' => Input::get('nombre'), 'content' => Input::get('mensaje'),'mobile_phone' =>Input::get('telefono')], function ($message)
{
$message->subject('Ticket de Cliente: '.Input::get('asunto'));
$message->from('hello#frikinow.com', Input::get('nombre'));
$message->to('hello#frikinow.com');
});
How I can send two different emails at once?
You may add CC header
Im not full aware of MailGun API, but you can try this:
$message->cc('email#example.com'); //Copy of the message goes to this email
I want to send email to multiple users. Here is my email sending code.
$email_id = User::select('email_id')->get();
Mail::send('test' , array('user' => $email_id) , function ($message) {
$message -> to('xyz#gmail.com') -> subject ('Welcome!!!');
});
I am getting array of email_id while printing $email_id and I pass $email_id to array. but it's not working.
Any help would be grateful.
Thank You.
Please check mention url you can use this
Laravel Mail::send() sending to multiple to or bcc addresses
https://codedump.io/share/7tL5qbeUwnKT/1/laravel-mailsend-sending-to-multiple-to-or-bcc-addresses
Try this:
$email_id = User::select('email_id')->get()->toArray();
Mail::send('test' , array('user' => $email_id) , function ($message) {
$message -> to('xyz#gmail.com') -> subject ('Welcome!!!');
});
I have a function in my controller which saves messages from users, first it saves message into database than it sends email to that user's mail address and than it returns json response to the sender.
Problem: sometimes it takes too long for email to be sent, and the sender has to wait long time for the response, or sometimes email is not even sent (due to some smpt problems etc.) and it triggers an error, however I don't really care that much if email is sent or not, most important that message is saved to the database.
What I'm trying to achieve:
I want to save message to the database, ->
immediately after that send response to the sender, ->
and only after run Mail::send();
so run Mail::send() after controller returns json to the sender, so the sender will receive a positive response regardless of how Mail::send() performs
$message = new MessageDB;
$message->listing_id = e(Input::get('listing_id'));
$message->user_id = $listing->User->id;
$message->name = e(Input::get('name'));
$message->mobile = e(Input::get('mobile'));
$message->message = e(Input::get('message'));
if ($message->save()) {
Mail::send('emails.message', ['user' => 'Jon Doe','message' => $message], function($m) use($listing){
$listing_agent = $listing->Agent;
if ($listing->agent == null) {
$mail_to = $listing->User->email;
$name = '';
}else{
$mail_to = $listing->Agent->email;
$name = $listing->Agent->first_name.' '.$listing->Agent->second_name;
}
$m->to($mail_to)->subject('new message from company.com');
});
return ['success' => 1, 'message' => 'messasge has been sent'];
You can use Mail Queueing functions of Laravel. Here's the Link
Mail::queue('emails.welcome', $data, function($message) {});Queue mail for background Sending.
Mail::later(5, 'emails.welcome', $data, function($message){});Define Seconds after which mail will be sent.