How to get the actually/current email address in the 'emails.subscribed' view?
Here is my code:
Mail::send('emails.subscribed', $data, function($message) use ($subject,$recievers,$meta) {
foreach ($recievers as $email) {
$message->bcc($email,$email);
$message->email = $email;
}
});
And the view I need to get the current email address (not the whole email array just only the current):
{{$somehow->email}}
Send emails one by one, not all in bcc, then you can easily change email view:
$recipients = [ ["name" => "John", "email" => "john#john.com"], ["name" => "Doe", "email" => "doe#doe.com"] ];
$subject = 'subject';
$meta = 'meta';
foreach($recipients as $recipient) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['recipient'] = $recipient;
Mail::send('emails.subscribed', $dataToPassToEmailView, function($message) use ($subject, $recipient, $meta) {
$message->to($recipient['email'], $recipient['name']);
$message->subject($subject);
});
}
view emails.subscribed.blade.php:
Email: {{ $recipient['email'] }}
Name: {{ $recipient['name'] }}
Related
I am trying to iterate and send message to mail in array of emails using Laravel Mail::send function
I searched for same problem and found the code below reference from Radmation here https://stackoverflow.com/a/39625789.
$emails = ['tester#blahdomain.com', 'anotheremail#blahdomian.com'];
Mail::send('emails.lead', ['name' => $name, 'email' => $email,
'phone' => $phone], function ($message) use ($request, $emails)
{
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
//$message->to( $request->input('email') );
$message->to( $emails);
//Add a subject
$message->subject("New Email From Your site");
});
I am wondering the second paramater for iteration usage, so i can message each email with dynamic message of their name.
$emails = ['tester#blahdomain.com', 'anotheremail#blahdomian.com'];
foreach($emails as $currentRecipient){
$customtMsg = //create here a custom msg
Mail::send(['text' => 'view'], $customtMsg, function ($message) use ($request, $currentRecipient)
{
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
$message->to($currentRecipient);
//Add a subject
$message->subject("New Email From Your site");
});
}
Please check usage here
You could put emails in associative array, for example:
$emails = [
'tester#blahdomain.com' => 'tester',
'anotheremail#blahdomian.com' => 'anotheremail'
];
And then iterate over key=>value pairs, like:
foreach($emails as $email=>$name){
Mail::send('emails.lead', ['name' => $name, 'email' => $email], function ($message) use ($email, $name){
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
$message->to($email, $name);
$message->subject("New Email From Your site");
});
}
If you want to send same mail to multiple recipients at once, you could also pass an array of email=>name pairs to the to method:
$message->to($emails)
But I don't think it is possible to customize an email content individualy with that approach. Also in that case, all of the email addresses are visible to every recipient.
I m new to laravel.i have table email_template and want to send mail to user when user forgot password.i m fetching content dynamically from database but i dont know how to pass it to mail function in laravel.
Mail::send($posts['email_template'], ['USER' =>$post['user] ], function($message)
{
$message->from('test#gmail.com')->subject('Welcome to laravel');
$message->to('test8#gmail.com');
});
where $posts['email_template'] is a content which i want to send and user is a variable which i want to replace in content
Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
$message->from('test#gmail.com', 'Your Application');
$message->to('test8#gmail.com', $user->name)->subject('Welcome to laravel');
});
emails.template is your view - template.blade.php file - /resources/views/emails/template.blade.php
Now, in your view i.e emails.template, you can do:
{{ $user->name }}, {{ $data->address }}
You can Define
ADMIN_EMAIL and CC_EMAIL in the constant file in the config folder
$emailData = array(
'name'=>'toName',
'toEmail'=>$request->email
);
$this->sendEmail($emailData);
Email Function
function sendEmail($emailData){
$this->adminEmail = config('constant.ADMIN_EMAIL');
$this->ccEmail = config('constant.CC_EMAIL');
$this->toEmail = $emailData['toEmail'];
$this->emailTemplate = $emailData['emailTemplate'];
$data['emailInfo'] = array(
'name'=>$emailData['name']
);
Mail::send('emails.yourTemplate', $data, function ($message) {
//$message->attach($pathToFile);
$message->from($this->adminEmail, 'Laravel Email Test');
$message->to($this->toEmail)->cc($this->ccEmail);
});
}
$emails is an array but only shows me one email.
Also how to pass the email array into info_on_hand view?
$emails=$request['emails'][$i];
Mail::send('emails.info_on_hand', $data, function ($message) use($emails) {
$message->from('us#example.com', 'Name Company');
foreach($emails as $email):
$message->to($emails)->subject('Status: '.Input::get('desc').' With VIN# '.Input::get('vin').' is '.Input::get('status').'At Name');
endforeach;
});
So to send and pass the array of emails to the view and send it to them is like this
Guessing when you return just $emails it looks like this:
['test#example.com', 'test2#example.com','test3#example.com']
If so use this
$emails = $request['emails'];
Mail::send('emails.info_on_hand', ['emails' => $emails], function ($m) use ($emails) {
$m->from('youremail#eample.com', 'Name');
$m->to($emails);
$m->subject('Your Subject');
});
Another thing is that with the New GDPR laws don't think this is allowed?
i want to send text not view by laravel mail , as i use simple form wth textarea to write the content of message , how to seend text or how to convert this text to view ....... thanks
public function sendmail(){
$sendmessage=Input::get('message');
//$messageView=View::make('messageview')->with('message',$sendmessage);
Mail::send($sendmessage, array('name'=>'hossam'),function($message){
$title=Input::get('title');
$mail=Input::get('mailsender');
$message->from($mail,'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($title);
You can create an email view where the only thing it sends is the contents of a variable:
app/views/emails/nonview.blade.php
{{ $contents }}
Then, you can use this view whenever you want to send an email that doesn't have a view:
public function sendmail() {
$from = Input::get('mailsender');
$subject = Input::get('title');
// 'contents' key in array matches variable name used in view
$data = array(
'contents' => Input::get('message')
);
Mail::send('emails.nonview', $data, function($message) use ($from, $subject) {
$message->from($from, 'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($subject);
});
}
I'm using Laravel 4 , tying to send mail to multiple user using Queueing Mail , my code looks like -
$mailuserlist=DB::table('table')
->join('some_table')
->select('some_thing')
->where('somecondition'))->get();
Mail::queue('mail_template', $data, function($message) use ($mailuserlist)
{
$message->from('test#desto.co.in', 'Mail Notification');
foreach ($mailuserlist as $value) {
$message->to($value['email'],$value['firstname'].' '.$value['lastname']);
}
$message->subject('Testing mail');
});
..it's not at all working . How can i send ail to multiple address ??
It should be possible in two ways as we can see in the source code framework/src/Illuminate/Mail/Message.php:
Chaining
Using array
Chaining:
->to($address1, $name1)->to($address2, $name2)->to($address3, $name3)...
Using array of addresses:
->to(array($address1,$address2,$address3,...), array($name1,$name2,$name3,...))
Queueing Mail doesn't seem to support sending 1 mail to multiple user. I think you should queue 1 mail for each of your recipients:
$mailuserlist=DB::table('table')
->join('some_table')
->select('some_thing')
->where('somecondition'))->get();
foreach ($mailuserlist as $mailuser) {
Mail::queue('mail_template', $data, function($message) use ($mailuser) {
$message
->from('test#desto.co.in', 'Mail Notification')
->to($mailuser['email'],
$mailuser['firstname'].' '.$mailuser['lastname'])
->subject('Testing mail');
});
}
You can get all emails to send
$users = User::select('email')->get()->toArray();
And delete the key of array for get an array only with the emails
$emails = array_pluck($users, 'email');
And then run the Mail::queue
Mail::queue('mail.your_view', [], function($message) use ($emails) {
$message->from('test#from.com', 'Mail Notification');
$message->to($emails);
$message->subject('Your Subject');
});
**
Sending multiple users
**
$users = UsersGroup::where(['groups_id' => $group->id])->get();
if(!$users->count()) {
Mail::send('emails.groups.delete-group', [
'group' => $group->title,
], function ($message) use ($group, $users) {
$message->from(Config::get('mail.from.address'), Config::get('mail.from.name'))
->subject(Lang::get('groups.group_deleted'));
foreach ($users as $user) {
$message = $message->to($user->users->email);
}
});
}