Jeg har et problem med en mail form hvor jeg vil vedhæftet tre filer til en mail, php koden skulle efter sigende virke men jeg er i tvivl om kommunikationen mellem form og php er korrekt...
håber der er nogen der kan hjælpe
email.php:
<?php
$attachments[] = 'report.xls'; // don't forget to add the path
$attachments[] = 'title.bmp'; // don't forget to add the path
$attachments[] = 'disclaimer.txt'; // don't forget to add the path
$sendto = 'myAddress@hotmail.com'; // use a conventional mail server
$sendtoCC = 'otheraddress@gmail.com';
$sendtoBCC = 'thirdaddress@msn.com';
$message = 'This is the text body';
$subject = 'This is the subject';
$sendfrom = 'you@yourDomain.com'; // How about adding the sender
$headers = 'Cc: '.$sendtoCC."\\r\\n".'Bcc: '.$sendtoBCC."\\r\\n".'From: '.$sendfrom;
$sent = send_mime_mail($sendto, $subject, $message, $headers, FALSE, $attachments);
if($sent) print 'Sent ok';
else print 'Error sending';
function send_mime_mail($recipient, $subject, $message, $headers = NULL, $html = NULL, $attachments = NULL, $css = NULL)
{
if (!function_exists('mime_content_type')){
function mime_content_type($file){
if(!is_readable($file)) return false;
@$size = getimagesize($file);
if(!empty($size['mime'])){
return($size['mime']);
}else{
$extensions = array('doc' => 'application/msword', 'html'=> 'text/html', 'htm' => 'text/html',
'pdf' => 'application/pdf', 'ppt' => 'application/vnd.ms-powerpoint', 'rtf' => 'text/rtf',
'xls' => 'application/vnd.ms-excel', 'zip' => 'application/zip');
$keys = array_keys($extensions);
$parts = array_reverse(explode('.', $file));
$extension = $parts['0'];
if(in_array($extension, $keys)) return $extensions[$extension];
$data = file_get_contents($filename);
$bad = false;
for($x = 0, $y = strlen($data); !$bad && $x < $y; $x++)
{
$bad = (ord($data{$x}) > 127);
}
if(!$bad) return ('text/plain');
return('application/octet-stream');
}
}
}
function convert_links($string, $protocol = TRUE)
{
$search[] = '/<a\\s[^>]*href=[\\'"]mailto:([^\\'"]+)[\\'"][^>]*>([^<]+)<\\/a>/i';
$search[] = '/<a\\s[^>]*href=mailto:([^\\s>]+)[^>]*>([^<]+)<\\/a>/i';
$search[] = '/<a\\s[^>]*href=[\\'"](http:\\/\\/[^\\'"]+)[\\'"][^>]*>([^<]+)<\\/a>/i';
$search[] = '/<a\\s[^>]*href=(http:\\/\\/[^\\s>]+)[^>]*>([^<]+)<\\/a>/i';
$replace = '\\2 (\\1)';
return(strip_tags(preg_replace($search, $replace, $string)));
}
function html_to_plain($string)
{
$string = preg_replace("(\\r\\n|\\n|\\r)", ' ', $string);
$string = preg_replace('/<p>/i', "\\n\\n", $string);
$string = preg_replace('/(<br>|<br \\/>)/i', "\\n", $string);
return (convert_links($string));
}
if(!isset($css)) $css = '';
// Generate a boundary string
$mime_boundary = "mime_boundry_multipart/mixed".md5(time())."x";
$alternative_mime_boundary = "mime_boundry_multipart/alternative_".md5(time())."x";
if(empty($headers)) $headers = '';
$headers .= "\\nMIME-Version: 1.0\\n";
if(count($attachments) > 0){
$headers .= "Content-Type: multipart/mixed;\\n" .
" boundary=\\"{$mime_boundary}\\"\\n\\n";
if(!empty($html) and $html == TRUE){
$content = "This is a multi-part message in MIME format.\\n\\n".
"--{$mime_boundary}\\n".
"Content-Type: multipart/alternative;\\n".
" boundary=\\"{$alternative_mime_boundary}\\"\\n\\n".
"--{$alternative_mime_boundary}\\n".
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
html_to_plain($message)."\\n\\n".
"--{$alternative_mime_boundary}\\n".
"Content-Type: text/html; charset=iso-8859-1\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
"<!DOCTYPE html PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\">\\n".
"<html>\\n".
"<head>\\n".
" <style type=\\"text/javascript\\">\\n".
" $css \\n".
" </style>\\n".
" <meta content=\\"text/html;charset=iso-8859-1\\" http-equiv=\\"Content-Type\\">\\n".
" <title></title>\\n".
"</head>\\n".
"<body>\\n".
$message."\\n".
"</body>\\n".
"</html>\\n\\n".
"--{$alternative_mime_boundary}--\\n";
}else{
$content = "This is a multi-part message in MIME format.\\n\\n".
"--{$mime_boundary}\\n".
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n" .
"Content-Transfer-Encoding: 7bit\\n\\n" .
convert_links($message)."\\n\\n";
}
// Deal with attachments array
if(count($attachments) > 0){
foreach($attachments as $key => $value){
$mime_type = mime_content_type($value);
if(is_file($value) and is_readable($value) ){
if(ereg('^image', $mime_type)){
$disposition = 'inline';
}else{
$disposition = 'attachment';
}
$filesize = filesize($value);
$file = fopen($value,'rb');
$data = fread($file,$filesize);
fclose($file);
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
$value = pathinfo($value);
$content .= "--{$mime_boundary}\\n" .
"Content-Type: $mime_type;\\n" .
" name=\\"{$value['basename']}\\"\\n" .
"Content-Transfer-Encoding: base64\\n" .
"Content-Disposition: $disposition;\\n" .
" filename=\\"{$value['basename']}\\"\\n\\n" .
$data . "\\n";
}
}
}
$content .= "--{$mime_boundary}--\\n";
}elseif(!empty($html) and $html == TRUE){
$headers .= "Content-Type: multipart/alternative;\\n".
" boundary=\\"{$alternative_mime_boundary}\\"\\n\\n";
$content = "--{$alternative_mime_boundary}\\n".
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
html_to_plain($message)."\\n\\n".
"--{$alternative_mime_boundary}\\n".
"Content-Type: text/html; charset=iso-8859-1\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
"<!DOCTYPE html PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\">\\n".
"<html>\\n".
"<head>\\n".
" <style type=\\"text/javascript\\">\\n".
" $css \\n".
" </style>\\n".
" <meta content=\\"text/html;charset=iso-8859-1\\" http-equiv=\\"Content-Type\\">\\n".
" <title></title>\\n".
"</head>\\n".
"<body>\\n".
$message."\\n".
"</body>\\n".
"</html>\\n\\n".
"--{$alternative_mime_boundary}--\\n";
}else{
$headers .= "Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n" .
"Content-Transfer-Encoding: 7bit\\n\\n";
$content = convert_links($message)."\\n";
}
if(@mail($recipient, $subject, $content, $headers)) return TRUE;
return FALSE;
}
?>
form.php:
<?php
$attachments[] = 'report.xls'; // don't forget to add the path
$attachments[] = 'title.bmp'; // don't forget to add the path
$attachments[] = 'disclaimer.txt'; // don't forget to add the path
$sendto = 'myAddress@hotmail.com'; // use a conventional mail server
$sendtoCC = 'otheraddress@gmail.com';
$sendtoBCC = 'thirdaddress@msn.com';
$message = 'This is the text body';
$subject = 'This is the subject';
$sendfrom = 'you@yourDomain.com'; // How about adding the sender
$headers = 'Cc: '.$sendtoCC."\\r\\n".'Bcc: '.$sendtoBCC."\\r\\n".'From: '.$sendfrom;
$sent = send_mime_mail($sendto, $subject, $message, $headers, FALSE, $attachments);
if($sent) print 'Sent ok';
else print 'Error sending';
function send_mime_mail($recipient, $subject, $message, $headers = NULL, $html = NULL, $attachments = NULL, $css = NULL)
{
if (!function_exists('mime_content_type')){
function mime_content_type($file){
if(!is_readable($file)) return false;
@$size = getimagesize($file);
if(!empty($size['mime'])){
return($size['mime']);
}else{
$extensions = array('doc' => 'application/msword', 'html'=> 'text/html', 'htm' => 'text/html',
'pdf' => 'application/pdf', 'ppt' => 'application/vnd.ms-powerpoint', 'rtf' => 'text/rtf',
'xls' => 'application/vnd.ms-excel', 'zip' => 'application/zip');
$keys = array_keys($extensions);
$parts = array_reverse(explode('.', $file));
$extension = $parts['0'];
if(in_array($extension, $keys)) return $extensions[$extension];
$data = file_get_contents($filename);
$bad = false;
for($x = 0, $y = strlen($data); !$bad && $x < $y; $x++)
{
$bad = (ord($data{$x}) > 127);
}
if(!$bad) return ('text/plain');
return('application/octet-stream');
}
}
}
function convert_links($string, $protocol = TRUE)
{
$search[] = '/<a\\s[^>]*href=[\\'"]mailto:([^\\'"]+)[\\'"][^>]*>([^<]+)<\\/a>/i';
$search[] = '/<a\\s[^>]*href=mailto:([^\\s>]+)[^>]*>([^<]+)<\\/a>/i';
$search[] = '/<a\\s[^>]*href=[\\'"](http:\\/\\/[^\\'"]+)[\\'"][^>]*>([^<]+)<\\/a>/i';
$search[] = '/<a\\s[^>]*href=(http:\\/\\/[^\\s>]+)[^>]*>([^<]+)<\\/a>/i';
$replace = '\\2 (\\1)';
return(strip_tags(preg_replace($search, $replace, $string)));
}
function html_to_plain($string)
{
$string = preg_replace("(\\r\\n|\\n|\\r)", ' ', $string);
$string = preg_replace('/<p>/i', "\\n\\n", $string);
$string = preg_replace('/(<br>|<br \\/>)/i', "\\n", $string);
return (convert_links($string));
}
if(!isset($css)) $css = '';
// Generate a boundary string
$mime_boundary = "mime_boundry_multipart/mixed".md5(time())."x";
$alternative_mime_boundary = "mime_boundry_multipart/alternative_".md5(time())."x";
if(empty($headers)) $headers = '';
$headers .= "\\nMIME-Version: 1.0\\n";
if(count($attachments) > 0){
$headers .= "Content-Type: multipart/mixed;\\n" .
" boundary=\\"{$mime_boundary}\\"\\n\\n";
if(!empty($html) and $html == TRUE){
$content = "This is a multi-part message in MIME format.\\n\\n".
"--{$mime_boundary}\\n".
"Content-Type: multipart/alternative;\\n".
" boundary=\\"{$alternative_mime_boundary}\\"\\n\\n".
"--{$alternative_mime_boundary}\\n".
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
html_to_plain($message)."\\n\\n".
"--{$alternative_mime_boundary}\\n".
"Content-Type: text/html; charset=iso-8859-1\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
"<!DOCTYPE html PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\">\\n".
"<html>\\n".
"<head>\\n".
" <style type=\\"text/javascript\\">\\n".
" $css \\n".
" </style>\\n".
" <meta content=\\"text/html;charset=iso-8859-1\\" http-equiv=\\"Content-Type\\">\\n".
" <title></title>\\n".
"</head>\\n".
"<body>\\n".
$message."\\n".
"</body>\\n".
"</html>\\n\\n".
"--{$alternative_mime_boundary}--\\n";
}else{
$content = "This is a multi-part message in MIME format.\\n\\n".
"--{$mime_boundary}\\n".
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n" .
"Content-Transfer-Encoding: 7bit\\n\\n" .
convert_links($message)."\\n\\n";
}
// Deal with attachments array
if(count($attachments) > 0){
foreach($attachments as $key => $value){
$mime_type = mime_content_type($value);
if(is_file($value) and is_readable($value) ){
if(ereg('^image', $mime_type)){
$disposition = 'inline';
}else{
$disposition = 'attachment';
}
$filesize = filesize($value);
$file = fopen($value,'rb');
$data = fread($file,$filesize);
fclose($file);
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
$value = pathinfo($value);
$content .= "--{$mime_boundary}\\n" .
"Content-Type: $mime_type;\\n" .
" name=\\"{$value['basename']}\\"\\n" .
"Content-Transfer-Encoding: base64\\n" .
"Content-Disposition: $disposition;\\n" .
" filename=\\"{$value['basename']}\\"\\n\\n" .
$data . "\\n";
}
}
}
$content .= "--{$mime_boundary}--\\n";
}elseif(!empty($html) and $html == TRUE){
$headers .= "Content-Type: multipart/alternative;\\n".
" boundary=\\"{$alternative_mime_boundary}\\"\\n\\n";
$content = "--{$alternative_mime_boundary}\\n".
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
html_to_plain($message)."\\n\\n".
"--{$alternative_mime_boundary}\\n".
"Content-Type: text/html; charset=iso-8859-1\\n".
"Content-Transfer-Encoding: 7bit\\n\\n".
"<!DOCTYPE html PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\">\\n".
"<html>\\n".
"<head>\\n".
" <style type=\\"text/javascript\\">\\n".
" $css \\n".
" </style>\\n".
" <meta content=\\"text/html;charset=iso-8859-1\\" http-equiv=\\"Content-Type\\">\\n".
" <title></title>\\n".
"</head>\\n".
"<body>\\n".
$message."\\n".
"</body>\\n".
"</html>\\n\\n".
"--{$alternative_mime_boundary}--\\n";
}else{
$headers .= "Content-Type: text/plain; charset=iso-8859-1; format=flowed\\n" .
"Content-Transfer-Encoding: 7bit\\n\\n";
$content = convert_links($message)."\\n";
}
if(@mail($recipient, $subject, $content, $headers)) return TRUE;
return FALSE;
}
?>