dtf76989 2011-09-28 12:45
浏览 32

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

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", "darren@mywebsite.co.uk");
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 = 'darren@mywebsite.co.uk';

/* 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
";
$headers .= "Contact-type: text/html
";
$attachment = chunk_split(base64_encode(file_get_contents($temp_file)));
$tmp_file = $_FILES['userfile']['tmp_name'];
$success = mail($webMaster, $attatchment, $body, $headers, "-fdarren@mywebsite.co.uk");

/* Results rendered as HTML */

$theResults = <<<EOD
SENT SENT SENT
EOD;
echo "$theResults"
?>
  • 写回答

3条回答 默认 最新

  • douyazi1129 2011-09-28 12:54
    关注

    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 = 'example@email.com';
    
    //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 <noreply@website.co.uk>' . "
    ";
    $mail_header .= 'Date: ' . date('r') . "
    ";
    
    //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 . '"' . "
    
    ";
    $mail_header .= 'MIME-Version: 1.0' . "
    ";
    $mail_header .= 'This is a multi-part message in MIME format' . "
    ";
    $mail_header .= '--' . $new_boundary . "
    ";
    $mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "
    ";
    $mail_header .= 'Content-Transfer-Encoding: 7bit' . "
    
    ";
    $mail_header .= $message . "
    
    ";
    
    //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 . "
    ";
    $mail_header .= 'Content-Type: ' . $file_type . '; name="' . $file_name . '"' . "
    ";
    $mail_header .= 'Content-Transfer-Encoding: base64' . "
    ";
    $mail_header .= 'Content-Disposition: attachment; filename="' . $file_name . '"' . "
    
    ";
    $mail_header .= $data . "
    
    ";
    
    //This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
    $mail_header .= '--' . $new_boundary  . '--' . "
    ";
    
    //This mails out all of the above.
    mail($mail_recipients, $mail_subject, '', $mail_header);
    
    //And we should be done!
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题