我正在尝试修改脚本以发送带有附件的电子邮件。它现在可以工作,但所有字段都是必需的,我想知道我缺少什么来制作它,所以这些字段不是必需的。
<?php
if (($_POST)) {
$success = $error = false;
$post = new stdClass;
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
$dir = dirname(__FILE__);
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport());
$message = Swift_Message::newInstance()
->setSubject('Imperial Order') // Message subject
->setTo(array($post->email => $post->name, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])->setFilename($_FILES['attachment']['name']));
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
}
?>
我相信它与 foreach 有关,但如果我尝试将其取出,它会破坏整个代码。如果有人可以提供帮助,那就太好了。我进行验证的原因是因为我想在客户端而不是服务器端进行验证。