在 Laravel 中使用准备好的模板发送电子邮件时遇到麻烦:

Trouble Sending Emails with Prepared Templates in Laravel:

提问人:anbu 提问时间:10/15/2023 最后编辑:anbu 更新时间:10/16/2023 访问量:58

问:

我遇到了错误和问题,尤其是在处理异常和转义 HTML 字符时。我将不胜感激任何有关使用 laravel 10 发送带有模板的电子邮件的最佳实践的指导,以及解决我面临的问题的任何见解。先谢谢你!

在邮件中,只有html代码是可见的,预期是html页面。

我正在使用 [][1]https://app.bootstrapemail.com/templates/free-simple-card

 public function prepareTemplate($subject, $body, $templateId)
    {
        $subject = htmlentities($subject, ENT_QUOTES, 'UTF-8');
        $body = htmlentities($body, ENT_QUOTES, 'UTF-8');
        // Load the template from your database or source
        $template = $this->templates::where('InternalID', $templateId)->first(); // Assuming you want to retrieve a single template.

        $templateHeader = $template->Header;
        $templateFooter = $template->Footer;

    // Replace placeholders in the template body with dynamic content
        $templateBody = str_replace('{{ subject }}', $subject, $template->Body);
        $templateBody = str_replace('{{ body }}', $body, $templateBody);

        // Escape HTML characters in subject and body


        // Combine the header, dynamic body, and footer
        $completeTemplate = $templateHeader . $templateBody . $templateFooter;

        return $completeTemplate;

    } 

 // SendMail

  



 public function SendMail($notification)
    {
    
        // Inject the Templates model when creating TemplateService
        $templateService = new TemplateService(new Templates);
        $template  =  $templateService->prepareTemplate($notification->Subject,$notification->Body,1);
    
        try {
            Mail::to($notification->Contact)->send(new MyTestEmail($notification,$template));
        } catch (\Exception $e) {
            $this->updateNotificationService->UpdateTriedCount($notification);
            Log::info(['Failed Notification '=>$notification]);
           return false;
        }
    }

我正在使用此模板 https://app.bootstrapemail.com/templates/free-simple-card

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/dBZAi.png

    <?php
    
    namespace App\Mail;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Mail\Mailable;
    use Illuminate\Mail\Mailables\Content;
    use Illuminate\Mail\Mailables\Envelope;
    use Illuminate\Queue\SerializesModels;
    
    
    class MyTestEmail extends Mailable
    {
        use Queueable, SerializesModels;
    
        /**
         * Create a new message instance.
         */
    
        private $notification;
        private $template;
    
        public function __construct($notification ,$template)
        {
            $this->notification = $notification;
            $this->template = $template;
        }
    
        /**
         * Get the message envelope.
         */
        public function envelope(): Envelope
        {
            return new Envelope(
                subject: $this->notification->Subject,
            );
        }
    
        /**
         * Get the message content definition.
         */
        public function content(): Content
        {
            return new Content(
                html: 'mail.test-email',
                with: [
                    'template'=>$this->template
                ]
            );
        }
    
        /**
         * Get the attachments for the message.
         *
         * @return array<int, \Illuminate\Mail\Mailables\Attachment>
         */
        public function attachments(): array
        {
            return [];
        }
    }
PHP Laravel 电邮

评论

0赞 Olivier 10/15/2023
“我遇到了错误和问题”你应该更具体地了解它们。
0赞 anbu 10/15/2023
我正在尝试使用 bootstrap.mail 模板提供的 html 模板发送邮件。我正在使用 Laravel 10 ,这里的问题是 HTML 代码直接在邮件中发送
0赞 Olivier 10/15/2023
所以你的意思是 HTML 没有被解释?好的,但是您没有发布您的课程,所以我们不知道它的作用。MyTestEmail
0赞 anbu 10/15/2023
对不起,回复晚了,我已经添加了MyTestEmail文件。
0赞 xenooooo 10/16/2023
你能添加你的文件吗?mail.test-email

答:

0赞 anbu 10/16/2023 #1
  1. php artisan vendor:publish --tag=laravel-mail 2.in test.email.blade 页面更新为 {!! $template !!}

在此 2 个步骤之后,问题得到解决并按预期工作。