【发布时间】:2014-09-29 10:18:32
【问题描述】:
我正在尝试使用 swftmailer - 从 html 网页上的表单上传文件(jpg、ppt、pdf、word 等,最大上传例如:6MB), - 通过电子邮件发送表单的内容通过 swiftmailer 附加文件。
我的表单已创建,我正在使用 $_POST 来捕获thankyou.php 页面中的字段值。电子邮件正在发送,但我无法获取要附加的文件!
上传/附加文件需要是一个选项,我在网上找到了一个资源,它只在文件上传时才发送信息,但情况并非总是如此,所以我转向了 swiftmailer,它看起来很棒,但在文档/示例中,它也指定了上传附件的路径,我只想能够读取文件(不指定路径)并将其作为附件与其他表单字段一起发送到给定的电子邮件地址, 姓名电子邮件、电话和消息。
任何帮助将不胜感激。
我的 HTML (contact.html)
<div id="form-main">
<div id="form-div">
<form class="form" method="post" action="thank-you.php" enctype="multipart/form-data">
<p class="name">
<input name="fieldFormName" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
</p>
<p class="email">
<input name="fieldFormEmail" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
</p>
<p class="phone">
<input name="fieldFormPhone" type="number" class="validate[required,custom[onlyLetter],length[0,20]] feedback-input" placeholder="Phone" id="phone" />
</p>
<p class="text">
<textarea name="fieldDescription" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
</p>
<p class="upload">
<small>Send us a Drawing or file</small>
<input name="fieldAttachment" id="attachment" class="feedback-input" type="file">
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
</div>
</div>
我的 PHP 代码:
<?php
require_once 'lib/swift_required.php';
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$fromPhone = $_POST['fieldFormPhone'];
$fromMessage = $_POST['fieldDescription'];
$fromAttachment = $_POST['fieldAttachment'];
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"sender@domain.com" => "Sender Name"
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody(
"From: " . $fromName . "\n" .
"Email: " . $fromEmail . "\n" .
"Phone: " . $fromPhone . "\n" .
"Message: " . $fromMessage . "\n"
);
$message->setFrom("$fromEmail", "$fromName");
// *** This is the part I am struggling to understand *** //
$message->attach(Swift_Attachment::newInstance('$fromAttachment'));
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
?>
提前谢谢你
【问题讨论】:
标签: php forms file-upload email-attachments swiftmailer