加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何使用带有php库的xsmtp-api将替换标签添加到SendGrid中的电子

发布时间:2020-12-13 17:16:10 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试在电子邮件的主题中设置我的客户的名称. 这对我的应用程序以及我在 SendGrid API docs中读到的内容非常重要 这是很有可能!. Info – Substitution tags will work in the Subject line as well as the body of the email. 问题是我似乎无法实现这
我正在尝试在电子邮件的主题中设置我的客户的名称.
这对我的应用程序以及我在 SendGrid API docs中读到的内容非常重要
这是很有可能!.

Info – Substitution tags will work in the Subject line as well as the body of the email.

问题是我似乎无法实现这一目标.
起初我可能是因为我已经在电子邮件正文中使用了%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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读