Reputation: 71
I recently upgraded to laravel 5.4 and have been able to successfully build and send a markdown mailable email to my mailtrap.io and my personal email with no problem and it works great! My problem is I use a third party email service that requires text and html views in separate string variables. I know I can build separate templates to accomplish this, but I would like to use the convenience and efficiency of markdown mailables. I have a MailController.php where I validate and build information needed to send this request:
Mail::to('[email protected]')->send(new RequestShowing($jrequest));
This runs through my mailable class RequestShowing shown here:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class RequestShowing extends Mailable
{
use Queueable, SerializesModels;
public $request;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('[email protected]')
->to('[email protected]')
->replyTo('[email protected]')
->subject('Contact from '.$this->request->name)
->markdown('emails.request-showing');
}
}
This all works fine with mailtrap.io and testing, but in order to get the text and html views I run the following in my MailController.php
$inquiry = new RequestShowing($jrequest);
dd($jrequest, $jrequest->email, $inquiry);
I get this output with no objects for "view" or "textView"
RequestShowing {#637 ▼
+request: {#665 ▼
+"name": "Sally Johnson"
+"email": "[email protected]"
+"phone": "406-333-5555"
+"comment": "This is a test email"
+"renturl": "http://localhost:8000/for-rent/apartment/3409-1st-ave-n-billings-mt-59101/178"
}
+from: []
+to: []
+cc: []
+bcc: []
+replyTo: []
+subject: null
#markdown: null
+view: null <== need html view
+textView: null <== need text view
+viewData: []
+attachments: []
+rawAttachments: []
+callbacks: []
+connection: null
+queue: null
+delay: null
}
This is where I am stuck. I have looked through forums and documentation with no answer on how to get the two views for my third party email provider.
The documentation for the mailable output is here: https://laravel.com/api/master/Illuminate/Mail/Mailable.html
I don't understand why the $jrequest is preserved and other objects are not displayed, unless they are used and disposed of immediately. Any insights or help is appreciated. Thanks!
Upvotes: 1
Views: 4137
Reputation: 3113
it appears markdown combines the two views together in the markdown format and the non-markdown methods require building two templates for every email.
Yes, that's absolutely correct.
Mailtrap helps with debugging
when viewing a message in Mailtrap the Text
tab shows the plain text version, if it exists. If it does not exist in the message, the tab is grayed out.
the Raw
tab shows the whole message, and you'll see: Content-Type: text/plain
heading the plain text message if it exists, and, following it, Content-Type: text/html
for the html version.
You are absolutely right that before 5.4, ->view('my-email-view-here');
generates no plain text version at all (unless you add ->text('plain-text-version-here').
In contrast, ->markdown('my-markdown-email-view-here');
generates a plain text version in addition to the markdown-to-html render, by default.
Upvotes: 0
Reputation: 71
As usual, familiarity with documentation helps so on the Mail page under configuring the view I found my answer in the Plain Text Emails section:
https://laravel.com/docs/master/mail#configuring-the-view
Like the view method, the text method accepts a template name which will be used to render the contents of the email. You are free to define both a HTML and plain-text version of your message:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->text('emails.orders.shipped_plain');
}
So it appears markdown combines the two views together in the markdown format and the non-markdown methods require building two templates for every email.
A side comment is that I checked the Analysis tab at Mailtrap.io for the markdown emails and the spam score is 0.0 - The spam score for my simple little html template tests was 1.8 which isn't bad for something simple.
I think I will look for another delivery method since that score is too good to pass up! The folks that built the Markdown method have done all the tedious work of avoiding spam ratings and I am all for that!!
Upvotes: 3