PHP联系表格-图像上传到临时但不附加到电子邮件

发布于 2024-12-06 23:30:57 字数 2856 浏览 1 评论 0原文

我只是想知道是否有人可以帮助解决一个相当简单的修复(显然不适合我)的图像上传联系表单。该图像确实已上传到临时文件夹,但当我的帐户收到电子邮件时,该图像并未出现。

我认为表格已经差不多了,只是不明白为什么当我收到电子邮件时图像没有附加到电子邮件中 ​​- 猜测问题出在最后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千柳 2024-12-13 23:30:57

您的邮件标题错误,您需要使用 MIME 来定义不同的内容类型(电子邮件正文和附件)。

我在某处有一些用于通过电子邮件发送上传的代码,如果在大约 10-20 分钟内没有回答您,我会尝试为您找到它。

祝你好运!

我成功地挖出了代码!这不是最干净的代码,但它可以完成工作。您将需要进行一些调整,因为这最初是为了通过电子邮件发送 CSV 文件而编写的,但我已尽我所能为您进行了评论。

//The recipient(s) who will receive this email.
$mail_recipients = '[email protected]';

//This is the subject line of the email.
$mail_subject .= 'A new CVS has arrived!';

//This is the emails body.
$message .= 'A new CVS file has been generated and sent to you, please find it attached to this email. <br />';

//This is the full path to the file you want to attach.
$full_path = '/example/path/file.csv';

//The file type of the attachment.
$file_type = 'text/csv';

//Create a file handle to open the file.
$file = fopen($full_path,'rb'); 

//Read all of the files contents into a variable.
$data = fread($file, filesize($full_path)); 

//encode and split the contents.
$data = chunk_split(base64_encode($data)); 

//Close the file, we don't need it anymore!
fclose($file); 

//Create a unique boundary
$new_boundary = md5(time());

//This is your basic header information such as who the email is from and the date it was sent.
$mail_header .= 'From: CSV Admin <[email protected]>' . "\r\n";
$mail_header .= 'Date: ' . date('r') . "\r\n";

//This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
$mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "\r\n\r\n";
$mail_header .= 'MIME-Version: 1.0' . "\r\n";
$mail_header .= 'This is a multi-part message in MIME format' . "\r\n";
$mail_header .= '--' . $new_boundary . "\r\n";
$mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "\r\n";
$mail_header .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
$mail_header .= $message . "\r\n\r\n";

//This part of the header is for the attachment and includes the contents of the file, the files name and other information.
$mail_header .= '--' . $new_boundary . "\r\n";
$mail_header .= 'Content-Type: ' . $file_type . '; name="' . $file_name . '"' . "\r\n";
$mail_header .= 'Content-Transfer-Encoding: base64' . "\r\n";
$mail_header .= 'Content-Disposition: attachment; filename="' . $file_name . '"' . "\r\n\r\n";
$mail_header .= $data . "\r\n\r\n";

//This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
$mail_header .= '--' . $new_boundary  . '--' . "\r\n";

//This mails out all of the above.
mail($mail_recipients, $mail_subject, '', $mail_header);

//And we should be done!

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.

//The recipient(s) who will receive this email.
$mail_recipients = '[email protected]';

//This is the subject line of the email.
$mail_subject .= 'A new CVS has arrived!';

//This is the emails body.
$message .= 'A new CVS file has been generated and sent to you, please find it attached to this email. <br />';

//This is the full path to the file you want to attach.
$full_path = '/example/path/file.csv';

//The file type of the attachment.
$file_type = 'text/csv';

//Create a file handle to open the file.
$file = fopen($full_path,'rb'); 

//Read all of the files contents into a variable.
$data = fread($file, filesize($full_path)); 

//encode and split the contents.
$data = chunk_split(base64_encode($data)); 

//Close the file, we don't need it anymore!
fclose($file); 

//Create a unique boundary
$new_boundary = md5(time());

//This is your basic header information such as who the email is from and the date it was sent.
$mail_header .= 'From: CSV Admin <[email protected]>' . "\r\n";
$mail_header .= 'Date: ' . date('r') . "\r\n";

//This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
$mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "\r\n\r\n";
$mail_header .= 'MIME-Version: 1.0' . "\r\n";
$mail_header .= 'This is a multi-part message in MIME format' . "\r\n";
$mail_header .= '--' . $new_boundary . "\r\n";
$mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "\r\n";
$mail_header .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
$mail_header .= $message . "\r\n\r\n";

//This part of the header is for the attachment and includes the contents of the file, the files name and other information.
$mail_header .= '--' . $new_boundary . "\r\n";
$mail_header .= 'Content-Type: ' . $file_type . '; name="' . $file_name . '"' . "\r\n";
$mail_header .= 'Content-Transfer-Encoding: base64' . "\r\n";
$mail_header .= 'Content-Disposition: attachment; filename="' . $file_name . '"' . "\r\n\r\n";
$mail_header .= $data . "\r\n\r\n";

//This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
$mail_header .= '--' . $new_boundary  . '--' . "\r\n";

//This mails out all of the above.
mail($mail_recipients, $mail_subject, '', $mail_header);

//And we should be done!
归途 2024-12-13 23:30:57

就像那些人说的,你的内容类型不正确,你有:

$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];

它应该是

$tmp_file = $_FILES['userfile']['tmp_name'];
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));

Like the guys said your content type is incorrect and you have:

$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];

It should be

$tmp_file = $_FILES['userfile']['tmp_name'];
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
青春有你 2024-12-13 23:30:57

您遇到三个不同的错误:

  1. 多部分电子邮件的标头有点复杂。
  2. mail 函数的参数不正确。
  3. 您正在将未定义的变量传递给 file_get_contents
$mime_boundary = md5(time());

$headers = "From: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= "--".$mime_boundary."--\r\n";

$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$mime_boundary."--\r\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$body\r\n";
$message .= "--".$mime_boundary."--\r\n";

$message .= chunk_split(base64_encode(file_get_contents($_FILES['userfile']['tmp_name'])));

mail($webMaster, 'email subject text', $message, $headers);

SitePoint 有一篇关于 PHP 中的高级电子邮件文章的深入文章,您可能会发现它有益。

You have three distinct errors:

  1. The headers for multipart email are a bit more complex.
  2. The parameters to the mail function are incorrect.
  3. You’re passing an undefined variable to file_get_contents.
$mime_boundary = md5(time());

$headers = "From: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= "--".$mime_boundary."--\r\n";

$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$mime_boundary."--\r\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$body\r\n";
$message .= "--".$mime_boundary."--\r\n";

$message .= chunk_split(base64_encode(file_get_contents($_FILES['userfile']['tmp_name'])));

mail($webMaster, 'email subject text', $message, $headers);

SitePoint has an in-depth article on Advanced email in PHP Article that you may find beneficial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文