【问题标题】:Laravel 5.4 mail markdown 2 button inlineLaravel 5.4 邮件降价 2 按钮内联
【发布时间】:2018-11-27 08:59:56
【问题描述】:

我想知道如何将按钮放在一起。

现在它在一个单独的行上。

@component('mail::message')
    Dear {{$vendor_name}}

    Product {{$product_id}} : {{$product_name}} price id : {{$price_id}} will expire on {{$date_expire}}.
    Please renew price.


@component('mail::button', ['url' => 'http://phuketjettour.com/', 'color' => 'green'])
    Phuket Jet Tour
@endcomponent
@component('mail::button', ['url' => 'http://phuketjettour.com/s/vendors'])
    Vendor submission
@endcomponent
    Thanks,<br>
    Phuket Jet Tour

@endcomponent

【问题讨论】:

  • 请发布您的电子邮件刀片或配置此按钮的代码
  • @Dearwolves 我在帖子中更新刀片。

标签: laravel email markdown


【解决方案1】:

内嵌按钮的动态数量

对我来说,以下工作在 Laravel 降价邮件中创建了 2、3、4(动态数量)按钮:

1) 为您的自定义组件添加一个新目录

首先让我们扩展邮件配置以包含我们的新组件目录:

config/mail.php

<?php 

// ...

'markdown' => [
    'theme' => 'default',

    'paths' => [
        resource_path('views/vendor/mail'),
        resource_path('views/emails/components') // <-- This line was added
    ],
],

2) 为内联按钮创建新组件

您需要同时创建组件的 HTML 和文本版本。否则你会得到一个 InvalidArgumentException "View $name not found":

www/resources/views/emails/components/html/buttons.blade.php

这是 HTML 文件:

<table class="action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table border="0" cellpadding="0" cellspacing="0" role="presentation">
@if(null !== ($headline ?? null))
<tr><td><h2 style="text-align: center;">{{ $headline }}</h2></td></tr>
@endif
<tr>
<td>
@foreach($buttons as $button)
<a href="{{ $button['url'] }}" class="button button-{{ $button['color'] ?? 'primary' }}" target="_blank" rel="noopener">{!! $button['slot'] !!}</a>
@endforeach
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

www/resources/views/emails/components/text/buttons.blade.php

这是文本文件

@if(null !== ($headline ?? null))
{{ $headline }}
------------------------------
@endif
@foreach($buttons as $button)
{!! $button['slot'] !!}: {{ $button['url'] }}
@endforeach

3) 在您的邮件中包含新组件:

现在您可以像这样在降价电子邮件中包含该组件:

带有2 个内嵌按钮 和一个标题 的示例:

@component('mail::buttons', [
    'headline' => 'Share link',
    'buttons' => [
        [
            'url' => 'https://wa.me/?text=' . urlencode('Whatsapp text'),
            'slot' => 'WhatsApp',
        ],[
            'url' => 'https://t.me/share/url?text=' . urlencode('telegram text'),
            'slot' => 'Telegram',
        ]
    ]
])
@endcomponent

带有 3 个内嵌按钮的示例 没有标题:

@component('mail::buttons', [
    'buttons' => [
        [
            'url' => 'https://wa.me/?text=' . urlencode('Whatsapp text'),
            'slot' => 'WhatsApp',
        ],[
            'url' => 'https://t.me/share/url?text=' . urlencode('telegram text'),
            'slot' => 'Telegram',
        ],[
            'url' => 'https://twitter.com/intent/tweet?text=' . urlencode('Twitter text'),
            'slot' => 'Twitter',
        ]
    ]
])
@endcomponent

3 个内嵌按钮 的示例具有不同的颜色

@component('mail::buttons', [
    'buttons' => [
        [
            'url' => 'https://wa.me/?text=' . urlencode('Whatsapp text'),
            'slot' => 'WhatsApp',
            'color' => 'blue' // This is the default
        ],[
            'url' => 'https://t.me/share/url?text=' . urlencode('telegram text'),
            'slot' => 'Telegram',
            'color' => 'green'
        ],[
            'url' => 'https://twitter.com/intent/tweet?text=' . urlencode('Twitter text'),
            'slot' => 'Twitter',
            'color' => 'red'
        ]
    ]
])
@endcomponent

【讨论】:

  • 你拯救了我的一天!
  • 抱歉,迟到的答案。
【解决方案2】:

将变量传递给刀片模板时,请使用单引号 ( ' ) 而不是双引号 ( " )。

$btn_style = " color: #FFF; border: 1px solid #FEFEFE; padding: 5px 30px;";    
$buttons = '<span style="text-align: center; width: 100%; display: inline-block;">
                <a href="'. url('/login') .'" style="background: #449a44; '.$btn_style.'"> Accept </a>
                <a href="'. url('/some/url/'. $id) .'" style="background: #b76161; '.$btn_style.'"> Decline </a>
            </span>';

渲染这段代码:

编辑您的应用\邮件

$this
    ->subject('Subject')
    ->markdown('emails.tempalte')
    ->with([
      'buttons' => $buttons,
    ]);

刀片模板查看\电子邮件

{!! $buttons !!}

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    你可以把它放在一个&lt;div&gt; 例如:

    <div class = "row>
    

    你的按钮在这里

    </div>
    

    如果没有帮助,请分享您的代码


    试试这个:

    <span style="display: inline;">
    
    @component('mail::button', ['url' => 'http://phuketjettour.com/', 'color' => 'green'])
        Phuket Jet Tour
    @endcomponent
    @component('mail::button', ['url' => 'http://phuketjettour.com/s/vendors'])
        Vendor submission
    @endcomponent
    
    </span>
    

    【讨论】:

      【解决方案4】:

      这只是一种解决方法。我检查了来自 laravel 项目的电子邮件。

      试试这个代替你的按钮组件:

      <a href = "http://phuketjettour.com/" class="button button-green"> Phuket Jet Tour</a>
      <a href = "http://phuketjettour.com/" class="button button-blue">SAMPLE !</a>
      

      它内联你的按钮。然后只需调整其他元素。如果您想将按钮放在降价上,那么您喜欢:

      关于你的降价:

      $button1= "   <a href = "http://phuketjettour.com/" class="button button-green"> Phuket Jet Tour</a>";
      $button2 = "<a href = "http://phuketjettour.com/" class="button button-blue">SAMPLE !</a>";
      
        $emailMarkdown = $this->markdown('your_email_template')
                      ->subject('your_subject)
                      //you will define here the variables you need to pass in the template
                      ->with([    
                         'emailBody' => $emailBody,
                         'button_first' => $button1,
                         'button_second' => $button2,
                      ]);
      
           return $emailMarkdown;
      

      然后在电子邮件模板上:

      @component('mail::message')
              Dear {{$vendor_name}}
      
              Product {{$product_id}} : {{$product_name}} price id : {{$price_id}} will expire on {{$date_expire}}.
              Please renew price.
      
          {!!$button_first!!} {!!$button_second!!}
      @endcomponent
      

      【讨论】:

      • 如果在markdown中使用html。它将 html 更改为字符串 Product 19361 : 2142352 价格 id : 2124284 将于 2018 年 12 月 27 日 00:00:00 到期。请更新价格。 phuketjettour.com" class="button button-green">普吉岛喷气之旅 phuketjettour.com" class="button button-blue">样品!
      • 我所做的是你直接把按钮放在电子邮件刀片模板上。
      【解决方案5】:

      我已经这样做了,css当然应该在一个单独的文件中

      <style>
          .email-row {
          }
          .email-col-2 {
              width: 49%;
              display: inline-block;
          }
      </style>
      
      @component('mail::message')
      <div class="email-row">
          <div class="email-col-2">
              @component('mail::button', ['url' => '453634', 'color' => 'light'])
                  btn1
              @endcomponent
          </div>
          <div class="email-col-2">
              @component('mail::button', ['url' => '123', 'color' => 'green'])
                  bnt2
              @endcomponent
          </div>
      </div>
      
      @endcomponent
      

      【讨论】:

        【解决方案6】:

        要解决此问题,只需将此代码放入电子邮件刀片文件中:

        <div style="text-align: center">
          <a href="{{ $link }}" class="button button-red">Cancel</a>
          <a href="{{ $link }}" class="button button-blue">Reschedule</a>
          <a href="{{ $link }}" class="button button-green">Confirm</a>
        </div>
        

        【讨论】:

          猜你喜欢
          • 2015-10-27
          • 2018-07-10
          • 2018-02-24
          • 1970-01-01
          • 1970-01-01
          • 2019-01-13
          • 1970-01-01
          • 2021-06-16
          • 2016-12-15
          相关资源
          最近更新 更多