如何在Swiftmailer的HTML中为IF条件应用正确的语法?

问题描述 投票:0回答:1

我需要将PHP在线表单从5.3升级到7.2。决定使用Swiftmailer并进行设置以发送电子邮件。现在,我需要重现与我相同的电子邮件。当我从旧版本复制PHP代码时,遇到了一些我不理解的语法错误。这是代码:

<?php

// Varibles for the general information section
$company_name = $_REQUEST['company_name'];
$contact = $_REQUEST['contact'];
$delivery_date = $_REQUEST['delivery_date'];
$delivery_time = $_REQUEST['delivery_time'];
$delivery_address = $_REQUEST['delivery_address'];
$phone = $_REQUEST['phone'];
$special_instruction = $_REQUEST['special_instruction'];
$email = $_REQUEST['email'];
$payment = $_REQUEST['payment'];


require_once '[localhosting_service]/phpmailer/vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('********.com', ***, 'ssl'))
->setUsername('******@********.com')
->setPassword('[password]')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['******@*******.com' => '!!! Online Order !!!'])
->setTo(['********@*****.com' => 'Jake'])
->setCc(['********@*****.com' => 'Jay'])

->setSubject('Online order from A COMPANY')

->setBody('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<p>You have an order from a visitor (<strong>'.$company_name.'</strong>) on your website:</p>
<p>&nbsp;</p>
<p><strong>*** General Information ***</strong></p>
<p>
<strong>Company Name:</strong> <span style="color:red;">'.$company_name.'</span><br />';
if (!empty($contact))
  {
    $message->setBody .= '<strong>Contact:</strong> <span style="color:red;">'.$contact.'</span><br />';
  }
$message->setBody .= '<strong>Delivery Date:</strong> <span style="color:red;">'.$delivery_date.'</span><br />
<strong>Delivery Time:</strong> <span style="color:red;">'.$delivery_time.'</span><br />
<strong>Delivery Address:</strong> <span style="color:red;">'.$delivery_address.'</span><br />
<strong>Phone:</strong> <span style="color:red;">'.$phone.'</span><br />';
if (!empty($special_instruction))
  {
    $message->setBody .= '<strong>Special Instruction:</strong> <span style="color:red;">'.$special_instruction.'</span><br />';
  }
$message->setBody .= '<strong>Email:</strong> <span style="color:red;">'.$email.'</span><br />
<strong>Type of Payment:</strong> <span style="color:red;">'.$payment.'</span><br />
</p>
</body>
</html>')
;

// Send the message
$result = $mailer->send($message);

?>
php syntax swiftmailer
1个回答
1
投票

您可以通过设置方法之外的$body电子邮件来实现。定义之前,先进行“构建”,然后将其作为参数传递给setBody()方法。

看这个例子:

// ...
$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<p>You have an order from a visitor (<strong>' . $company_name . '</strong>) on your website:</p>
<p>&nbsp;</p>
<p><strong>*** General Information ***</strong></p>
<p>
<strong>Company Name:</strong> <span style="color:red;">' . $company_name . '</span><br />';
if (!empty($contact)) {
    $body.='<strong>Contact:</strong> <span style="color:red;">' . $contact . '</span><br />';
}
$body.='<strong>Delivery Date:</strong> <span style="color:red;">' . $delivery_date . '</span><br />
<strong>Delivery Time:</strong> <span style="color:red;">' . $delivery_time . '</span><br />
<strong>Delivery Address:</strong> <span style="color:red;">' . $delivery_address . '</span><br />
<strong>Phone:</strong> <span style="color:red;">' . $phone . '</span><br />';
if (!empty($special_instruction)) {
    $body.='<strong>Special Instruction:</strong> <span style="color:red;">' . $special_instruction . '</span><br />';
}
$body.='<strong>Email:</strong> <span style="color:red;">' . $email . '</span><br />
<strong>Type of Payment:</strong> <span style="color:red;">' . $payment . '</span><br />
</p>
</body>
</html>';

$message = (new \Swift_Message('Wonderful Subject'))
    ->setFrom(['******@*******.com' => '!!! Online Order !!!'])
    ->setTo(['********@*****.com' => 'Jake'])
    ->setCc(['********@*****.com' => 'Jay'])
    ->setSubject('Online order from A COMPANY')
    ->setBody($body);

$result = $mailer->send($message);

也就是说,我建议您使用新的Symfony Mailer component。创建消息并将其与不同的邮件程序一起使用非常容易! :)

© www.soinside.com 2019 - 2024. All rights reserved.