【发布时间】:2016-05-18 09:18:13
【问题描述】:
我是新来的,但是在浏览了很多页面之后,我想出了一个可以实际使用的表单,并将附件发送到我的电子邮件中。但是,如果在填写表格时没有附加附件,我会得到这个而不是感谢页面。
警告:file_get_contents():第 21 行 /home/advem/public_html/careers.php 中的文件名不能为空
警告:无法修改标头信息 - 第 61 行 /home/advem/public_html/careers.php 中的标头(输出开始于 /home/advem/public_html/careers.php:21)
但是,当不使用附件时,电子邮件仍会发送给我。 非常感谢任何帮助。
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
if(isset($_POST['submit']))
{
//Deal with the email
$to = 'email@testdesign.net';
$subject = 'New Proof Request';
$visitor_email = $_POST['email'];
$message = strip_tags($_POST['message']);
$sku = $_POST['sku'];
$phone = $_POST['phone'];
$name = $_POST['name'];
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
$headers = "From: email@testdesign.net\r\nReply-To: $visitor_email";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$name
$sku
$message
$phone
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
//done. redirect to thank-you page.
header('Location: thank-you-proof-request');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
}
?>
我也尝试了以下链接的答案,但也没有运气。
PHP - sending email with attachment does not show message content
【问题讨论】:
-
在使用
file_get_contents函数之前,只需添加一个检查以查看$_FILES是否包含任何内容。至于已经发送的header,见stackoverflow.com/questions/8028957/… -
Qirel .....你能告诉我怎么写以及放在哪里吗?到目前为止,我所拥有的一切都只是从 Stackoverflow 上的不同问题中复制和粘贴。恐怕我会搞砸。
标签: php email email-attachments