Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用带有php库的xsmtp api向SendGrid中的电子邮件主题添加替换标记_Php_Sendgrid - Fatal编程技术网

如何使用带有php库的xsmtp api向SendGrid中的电子邮件主题添加替换标记

如何使用带有php库的xsmtp api向SendGrid中的电子邮件主题添加替换标记,php,sendgrid,Php,Sendgrid,我正在尝试在电子邮件主题中设置我客户的名称。 这对我的申请非常重要,从我在 这很有可能 信息替换标签将在主题行和邮件正文中起作用 问题是我似乎无法做到这一点。 起初我认为这可能是因为我已经在电子邮件正文中使用了%name%sub,所以我创建了一个新的替换参数名称%nameSubject%, 然而,这是行不通的 我使用以下代码,电子邮件中的其他参数工作正常: /** @description Wrapper method in order to handle possible ex

我正在尝试在电子邮件主题中设置我客户的名称。 这对我的申请非常重要,从我在 这很有可能

信息替换标签将在主题行和邮件正文中起作用

问题是我似乎无法做到这一点。 起初我认为这可能是因为我已经在电子邮件正文中使用了%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('SendGrid\Mail')){
        throw new exception('Dependency exception SendGrid or SendGrid\Mail 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代码示例,显示文档中的替换标记。()

非常感谢,先生,问题是我将此功能用作相当复杂的cron自动邮件程序的一部分(如果我自己可以这么说的话)。我必须使用精确的语法吗?或者在添加主题后调用mail->addSubstitution()一次,在添加主体后再调用一次,这样的顺序就足够了吗?。