How to add more parameter on controller laravel mail - laravel

I want to send email using Laravel and success with this code
Mail::to($email)->send(new SendMail($name))
Now I want to send more parameter like subject with this code, but didn't work.
No error message but email not being sent.
Mail::send(new SendMail($name function($message) use ($email){
$message->to($email)->subject('Verify your Account');
}));
Any suggestions? Thanks in advance!

You can use something like that
$template = 'view.email';
$paramets = ['data'=>$data];//Your to value to print in email
$email = 'to#email.com'// to email
$cc = 'cc#email.com' // or null
$toName = 'John Doe' // or null
$subject = 'Verify your Account';
Mail::send($template, $parameters, function($message) use($email,$cc,$toName,$subject) {
$message->from(env('sender'), env('nameSender'));
if($cc) $message->cc($cc);
$message->to($email, $toName)
->subject($subject);
});
I am using a generic method. So i can use this method to all emails that i need to send

Related

Address in mailbox given [$to_name,$to_email] does not comply with RFC 2822, 3.6.2

i get that error here is my store function am trying to send mail to that email
public function store(Request $request)
{
$to_name = 'TO_NAME';
$to_email = 'TO_EMAIL_ADDRESS';
$data = array('name'=>"Sam Jose", "body" => "Test mail");
Mail::send('layouts.mail', $data, function($message) use ($to_name,$to_email) {
$message->to('$to_name,$to_email');
$message->subject('Booking Enquiry');
$message->from('kisilamapeni#gmail.com','kisila');
});
echo "Email sent";
}
If you want variable parsed from string, you have to use " instead of '
$message->to("$to_name, $to_email");
In Laravel's Illuminate/Mail/Message method to has three arguments, and email address is first
https://laravel.com/api/5.8/Illuminate/Mail/Message.html#method_to
So, you should in general omit your quotes:
$message->to($to_email, $to_name);

Why giving undefined variable in sending Email attachment in Laravel?

I can't pass any variable to Email body from Controller. I have searched many solutions but no is matching with me.
I am using Mailable. But it is empty. I am not sure it is creating problem or not.
public function __construct(){}
public function build(){}
I have tested with dummy Email without passing variable. Email is sending successfully. So, I think there is no problem with configuration.
Function in controller:
public function mail()
{
$info = Invoice::find(65)->first();
// 65 is giving me value. I have checked with dd()
$data = [
'invoice' => $info->invoiceNumber
];
Mail::send(['text'=>'invoice.test'], $data, function($message) use($data){
$pdf = PDF::loadView('invoice.test');
$message->to('example#gmail.com','John Smith')->subject('Send Mail from Laravel');
$message->from('from#gmail.com','The Sender');
$message->attachData($pdf->output(), 'Invoice.pdf');
});
return 'Email was sent';
}
test.blade.php
<h1>It is working!! {{ $invoice }}</h1>
Data sending style is followed by:
Laravel Mail::send how to pass data to mail View
Try adding the $data with the PDF view:
$pdf = PDF::loadView('invoice.test', $data);
But if you want to add an email body content different with the PDF content, try this:
Mail::send([], $data, function($message) use($data){
$pdf = PDF::loadView('invoice.test', $data);
$message->to('example#gmail.com','John Smith')->subject('Send Mail from Laravel');
$message->from('from#gmail.com','The Sender');
$message->setBody('sample mail content');
$message->attachData($pdf->output(), 'Invoice.pdf');
});

Sending email notification to existing user Laravel 5.8

I am trying to send email to all existing admins once a user sends a request. I am using a Notifiable class to send emails but I am getting an error of :-
Call to a member function notify() on string
The code for me to notify all admins:-
$all_admins = User::whereHas('roles', function ($query) {
$query->where('admin', '1');
})->paginate(100);
foreach($all_admins as $admin){
if ($type == 1){
$subject = $username.' made a lawyer request.';
$message = "User: ". $user;
$message2 = "Please handle the request in the admin portal under the request tab.";
}
$user = $admin->email;
//$admin->email has a string of an email
$user->notify(new EmailAdmin($subject, $message, $message2)); <--
***The above code is the code that is marked when laravel is returning the error to me.
}
You wrote it yourself. $admin->email is a string. The error says you're calling a function on a string. You're doing it wrong.
The documentation says you're supposed to call notify() on a User model that has the trait.
(answered in the comments by #IGP)

When i am trying to send mail from contactUS form getting this error using swiftmailer in Laravel 5.2

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']);
});

How to set up email piping with codeigniter?

I've redirected incoming emails via cPanel to a php script which is below. At the moment it just sends a email reply so I can test that I have the correct info, once I've confirmed that I'll start adding to a database.
What I need to know is how would I use codeigniter to do this?
Also where would I redirect the emails to in cPanel, for example at the moment they go to - public_html/email/parse.php, how would I redirect them to a controller in codeigniter?
I've tried putting this code into a controller but the email bounces back.
#!/usr/bin/php -q
<?php
// get contents of email and add to $email
$email = file_get_contents('php://stdin');
// regular expression to get parts of email that I need
preg_match_all("/(To):\s(.*)\n/i", $email, $to);
preg_match_all("/(Subject):\s(.*)\n/i", $email, $subject);
preg_match_all("/(From):\s(.*)\n/i", $email, $from);
// assign parts of email to variables
$sender = $to[2][0];
$sender = trim($sender, ' " ');
$sender_id = explode('#', $sender);
$sender_id = $sender_id[0];
$subject = $subject[2][0];
$from = $from[2][0];
// make a reply email to test the above works
$reply = "Here's your information!\n\nSender: $sender\nSender ID: $sender_id\nFrom: $from\nSubject: $subject";
// send email to test
mail($from, 'From my email pipe!', $reply, 'From:noreply#mydomain.me');
?>

Resources