daugaard47's avatar

Sending Markdown Emails

I have a feature on my site similar to a contact form, but I'm using it so a Sponsor can send a letter to their Sponsored Child. I wanted to use Markdown emails rather than plan html.

I currently have this working with no issues, but seems like it was way to much work and thought that there had to be an Easier Way.

If anyone could review and give me some pointers that would be great.

Route:

Route::post('/email/letter', 'ProfileController@sponsorLetterPost')->name('sponsorLetterPost');

Form:

<form enctype="multipart/form-data" action="{{ route('profile.sponsorLetterPost') }}" method="post">
        @csrf
    //Name (Pre-Populated)
    <input type="text" value="{{ $user->first_name.' '.$user->last_name }}" name="name">
    //Email (Pre-Populated)
    <input type="text" value="{{ $user->email }}" name="email">
    //Letter/Message
    <textarea type="text" placeholder="Type your letter here" name="smessage"></textarea>
    // Hidden Field
    <input type="hidden" name="subject" value="Letter to {{ $name }}">
    <input type="hidden" name="child_cin" value="{{ $kid->kid->cin }}">
    <input type="hidden" name="child_name" value="{{ $name }}">
    <input type="hidden" name="child_age" value="{{ $kid->kid->age }}">
    <input type="hidden" name="child_gender" value="{{ $kid->kid->gender }}"> 
    <input type="hidden" name="child_location" value="{{ $kid->kid->current_town.','.' '.$kid->kid->current_country }}">
    <button type="submit">Send Letter</button>
</form>

Controller:

    public function sponsorLetterPost(Request $request)
    {
          $data = array(
            'name' =>Purifier::clean($request->name),
            'email' =>Purifier::clean($request->email),
            'smessage' =>Purifier::clean($request->smessage),
            'subject' =>$request->subject,
            'child_cin' =>$request->child_cin,
            'child_name' =>$request->child_name,
            'child_age' =>$request->child_age,
            'child_gender' =>$request->child_gender,
            'child_location' =>$request->child_location,
            );
          Mail::to('[email protected]')
          ->send(new \App\Mail\SponsorLetter(
            $request->input('name'),
            $request->input('email'),
            $request->input('smessage'),
            $request->input('subject'),
            $request->input('child_cin'),
            $request->input('child_name'),
            $request->input('child_age'),
            $request->input('child_gender'),
            $request->input('child_location')
          ));
          return back()->with('success','Your letter has been sent.');
      }

Mail SponsorLetter:

class SponsorLetter extends Mailable
{
    use Queueable, SerializesModels;
    public $name;
    public $email;
    public $smessage;
    public $subject;
    public $child_cin;
    public $child_name;
    public $child_age;
    public $child_gender;
    public $child_location;
 
    public function __construct( $name, $email, $smessage,$subject, $child_cin, $child_name,$child_age,$child_gender,$child_location)
    {
            $this->name = $name;
            $this->email =Purifier::clean($email);
            $this->smessage =Purifier::clean($smessage);
            $this->subject =$subject;
            $this->child_cin =$child_cin;
            $this->child_name =$child_name;
            $this->child_age =$child_age;
            $this->child_gender =$child_gender;
            $this->child_location =$child_location;
    }
    /**
     * Build the message.
     *
     * @return $this
     */

    public function build()
    { 
        return $this->markdown('emails.staff.sponsorletter');
    }
}

Markdown Email Sponsorletter:

@component('mail::message')
### Hello,<br>{{ ucfirst(strtolower($name)) }} has sent a letter to {{ ucfirst(strtolower($child_name)) }}<br>

###**Sponsored Child Info:**
@component('mail::panel')
- **Child ID Number:** {{$child_cin}}
- **Child Name:** {{ ucfirst(strtolower($child_name)) }}
- **Child Age:** {{$child_age}}
- **Child Gender:** {{$child_gender}}
- **Child Location:** {{$child_location}}
@endcomponent

###**Sponsor Info:**
@component('mail::panel')
- **Sponsor Name:** {{ ucfirst(strtolower($name)) }}
- **Sponsor Email:** {{$email}}
@endcomponent

###**Sponsor Letter:**
@component('mail::panel')
{!! nl2br ($smessage) !!}
@endcomponent

 
Thank You,<br>
*- The {{ config('app.name') }}, Notifier*
@endcomponent
0 likes
1 reply
STEREOH's avatar

It's pretty ok except your $datain your controller isn't used so you can delete it.

// either this

     function sponsorLetterPost(Request $request)
    {
          Mail::to('[email protected]')
          ->send(new \App\Mail\SponsorLetter(
            $request->input('name'),
            $request->input('email'),
            $request->input('smessage'),
            $request->input('subject'),
            $request->input('child_cin'),
            $request->input('child_name'),
            $request->input('child_age'),
            $request->input('child_gender'),
            $request->input('child_location')
          ));
          return back()->with('success','Your letter has been sent.');
      }

// or this

// Controller 

     function sponsorLetterPost(Request $request)
    {
       
      $data = array(
         'name' =>Purifier::clean($request->name),
         'email' =>Purifier::clean($request->email),
         'smessage' =>Purifier::clean($request->smessage),
         'subject' =>$request->subject,
         'child_cin' =>$request->child_cin,
         'child_name' =>$request->child_name,
         'child_age' =>$request->child_age,
         'child_gender' =>$request->child_gender,
         'child_location' =>$request->child_location,
         );

      Mail::to('[email protected]')
          ->send(new \App\Mail\SponsorLetter($data));
          return back()->with('success','Your letter has been sent.');
      }

// Mailable

class SponsorLetter extends Mailable
{
    use Queueable, SerializesModels;
    public $name;
    public $email;
    public $smessage;
    public $subject;
    public $child_cin;
    public $child_name;
    public $child_age;
    public $child_gender;
    public $child_location;
 
    public function __construct( $data )
    {
         [
            $this->name,
            $this->email,
            $this->smessage,
            $this->subject,
            $this->child_cin,
            $this->child_name,
            $this->child_age,
            $this->child_gender,
            $this->child_location
         ] = $data;
    }
    /**
     * Build the message.
     *
     * @return $this
     */

    public function build()
    { 
        return $this->markdown('emails.staff.sponsorletter');
    }
}

Please or to participate in this conversation.