Php Laravel:API上的动态电子邮件内容

Php Laravel:API上的动态电子邮件内容,php,laravel,email,Php,Laravel,Email,我正在Laravel上构建一个电子邮件API。我想通过这个API发送关于我其他项目的电子邮件。我通过此API成功发送电子邮件。但是,电子邮件内容是在客户端硬编码的。例如: $body = "Dear $user->name,<br /> Thank you for your order."; $client->request('POST', 'http://api.test/send_mail?token=' . $token, [

我正在Laravel上构建一个电子邮件API。我想通过这个API发送关于我其他项目的电子邮件。我通过此API成功发送电子邮件。但是,电子邮件内容是在客户端硬编码的。例如:

$body = "Dear $user->name,<br />
         Thank you for your order.";

$client->request('POST', 'http://api.test/send_mail?token=' . $token, ['form_params' => [
                'from' => 'client@test.com',
                'title' => 'API Test Mail',
                'to' => 'customer@test.com',
                'body' => $body,
                'signature' => 'Regards'
            ]]);
通过这种方式,我将只从客户端向API发送
用户名
信息。我将不必处理HTML电子邮件内容

然而,我不确定在这个阶段之后该做什么。如何从客户端向API发送和解析
$username
信息?我是否必须在每个参数上向API添加请求?以下是我当前的请求规则:

return [
            'title' => 'required',
            'from' => 'required|email',
            'to' => 'required|email',
            'body' => 'required',
            'signature' => 'nullable',
            'email_template' => 'nullable'
        ];
这也是我的邮件降价:

@component('mail::message')

    {!! $mail['body'] !!}

    @if(array_key_exists('signature', $mail))
        <p>{!! $mail['signature'] !!}</p>
    @endif
@endcomponent
@组件('mail::message')
{!!$mail['body']!!}
@如果(数组\密钥\存在('signature',$mail))
{!!$mail['signature']

@恩迪夫 @端部元件
实际上,动态参数将是
主体
的子成员。如果我想将
$username
作为动态变量添加到电子邮件正文中,是否应该将其添加到请求规则和邮件标记中?如果是这样,我必须对每个保存的不同电子邮件内容及其不同参数执行一些静态事务


如何以更清晰的方式构建此结构?

是的,您应该将每个参数解析到API中。 您可以这样做:

$data['username'] = 'John';
$data['other'] = 'other';

$client->request('POST', 'http://api.test/send_mail?token=' . $token, 
            ['form_params' => 
             [
                'from' => 'client@test.com',
                'title' => 'API Test Mail',
                'to' => 'customer@test.com',
                'data' => $data,
                'signature' => 'Regards',
                'template_id' => 'template_id' // parse if you have multi templates
            ]]);
您可以在API控制器中发送这样的电子邮件

public function sendEmail(Request $request)
{
  
     $template = Template::findorfail($request->template_id);
     
     $body = $template->body;// this is template dynamic body. You may get other parameters too from database. $title = $template->title; $from = $template->from;

     foreach($request->data as $key=>$parameter)
     {
          $body = str_replace('{{'.$key.'}}', $parameter, $body); // this will replace {{username}} with $data['username']
     }

    $request->body = $body;
    $mailObject = new MyMail($request); // you can make php artisan make:mail MyMail
    Mail::to($request->to)->send($mailObject);

}
...
class MyMail extends Mailable
{
    use Queueable, SerializesModels;
    public $request;
  
    public function __construct($request)
    {
        $this->request= $request;
    }

    public function build()
    {
        return $this->subject($this->request['title'])
            ->from($this->request['from'])
            ->markdown('components.mail.myTemplate');
    }
}

然后在
myTemplate.blade.php
中可以呈现
{!!$request['body']!}

希望这对你有帮助

public function sendEmail(Request $request)
{
  
     $template = Template::findorfail($request->template_id);
     
     $body = $template->body;// this is template dynamic body. You may get other parameters too from database. $title = $template->title; $from = $template->from;

     foreach($request->data as $key=>$parameter)
     {
          $body = str_replace('{{'.$key.'}}', $parameter, $body); // this will replace {{username}} with $data['username']
     }

    $request->body = $body;
    $mailObject = new MyMail($request); // you can make php artisan make:mail MyMail
    Mail::to($request->to)->send($mailObject);

}
...
class MyMail extends Mailable
{
    use Queueable, SerializesModels;
    public $request;
  
    public function __construct($request)
    {
        $this->request= $request;
    }

    public function build()
    {
        return $this->subject($this->request['title'])
            ->from($this->request['from'])
            ->markdown('components.mail.myTemplate');
    }
}