2013-04-12 1 views
0

У меня есть следующий код:SwiftMailer и PHP Переменные

<?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('[email protected]' => 'John Doe')) 
->setTo(array("[email protected]" => "John Smith")) 
->setBody('This is a test email message') ; 
//*** Send the message *** 
$result = $mailer->send($message); 
?> 

Как использовать PHP переменную (т.е. $ электронная почта, $ имя и т.д.) в setFrom, Сетто и setBody массивов? (Я не хочу набирать отдельные письма/имена).

ответ

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('[email protected]' => 'John Doe')) ->setTo(array("[email protected]" => "John Smith")) ->setBody('This is a test email message') ; 
//*** Send the message *** 
$result = $mailer->send($message); 

Теперь, вы можете просто заменить «[email protected]» с $ 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);