我只是想知道是否有人可以为图片上传的联系表单提供一个相当简单的修复(显然不适合我)。图像将上传到临时文件夹,但在我收到电子邮件后不会显示。
我认为表格几乎就在那里,只是无法弄清楚为什么当我收到它时图像没有附着到电子邮件 - 猜测问题是在最后10行代码中的某个地方。
如果有人能够弄清楚出了什么问题,我真的很感激。
<?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\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, "-fdarren@mywebsite.co.uk");
/* Results rendered as HTML */
$theResults = <<<EOD
SENT SENT SENT
EOD;
echo "$theResults"
?>
答案 0 :(得分:2)
您的邮件标题错误,您需要使用MIME来定义不同的内容类型(电子邮件正文和附件)。
我在某处使用某些代码来发送电子邮件,如果在大约10-20分钟内没有为您解答,我会尝试为您找到它。
祝你好运!我设法挖掘代码!这不是最干净的代码,但它完成了工作。您将不得不进行一些调整,因为这最初是写入电子邮件CSV文件,但我已经尽可能地为您评论了所有内容。
//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>' . "\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!
答案 1 :(得分:0)
您有三个不同的错误:
mail
功能的参数不正确。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有一篇关于Advanced email in PHP Article的深入文章,您可能会发现它很有用。
答案 2 :(得分:0)
就像那些人说你的内容类型不正确而且你有:
$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)));