0

我有以下代码:

<?php
include 'swiftmailer/lib/swift_required.php';
//*** Create the Transport ***
$transport = Swift_SmtpTransport::newInstance('smtp.My-Hosting', 25)
->setUsername('MyUserName')
->setPassword('Password');
//*** Create the Mailer using your created Transport ***
$mailer = Swift_Mailer::newInstance($transport);
//*** Create a message ***
$message = Swift_Message::newInstance('NameOfInstance')
->setFrom(array('Support@test.com' => 'John Doe'))
->setTo(array("John@test.com" => "John Smith"))
->setBody('This is a test email message') ;
//*** Send the message ***
$result = $mailer->send($message);
?>

如何在 setFrom、setTo 和 setBody 数组中使用 PHP 变量(即 $email、$name 等)?(我不想输入个人电子邮件/姓名)。

4

1 回答 1

0

如果我理解您的问题和代码(因为它是未格式化的),您应该能够简单地用您选择的变量名称替换字符串。

例如,您的原始代码格式化为:

setUsername('MyUserName') ->setPassword('Password'); 
//*** Create the Mailer using your created Transport *** 
$mailer = Swift_Mailer::newInstance($transport); 
//*** Create a message *** 
$message = Swift_Message::newInstance('NameOfInstance') ->setFrom(array('Support@test.com' => 'John Doe')) ->setTo(array("John@test.com" => "John Smith")) ->setBody('This is a test email message') ; 
//*** Send the message *** 
$result = $mailer->send($message); 

现在,您可以简单地将 "John@test.com" 替换为 $from_email 或您选择的任何变量名......等等其他文本字符串。

setUsername('MyUserName')->setPassword('Password'); 
//*** Create the Mailer using your created Transport *** 
$mailer = Swift_Mailer::newInstance($transport); 
//*** Create a message *** 
$message = Swift_Message::newInstance('NameOfInstance')->
    setFrom(array($from_email => $from_name))->
    setTo(array($to_email => $to_name))->setBody($body) ; 
//*** Send the message *** 
$result = $mailer->send($message);
于 2013-04-12T18:33:02.743 回答