在 Laravel 通知中添加第二个操作或链接

5

我正在使用Laravel发送通知,但是在发送第二个操作时遇到了问题。以下是我的toMail方法的代码:

public function toMail($notifiable)
    {
    return (new MailMessage)
        ->subject($this->options['subject'])
        ->greeting($this->options['greeting'])
        ->line($this->options['contentParagraph1'])
        ->line($this->options['contentParagraph2'])
        ->action('Facebook', 'https://www.facebook.com/')
        ->line($this->options['contentParagraph5'])
        ->action('LinkedIn', 'https://www.linkedin.com/')
        ->line($this->options['contentParagraph3'])
        ->line($this->options['contentParagraph4'])
        ->salutation($this->options['salutation']);
}

我所理解的是第二个操作被执行,似乎覆盖了第一个操作。有没有办法保留两个操作,或者使用链接而不是按钮?
2个回答

7

SimpleMessage类旨在创建具有一个操作按钮的简单消息,您可以在Illuminate/Notifications/Messages/SimpleMessage.php中找到支持该功能的代码,而SimpleMessage电子邮件的模板可以在Illuminate/Notifications/resources/views/email.blade.php中找到——请注意只有一个按钮。

您可以使用Markdown Mail Notifications功能创建更复杂的消息,这将允许您包括任意数量的按钮。您可以像这样实现:

  1. Run the command to generate a new notification and pass in the markdown option, e.g: php artisan make:notification InvoicePaid --markdown=mail.invoice.paid
  2. Open the newly created template, e.g: views/mail/invoice/paid.blade.php
  3. Add as many buttons as you like, e.g:

    @component('mail::message')
      # Introduction
    
      @component('mail::button', ['url' => $url1])
      Button 1 Text
      @endcomponent
    
      @component('mail::button', ['url' => $url2])
      Button 2 Text
      @endcomponent
    
      @component('mail::button', ['url' => $url3])
      Button 3 Text
      @endcomponent
    
      Thanks,<br>
      {{ config('app.name') }}
    @endcomponent
    
  4. Replace your calls to SimpleMessage methods with a reference to your markdown template when constructing your email, e.g:

    return (new MailMessage)
      ->subject($this->options['subject'])
      ->markdown('mail.invoice.paid', $this->options);
    
< p >在markdown方法中的第二个参数是一个数组,通过它您可以将各种值包含在电子邮件中,例如contentParagraph1、greeting和salutation。


4

我针对向标准的SimpleMail对象添加按钮的问题实现了这个解决方法。由于MailMessage::line()可以接受任何实现Htmlable的对象,因此您可以在其中放入任何想要的内容。

您仍然需要重写通知类,以创建MyMultiButtonVerifyEmail的实例(而不是VerifyEmail),但这很容易完成(并且您可能已经不止一次地更新了邮件副本)。

这些默认的Laravel通知类真的不太可重用,是吗?

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Notifications\Action;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;

class MyMultiButtonVerifyEmail extends VerifyEmail {

    public function toMail($notifiable) {
        $verificationUrl = ...;
        $resendUrl = ...;

        return (new MailMessage)
            ->line('some line')
            ->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
            ->line('another line')
            ->line($this->makeActionIntoLine(new Action(Lang::getFromJson('Request New Activation'), $resendUrl)))
            ->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
    } // end toMail()

    private function makeActionIntoLine(Action $action): Htmlable {
        return new class($action) implements Htmlable {
            private $action;

            public function __construct(Action $action) {
                $this->action = $action;
            } // end __construct()

            public function toHtml() {
                return $this->strip($this->table());
            } // end toHtml()

            private function table() {
                return sprintf(
                    '<table class="action">
                        <tr>
                        <td align="center">%s</td>
                    </tr></table>
                ', $this->btn());
            } // end table()

            private function btn() {
                return sprintf(
                    '<a
                        href="%s"
                        class="button button-primary"
                        target="_blank"
                        style="font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Helvetica, Arial, sans-serif, \'Apple Color Emoji\', \'Segoe UI Emoji\', \'Segoe UI Symbol\'; box-sizing: border-box; border-radius: 3px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); color: #fff; display: inline-block; text-decoration: none; -webkit-text-size-adjust: none; background-color: #3490dc; border-top: 10px solid #3490dc; border-right: 18px solid #3490dc; border-bottom: 10px solid #3490dc; border-left: 18px solid #3490dc;"
                    >%s</a>',
                    htmlspecialchars($this->action->url),
                    htmlspecialchars($this->action->text)
                );
            } // end btn()

            private function strip($text) {
                return str_replace("\n", ' ', $text);
            } // end strip()

        };
    } // end makeActionIntoLine()

}

为了后代,这是针对 Laravel 5.8.32 编写的。在框架的以后版本中可能有更好的实现方式。你的情况可能会有所不同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接