问题描述
我将Heredoc存储在这样的MySQL文本类型中。
Hi,$candidate.
This is $interviewer speaking.
我想像下面这样动态地使用heredoc。 (“ $ mailTemplate-> body”是上述MySQL文本类型的字符串。)
$candidate = 'CANDIDATE';
$interviewer = 'INTERVIEWER';
$mailBody = <<< EOM
$mailTemplate->body
EOM;
但是heredoc无法正常工作,变量按原样输出。
Hi,$candidate. This is $interviewer speaking.
有什么主意吗?还是不可能?
谢谢。
解决方法
不,您不能这样做。 const sendMail = async () => {
const mail = await new MailComposer({
to: ...,from: ...,subject: ...,html: ...,});
const message = await mail.compile().build();
const encodedMessage = message
.toString('base64')
.replace(/\+/g,'-')
.replace(/\//g,'_')
.replace(/=+$/,'');
await gmail.users.messages.send({
userId: 'me',requestBody: { raw: encodedMessage },});
}
中的$...
被视为文本。
但是您可以使用sprintf。
$mailTemplate->body
工作example。
输出
$text = "Hello %s this is a %s.";
$valueA = 'World';
$valueB = 'test';
echo sprintf(
$text,$valueA,$valueB
);
用Hello World this is a test.
替换字符串中的所有变量,并在%s
中将这些值作为参数提供。