PHP联系表格-图像上传到临时但不附加到电子邮件
我只是想知道是否有人可以帮助解决一个相当简单的修复(显然不适合我)的图像上传联系表单。该图像确实已上传到临时文件夹,但当我的帐户收到电子邮件时,该图像并未出现。
我认为表格已经差不多了,只是不明白为什么当我收到电子邮件时图像没有附加到电子邮件中 - 猜测问题出在最后 10 行代码中的某个地方。
如果有人能弄清楚出了什么问题,我将非常感激。
<?php
ini_set("sendmail_from", "[email protected]");
ini_set("SMTP", "smtp.myhostingcompany.co.uk");
if($name = filter_var($_POST['name']))
if($address = filter_var($_POST['address']))
if($postcode = filter_var($_POST['postcode']))
if($phone = filter_var($_POST['phone']))
if($email = filter_var($_POST['email']))
if($details = filter_var($_POST['details']))
if($contactby = filter_var($_POST['contactby']))
/* Subject and Email Destinations */
$emailSubject = 'Work Email!';
$webMaster = '[email protected]';
/* Gathering Data Variables */
$nameField = $_POST['name'];
$addressField = $_POST['address'];
$postcodeField = $_POST['postcode'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$detailsField = $_POST['details'];
$contactbyField = $_POST['contactby'];
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB)
$upload_path = './uploads/';
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href=
"' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed
$body = <<<EOD
Name: $name
Address: $address
Postcode: $postcode
Phone: $phone
Email: $email
Details: $details
Contactby: $contactby
EOD;
$headers = "From: $email\r\n";
$headers .= "Contact-type: text/html\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];
$success = mail($webMaster, $attatchment, $body, $headers, "[email protected]");
/* Results rendered as HTML */
$theResults = <<<EOD
SENT SENT SENT
EOD;
echo "$theResults"
?>
I was just wondering if anyone could help out with what should be a fairly simple fix (obviously not for me) for a contact form with image upload. The image DOES get uploaded to the temporary folder, but doesn't appear when I receive the email into my account.
I think the form is nearly there, just can't figure out why the image is not attatching to the email when i receive it - guessing the problem is somewhere in the last 10 lines of code.
I would really appreciate if someone could figure what's going wrong.
<?php
ini_set("sendmail_from", "[email protected]");
ini_set("SMTP", "smtp.myhostingcompany.co.uk");
if($name = filter_var($_POST['name']))
if($address = filter_var($_POST['address']))
if($postcode = filter_var($_POST['postcode']))
if($phone = filter_var($_POST['phone']))
if($email = filter_var($_POST['email']))
if($details = filter_var($_POST['details']))
if($contactby = filter_var($_POST['contactby']))
/* Subject and Email Destinations */
$emailSubject = 'Work Email!';
$webMaster = '[email protected]';
/* Gathering Data Variables */
$nameField = $_POST['name'];
$addressField = $_POST['address'];
$postcodeField = $_POST['postcode'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$detailsField = $_POST['details'];
$contactbyField = $_POST['contactby'];
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB)
$upload_path = './uploads/';
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href=
"' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed
$body = <<<EOD
Name: $name
Address: $address
Postcode: $postcode
Phone: $phone
Email: $email
Details: $details
Contactby: $contactby
EOD;
$headers = "From: $email\r\n";
$headers .= "Contact-type: text/html\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];
$success = mail($webMaster, $attatchment, $body, $headers, "[email protected]");
/* Results rendered as HTML */
$theResults = <<<EOD
SENT SENT SENT
EOD;
echo "$theResults"
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的邮件标题错误,您需要使用 MIME 来定义不同的内容类型(电子邮件正文和附件)。
我在某处有一些用于通过电子邮件发送上传的代码,如果在大约 10-20 分钟内没有回答您,我会尝试为您找到它。
祝你好运!
我成功地挖出了代码!这不是最干净的代码,但它可以完成工作。您将需要进行一些调整,因为这最初是为了通过电子邮件发送 CSV 文件而编写的,但我已尽我所能为您进行了评论。
Your mail headers are wrong, you need to use MIME to define the different content types (The body of the email and the attachment).
I have some code somewhere that I use for emailing uploads, if this isn't answered for you in about 10-20 minutes I'll try and find it for you.
Good luck!
I managed to dig up the code! It's not the cleanest code ever but it gets the job done. There will be a few tweaks you will have to do as this was originally written to email CSV files, but I've commented it all as much as I can for you.
就像那些人说的,你的内容类型不正确,你有:
它应该是
Like the guys said your content type is incorrect and you have:
It should be
您遇到三个不同的错误:
mail
函数的参数不正确。file_get_contents
。SitePoint 有一篇关于 PHP 中的高级电子邮件文章的深入文章,您可能会发现它有益。
You have three distinct errors:
mail
function are incorrect.file_get_contents
.SitePoint has an in-depth article on Advanced email in PHP Article that you may find beneficial.