如何设置Mailersend PHP API模板变量?遇验证异常求助
解决MailerSend PHP API模板变量设置错误
错误原因
报错提示variables.0.email field does not exist in to.*.email,核心问题是模板变量绑定的邮箱不在收件人列表中。MailerSend要求每个Variable实例的邮箱必须与Recipient中的收件人邮箱完全匹配——因为模板变量是对应单个收件人做内容替换用的,你当前用发件人邮箱作为变量绑定邮箱,不符合规则。
修正代码
将Variable的第一个参数改为收件人邮箱client_email@gmail.com即可:
... $mailersend = new MailerSend(['api_key' => $maSecretKey]); $recipients = [ new Recipient('client_email@gmail.com', 'the email'), ]; // 把变量绑定的邮箱改为收件人邮箱 $variables = [ new Variable('client_email@gmail.com', ['date' => date('Y-m-d'), 'order_number'=>250, 'delivery'=>"chosen"]) ]; $tags = ['tag']; $emailParams = (new EmailParams()) ->setFrom('my_registered_email@mail.com') ->setFromName('Website') ->setRecipients($recipients) ->setSubject('Se registro un pago nuevo') ->setTemplateId($templateVenta) ->setVariables($variables); //->setTags($tags); $mailersend->email->send($emailParams);
额外验证要点
- 若存在多个收件人,需为每个收件人单独创建对应的
Variable实例,每个实例的邮箱必须匹配对应收件人邮箱。 - 模板中的变量名要与代码传入的数组键名完全一致(比如模板用
{{date}},代码就要传'date' => ...)。
内容的提问来源于stack exchange,提问作者fran35
相关产品推荐
相关产品推荐

