laravel 5.4 embed image in mail

前端 未结 6 1008
无人及你
无人及你 2020-12-17 10:21

I have just upgraded my 5.2 install of laravel to 5.3 and then to 5.4 following the official upgrading methods.

I am now tryin

相关标签:
6条回答
  • 2020-12-17 10:36

    It seems that the older $message->embed doesn't work nicely with Markdown emails. Like you mentioned in the comments it seems broken since 5.4

    But you could just try it like this inside your markdown email:

    This is your logo 
    ![Some option text][logo]
    
    [logo]: {{asset('/img/official_logo.png')}} "Logo"
    

    Like shown here: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images

    Asset function reference: https://laravel.com/docs/5.4/helpers#method-asset

    0 讨论(0)
  • 2020-12-17 10:37

    Solved my issue!

      <img src="{{ $message->embed(base_path() . '/img/logo.png') }}" />
    Controller:
    
    
    \Mail::send(['html' =>'mail'],
            array(
                'name' => $r->name,
                'email' => $r->email,
                'user_message' => $r->message,
               // 'telephone'=>$r->telephone,
               // 'subject'=>$r->subject
            ), function($message) use ($r)  {
    
                $message->to('abc@gmail.com')->subject('Contact Form || abc.com');
                $message->from($r->email);
                // ->setBody($r->user_message); // assuming text/plain
    
            });
    

    If you are in localhost , you can use public_path instead of base_path function

    0 讨论(0)
  • 2020-12-17 10:49

    Try this:

    <img src="data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}}" alt="">
    

    or

    ![](data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}})
    
    0 讨论(0)
  • 2020-12-17 10:49

    You could try the following:

    class WelcomeCandidate extends Mailable
    {
        use Queueable, SerializesModels;
    
        public $message;
        public function __construct(User $user)
        {
            $this->user = $user;
            $this->message = (object) array('image' => '/path/to/file');
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:51

    I just encountered the same issue and found a solution.

    Before rendering images you have to create a symbolic link from "public/storage" to "storage/app/public" using this command:

    php artisan storage:link
    

    In "storage/app/public" you should have a folder "images" Now you can render this code in your markdown.blade.php:

    !['alt_tag']({{Storage::url('/images/your_image.png')}})
    

    Second option is similar:

    !['alt_text']({{Storage::url($comment->user->image->path)}}) 
    

    Both work fine

    0 讨论(0)
  • 2020-12-17 10:59

    You can also use this useful package

    https://github.com/eduardokum/laravel-mail-auto-embed

    Taken from the readme

    Its use is very simple, you write your markdown normally:

    @component('mail::message')
    # Order Shipped
    
    Your order has been shipped!
    
    @component('mail::button', ['url' => $url])
    View Order
    @endcomponent
    
    Purchased product:
    
    ![product](https://example.com/products/product-1.png)
    
    Thanks,<br>
    {{ config('app.name') }}
    @endcomponent
    

    When sending, it will replace the link that would normally be generated:

    <img src="https://example.com/products/product-1.png">
    

    by an embedded inline attachment of the image:

    <img src="cid:3991f143cf1a86257f8671883736613c@Swift.generated">
    
    0 讨论(0)
提交回复
热议问题