Skip to content
This repository has been archived by the owner on Dec 22, 2019. It is now read-only.

Support for markdown #120

Open
cosmini opened this issue Jul 18, 2017 · 7 comments
Open

Support for markdown #120

cosmini opened this issue Jul 18, 2017 · 7 comments

Comments

@cosmini
Copy link

cosmini commented Jul 18, 2017

I'm trying to integrate mailgun with laravel 5.4 and I wanted to use the markdown defined by vendor.notifications.email. Does anyone know how I might be able to do that? Thanks.

@gomesiago
Copy link

I've the same question, I also can't use markdown with mailgun.

@fvhockney
Copy link

Declare
$view = $this->markdown('yourMarkdownView'); Mailgun::send($view, $data, $callback);

Laravel 5.5

@dukesnuz
Copy link

dukesnuz commented May 9, 2018

@fvhockney Thank you for the fix to this.
I am also trying to use markdown. When I use @fvhockney fix I get the following error.
Method [markdown] does not exist on [App\Http\Controllers\Admin\EmailController].

Would anyone happen to have any ides as to a fix fr this?
Thank you

@fvhockney
Copy link

fvhockney commented May 9, 2018

My apologies...I forgot to come back and update this after finding a bug...

I've successfully got it working, but it was quite an ordeal. In rather broad strokes:

Create a new email View

This allows you to have access to the various 'mail::*' components that laravel has in its vendor folders

Create a custom Mailable class

This allows you to pass the data from your form into the View you just created. Code:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;

class TransactionEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $input;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($input)
    {
        $this->input = $input;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
      $this->markdown('emails.transactionEmail');
      return $this->buildView();
    }
}

Create Controller

The controller is pretty standard. The magic line was to create the view using your new class and then telling laravel to build this. In my case this is (new TransactionEmail($request->input()))->build()

Full code for the method that sends the mail, including namespacing:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;
use Mailgun;
use Bogardo\Mailgun\Mail\Message;
use App\Mail\TransactionEmail;


class TransactionalEmailController extends Controller
{
public function sendMail (Request $request){

    $data = $request->input();
    $view = (new TransactionEmail($request->input()))->build();
    $message = array (
      'from' => 'Name <[email protected]>',
      'to'=>$data['emailTo'],
      'subject'=>$data['emailSubject'],
      'html' => $view['html'],
      'text' => $view['text']
    );

    if (isset($data['emailCC'])) {
      $message['cc'] = $data['emailCC'];
    }

    if (isset($data['emailBCC'])) {
      $message['bcc'] = $data['emailBCC'];
    }

    Mailgun::api()->sendMessage(env('MAILGUN_DOMAIN'), $message);

    return redirect('view');

  }
}

Notice that I've used the Mailgun::api() method to actually send the mail. This was needed to pass in both the html and the markdown. This is needed since mailgun requires that the html and text be declared separately.

Hopefully this helps. I just tried it again before posting this to make sure that it works. I will try to package these together tonight and post the up on github so that the full code is available.

@dukesnuz
Copy link

dukesnuz commented May 9, 2018

@fvhockney Thank you very much, going to try and get this working now.

@dukesnuz
Copy link

dukesnuz commented May 10, 2018

Using Mailgun::api is not working. I am not getting any errors though the emails are not going out. so I am using Mailgun::send except then I get an error. The entire html code is displayed as the error. Any insight would be appreciated.

I am using code similiar to issue #11

Screen shot of error

image

Thank you

@dukesnuz
Copy link

dukesnuz commented May 11, 2018

@fvhockney Hi Thank you for your information. I believe I almost have my code working. A question I have is, will recipient variables work using your example? My emails are being sent except that in the email this prints out %recipients.name% instead of the actual name.

This is my new class $view = (new Emailloads($loads, $recipients, $data))->build();

Thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants