如何使用带有php库的xsmtp-api将替代标记添加到SendGrid中的电子邮件主题 [英] How to add substitution tags to the subject of the email in SendGrid using the xsmtp-api with the php library

查看:197
本文介绍了如何使用带有php库的xsmtp-api将替代标记添加到SendGrid中的电子邮件主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在电子邮件的主题中设置我客户的名称。 这对我的应用程序非常重要,从我在SendGrid API docs中读到的 这很有可能!。

信息替换标记将在电子邮件的主题行和正文中使用。

问题是我道布似乎做不到这一点。 起初,我认为这可能是因为我已经在电子邮件正文中使用了%name%SUB,所以我创建了一个新的替代参数%nameSubject%, 然而,它不会起作用。

我使用以下代码,电子邮件中的其他参数运行正常:

    /**
    @description Wrapper method in order to handle possible exception due to programmer maintainer errors.
    A part of a small wrapper class that I have designed to allow 1-line comfortable use of the the SendGrid and transport classes.
   @returns int - 1, unless there is an internal error, or an exception thrown
    */
public function execute(){
    if(!class_exists('SendGrid') || !class_exists('SendGridMail')){
        throw new exception('Dependency exception SendGrid or SendGridMail are not included');
    }
    if(!isset($this->mailingList) && empty($this->mailingList) || sizeof($this->mailingList) == 0){
        throw new exception('Cannot send an email to an empty set of addresses');
    }
    if(!isset($this->subject, $this->body) || empty($this->subject) || empty($this->body)){
        throw new exception('Cannot send an email without both a subject and a body');
    }

    $sendgrid = new SendGrid(SENDGRID_USER, SENDGRID_PASSWORD);

    // $this->importMailList();
    foreach($this->mailingList as $key => $val) {   
        $this->mail->addTo($key, $val);
    }
    $this->mail->
    setFrom($this->fromMail)-> 
    setSubject($this->subject)->
    setText($this->text)->
    setHtml($this->body);
    if(preg_match('/%name%/', $this->body) && !array_filter($this->mailingList, 'is_int') ){
        $this->mail->addSubstitution("%name%", array_values($this->mailingList));
    }
    return $sendgrid->smtp->send($this->mail);

}

任何帮助都非常有用!

推荐答案

应分别为主题或正文设置替代标记。试着这样做:

$this->mail->
setFrom($this->fromMail)-> 
setSubject($this->subject)->addSubstitution("%name%", array("Harry", "Bob"))->
...

您可以用您自己的数组值替换我使用的示例数组。我们还提供了一些在文档中显示替换标记的PHP代码示例。(http://sendgrid.com/docs/Code_Examples/php.html#-Using-Substitutions)

这篇关于如何使用带有php库的xsmtp-api将替代标记添加到SendGrid中的电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆