【发布时间】:2015-10-01 12:46:39
【问题描述】:
我有一个表单可以通过 php 成功将上传的图片作为附件发送,但我无法让它发送多个附件。
html:
<input id="file-3" type="file" name="image" multiple="true" required>
附加单个文件的php:
if (isset($email) && isset($name)) {
$to = "email@email.com"; /* <= Change this Email ID to yours */
$subject = "$name sent you a message"; /* <= Change the Subject If you want */
$random_hash = md5(date('r', time()));
$headers = "From: $email\r\nReply-To: $email";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = null;
if ( isset($_FILES["photo"]) ) {
$attachment = chunk_split(base64_encode(file_get_contents($_FILES["photo"]["tmp_name"])));
}
$message = "--PHP-mixed-" . $random_hash . "\r\n";
$message .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\r\n\r\n";
$message .= "--PHP-alt-" . $random_hash . "\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "Name: " . $name . "\r\nEmail: " . $email . "Message: " . $textarea . "\r\n\r\n";
$message .= "--PHP-alt-" . $random_hash . "\r\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\t\nContent-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "";
if ( $attachment != null ) {
$message .= "--PHP-mixed-" . $random_hash . "\r\n";
$message .= "Content-Type: " . $_FILES["photo"]["type"] . "; name=\"" . $_FILES["photo"]["name"] . "\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "\r\n\r\n--PHP-mixed-" . $random_hash . "\r\n";
}
我有一个使用 ajax 提交表单的 JS 文件,但我认为它与此问题无关。
感谢您的帮助!我是 php 新手。
【问题讨论】:
-
您是否考虑过使用像SwiftMailer 这样的第三方库?
-
@sandeepsure 这一切都取决于 OP 通过“使用 ajax 提交”的含义。 JS 绝对可以提交带有文件输入的表单。看看 JS 代码以确保那里没有任何奇怪的东西会很方便。
标签: javascript php ajax forms email-attachments