Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
Swiftmailer和PHP变量_Php_Variables_Swiftmailer - Fatal编程技术网

Swiftmailer和PHP变量

Swiftmailer和PHP变量,php,variables,swiftmailer,Php,Variables,Swiftmailer,我有以下代码: <?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 us

我有以下代码:

<?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等)?(我不想键入个人电子邮件/姓名)

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

例如,已格式化的原始代码是:

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_电子邮件或您选择的任何变量名称。。。以及其他文本字符串

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);