I have created an app using Laravel and uploaded it to the live server (shared hosting). I have programmed it such that emails are sent to clients together with email attachments generated via DOMPDF. The problem is that I receive a phishing error in Gmail inbox concerning the attachment. When I remove the image from the attachment the error disappears. Seems like am not inserting the image properly in the blade file. Please assist me on how to add the image via the controller then parse it to the view (that is sent as an attachment)?
~ Regards
PDF Controller that loads the PDF file
class PDFController extends Controller
{
//Loads the PDF document
public function getPDF(){
$pdf = \PDF::loadView('pdf.customer', ['format' => 'A5-L']);
return $pdf->stream('customer.pdf')->header('Content-Type','application/pdf');
}
}
Email blade
<div class="logo pull-right">
<?php $image_path = '/img/logo.png'; ?>
</div>
PagesController that sends the email
$pdf = PDF::loadView('pdf.customer', $data);
Mail::send('emails.feedback', $data, function($message) use ($data, $pdf){
$message->from('info#*************');
$message->to($data['email']);
$message->subject('Feedback');
//Attach output from PDF doc, customer.pdf is the name of the file attached
$message->attachData($pdf->output(),'customer.pdf');
});
If you are using Laravel inbuilt mailer then, Follow below structure
Crete mail class using
php artisan make:mail MailFileName
MailFileName.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MailFileName extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('from_email' ,'Title')->view('emails.email_ blade_file')->with(['data' => $this->data])->subject($subject)->attach($this->data['attachment_url']);
}
}
In view you are passing $data variable so you can use all this variable just like normal blade file.
View File - resource/views/emails/email_ blade_file.blade.php
In your controller where you want to call mail function add,
SomeController.php
use use App\Mail\MailFileName;
Inside function,
$data['subject'] = 'New message from';
$data['data'] = ['your data'];
Mail::to($to_email)->send(new MailFileName($data));
Related
I want to send two different emails with two different view blade files for submitting a single form.
My Controller
<?php
namespace App\Http\Controllers\Frontend;
use App\Mail\quickQuoteMail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\sysmaster;
use App\Mail\contactEmail;
use Illuminate\Support\Facades\Mail;
class EmailController extends Controller
{
public function contactEmail(Request $request)
{
$data = array(
'ContactPerson'=> $request->FullName,
'ContactEmail'=> $request->Email,
'ContactNumber'=> $request->Number,
'ContactMessage'=> $request->Enquiry,
);
$token = $request->input('g-recaptcha-response');
if(strlen($token)>0)
{
$quickQuoteemail = sysmaster::where('sysm_val_type', 'Contact_Email')->where('sysm_def_id', 'to')->first();
$quickQuote = sysmaster::where('sysm_val_type', 'Contact_Email')->where('sysm_def_id', 'Cc')->first();
if ($quickQuote->sysm_value!=null) {
Mail::to($quickQuoteemail->sysm_value)->cc([$quickQuote->sysm_value, $request->Email])->send(new contactEmail($data));
} else {
Mail::to($quickQuoteemail->sysm_value)->cc($request->Email)->send(new contactEmail($data));
}
return redirect()->back()->with('message','Thank you for contact us.');
}else{
return redirect()->back()->with('message','Please make sure your not a robot');
}
}
}
Mail Function
<?php
namespace App\Mail;
use App\sysmaster;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class contactEmail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$quickQuoteemail = sysmaster::where('sysm_val_type','Contact_Email')->where('sysm_def_id','from')->first();
return $this->from($quickQuoteemail->sysm_value,$this->data['ContactPerson'])
->subject('Contact Us Enquiry')->view('email_contact_quote')->with('data',$this->data);
}
}
I have another view blade file there (contact_email_company) How can I include that view with this function and this UI only will send for the company top one have to go for the only client please help how to do this.
You can use dynamic view name You can pass view name in $data Mail class
Change below in your controller function:
if ($quickQuote->sysm_value!=null) {
$data['view_name'] = 'email_contact_quote';
Mail::to($quickQuoteemail->sysm_value)->cc([$quickQuote->sysm_value, $request->Email])->send(new contactEmail($data));
}
else {
$data['view_name'] = 'contact_email_company';
Mail::to($quickQuoteemail->sysm_value)->cc($request->Email)->send(new contactEmail($data));
}
And in mail class change as below: take view name from $data,
return $this->from($quickQuoteemail->sysm_value,$this->data['ContactPerson'])
->subject('Contact Us Enquiry')->view($this->data['view_name'])->with('data',$this->data);
I'm trying to send an email using laravel however I keep getting the cannot access empty property error whenever I run my code.
I've done my research on the error and it seems to be usually caused by using a $ before the property name, example $this->$username instead of $this->username. However, that isn't the case in my code.
I can't really tell what's causing it nor do I have great experience in Laravel
Here's my mailable class:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class VerificationMail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$data2 = ['companyName' => $this->data['name'], 'verificationCode' => $this->data['verificationCode']];
return $this->from('noreply#REMOVED.com')
->$this->view('emails.verification', $data2);
}
}
My view is saved in resources/views/emails/verification.blade.php
I saw also that this error can sometimes be caused by using $message as variable name inside the views, however that isn't the case with me. I tried loading the view with a normal route without any mail sending involved and it loaded normally.
Can anyone spot it? Thanks.
You have error here:
return $this->from('noreply#REMOVED.com')
->$this->view('emails.verification', $data2);
Use following instead: (remove second $this->)
return $this->from('noreply#REMOVED.com')
->view('emails.verification', $data2);
Hello, I'm making a mail service in laravel, that supports attachment upload, when the user send me the file, I store it in a Amazon S3 driver for further attachment.
Here's my Mail class witch I send the mail object with the copies of email and attachments.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Queue\ShouldQueue;
class Mail extends Mailable
{
use Queueable, SerializesModels;
public $email;
public $attachments;
public $copies;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($email, $copies = [], $attachments = [])
{
$this->email = $email;
$this->copies = $copies;
$this->attachments = $attachments;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$m = $this->from($this->email->sender->email, $this->email->sender->name)
->replyTo($this->email->sender->email, $this->email->sender->name)
->subject($this->email->subject);
foreach ($this->copies as $copy) {
if ($copy->type == 'CC') {
$m->cc($copy->destiny->email);
} else {
$m->bcc($copy->destiny->email);
}
}
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
$attachmentParameters = [
"as" => $attachment->name,
"mime" => $attachment->mime
];
$m->attach(Storage::disk('s3Attachments')->url($attachment->path), $attachmentParameters);
}
}
return $m->view('emails.text-plain');
}
}
I've already used the dd(Storage::disk('s3Attachments')->url($attachment->path)) and confirmed that it is a string with the full path of the file like the documentation asks.
To add attachments to an email, use the attach method within the
mailable class' build method. The attach method accepts the full path
to the file as its first argument:
Then When I run the code, it brings this error:
[2018-05-05 20:58:52] testing.ERROR: Type error: Argument 2 passed to Illuminate\Mail\Message::attach() must be of the type array, null given, called in /home/lefel/Sites/happymail/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php on line 311
I've tried using attach(), with just one argument:
$m->attach(Storage::disk('s3Attachments')->url($attachment->path));
But Same Error, I'm Using Laravel 5.5 with Amazon SES Driver, and already confirmed that I installed the following dependencies in my package.json:
composer require guzzlehttp/guzzle
"aws/aws-sdk-php": "~3.0"
I've searched the web and didn't find the solution, I need Help Please.
Regards.
I have just faced the exact same issue.
The problem is that you override the $attachments property.
Use another name for this variable and it will work!
For testing purpose, I want to send raw mail via Queue.
I can send a raw mail like this:
Mail::raw('bonjour', function($message) {
$message->subject('Email de test')
->to('test#example.org');
});
But is there a way to send a raw mail via Queue (without creating a View nor Mailable)?
You can use dispatch helper function to push Closure onto the Laravel job queue:
dispatch(function () use ($name) {
Mail::raw('bonjour ' . $name, function($message) {
$message->subject('Email de test')
->to('test#example.org');
});
});
I searched the past days without any outcome to accomplish exact this: a raw mail that can be queued.
Unfortunately I didn't found a solution without using Mailables and Views.
I assume you have the same reason as me: You want to send a 100% dynamically generated Mail from a string.
My solution was:
creating a view that only contains one variable: <?php echo $content;
create a mailable, passing the content to the constructor and set it to $this->content
copy everything inside the old mail-closure into the build-method of the mailable and replace every $message-> with $this
queue it ;)
public function send(Request $request) {
$to = "test#example.org";
$subject = "email de test";
$content = "bonjour";
Mail::send(new RawMailable($to, $subject, $content));
}
view (/ressources/view/emails/raw.blade.php):
{!! $content !!}
mailable:
<?php
namespace App\Mail;
use Dingo\Api\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class RawMailable extends Mailable
{
use Queueable, SerializesModels, ShouldQueue;
private $mailTo;
private $mailSubject;
// the values that shouldnt appear in the mail should be private
public $content;
// public properties are accessible from the view
/**
* Create a new message instance.
*
* #param LayoutMailRawRequest $request
*/
public function __construct($to, $subject, $content)
{
$this->content = $content;
$this->mailSubject = $subject;
$this->mailTo = $to;
}
/**
* Build the message.
*
* #throws \Exception
*/
public function build()
{
$this->view('emails.raw');
$this->subject($this->mailSubject)
->to($this->mailTo);
}
}
Leave the paramaters view and variables with an empty array each one and add the line $mail->setBody($html, 'text/html') inside the function.
Mail::queueOn(
'name_of_queue',
[],
[],
function($mail) use ($destination_email, $destination_name, $html) {
$mail->to($destination_email, $destination_name);
$mail->subject($subject);
$mail->setBody($html, 'text/html');
}
);
I have a controller which mainly has two functions. First function adds the user to the database and sends an email (receipt.blade.php) to the user.The second function allows the user to get all the receipts for a particular email that a user enters.
Right now, if I directly go to the page where the user can enter the email and get all receipts, its working fine. But, if I try to do it through the process of adding a new user I get the error described in the title after I click submit to adding the user. However, it has added the user to the database, but shows the error because its sending the email which is the receipt.blade.php and has the undefined variable. My controller has these:
use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
The first Function is:
public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
'first_name'=> $data->first_name,
'last_name' => $data->last_name,
'email' => $data->email,
'video_count' => $video_count,
'amount_donated' => $data->amount_donated,
});
$mailer
->to($data->input('email'))
->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
($data->input('amount_donated'))));
return redirect()->action('PagesController#thank1');
}
My mailing file(mailable) is:
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $first_name, $last_name, $amount_donated;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($first_name,$last_name,$amount_donated)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->amount_donated = $amount_donated;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.receipt');
}
}
The second function is:
public function getUserReceipts(Request $data){
$email = $data->email;
$donor = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donor'));
}
The receipt file simply contains:
#foreach($donor as $value)
{{ $value->first_name }}
{{ $value->last_name }}
{{ $value->amount_donated }}
#endforeach
The error I'm getting seems to be because donor in the receipt file is
undefined and I'm not sure how to fix it as it was passed with compact
in the second function.
Would really appreciate the help.
From your last question i can see the problem. The issue is that you're reusing the same view which takes two different set of data. When used with the method getUserReceipts, it generates receipts for multiple donors. But when you send the mail you're using the view as the mail content. In that case you need to use the properites set in the mailable class. The ideal case would be to create a new view for sending a single email receipt and handle it like so.
Change your mailable class to this. With this you can use all the properties of the donor in your view.
<?php
namespace App\Mail;
use App\Donor;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $donor;
public function __construct(Donor $donor)
{
$this->donor = $donor;
}
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.singlereceipt');
}
}
Change the way you send the email to
\Mail::to($data->input('email'))->send(new \App\Mail\MyMail($donor));
Create a new view singlereceipt.blade.php and use the following code instead.
{{ $donor->first_name }}
{{ $donor->last_name }}
{{ $donor->amount_donated }}
You have an error in sending data to view. If you send data using with() function than syntax should be:
return view('emails.receipt')->with('donor', $donor);
See Passing Data To Views