I am emailing attachments in php to customers for backup purposes using the below script. However I am using weird file extensions like ".sav, .0, .raw, .duc etc...". When I mail these gmail, and I presume other clients, detects that there is a attachment (shows clip logo) but the attachment isn't shown. When I rename the file to a txt it is sent and shows up as a normal file. I believe I have to sets something with the content type in the mime. .
Does anyone know how to mail these files?
Code:
function sendmailatt($email_address,$htmlemail,$textemail,$subject,$fromname,$from,$attachment,$originalfilename) {
$headers = "From: $fromname<$from>\r\n"; //add From: header
$headers .= "MIME-Version: 1.0\r\n"; //specify MIME version 1.0
$boundary = uniqid("HTMLDEMO"); //unique boundary
$headers .= "Content-Type: multipart/alternative" .
"; boundary = $boundary\r\n\r\n"; //tell e-mail client this e-mail contains//alternate versions
$headers .= "This is a MIME encoded message.\r\n\r\n"; //message to people with clients who don't understand MIME
//plain text version of message
$headers .= "--$boundary\r\n" .
"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($textemail));
//HTML version of message
$headers .= "--$boundary\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($htmlemail));
if($attachment!="") {
//Attachment version of message
$extension=explode (".", $attachment);
$countdot=count($extension)-1;
$extension=$extension[$countdot];
$headers .= "--$boundary\r\n" .
"Content-Type: application/octet-stream; name=$originalfilename\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment\r\n\r\n";
$headers .= chunk_split(base64_encode(file_get_contents('backup/'.$attachment)));
}
if (!mail("$email_address", "$subject", "", $headers)) {
$erreur++; $erreurmsg.='There was a problem sending your email please try again.<br>';
}
}
ps. I wasn't sure witch forum to post in I hope I chose the right one
Use of function
//Ensure this isnt being externally run
if (!isset($codeword)) { echo 'Please stop trying to access places you are not allowed'; die(); }
$erreur=0; //Error Checking Variables
$erreurmsg=''; //Error Checking Variables
if(empty($typeDeFichierSource) || empty($typeDeFichierCible)) { $erreur++; $erreurmsg .= '<br>Random Error.'; }
if($typeDeFichierSource == $typeDeFichierCible) { $erreur++; $erreurmsg .= '<br>Source and target format were in the same format.'; }
$email = trim($emailaddress);
$halves=explode ("@", $email);
//Email Validation
if ($_POST['backup']=="yes") {
if($email=='') { $erreur++; $erreurmsg .= "<br>Please enter a email address"; }
elseif ((strlen($email)<5) or strstr($email," ")){ $erreur++; $erreurmsg .= "<br>Email address: Too short, " . strlen($email) . " characters."; }
elseif (substr_count($email, "@")<>1){ $erreur++; $erreurmsg .= "<br>Either more than or less than one @ character."; }
elseif (strlen($halves[0])<1 or strlen($halves[1])<=3){ $erreur++; $erreurmsg .= "<br>Not enough text characters in this email."; }
elseif (substr($halves[0],0,1)=="." or substr($halves[0],-1,1)=="."){ $erreur++; $erreurmsg .= "<br>There is a dot on the end of the LHS of this address."; }
elseif (substr($halves[1],0,1)=="." or substr($halves[1],-1,1)=="."){ $erreur++; $erreurmsg .= "<br>There is a dot on the end of the RHS of this address."; }
elseif (!strstr($halves[1],".") or strstr($halves[1],"..")){ $erreur++; $erreurmsg .= "<br>This address has the wrong number of dots in it."; }
}
if ($_FILES['fichierDeSauvegarde']['error'] > 0) { $erreur++; $erreurmsg .= '<br>The file upload has failed.'; }
if ($_FILES['fichierDeSauvegarde']['size'] > $tailleMaximale) { $erreur++; $erreurmsg .= '<br>You savegame is too big and cannot be uploaded.'; }
if (!in_array($extensionFichier,$extensionsValides)) { $erreur++; $erreurmsg .= '<br>The save extension is not accepted.'; }
if($erreur==0) $nom=str_replace(' ','',str_replace('.','',microtime()));
$tempfilename='tmp/'.$nom;
if($erreur==0) { $resultat=move_uploaded_file($_FILES['fichierDeSauvegarde']['tmp_name'],$tempfilename);
if ($_POST['backup']=="yes") {
$newfile = 'backup/'.str_replace(" ", "_", $_FILES['fichierDeSauvegarde']['name']);
if (!copy($tempfilename, $newfile)) {
$erreur++; $erreurmsg .= '<br>Failed to create backup.';
}
//If all is well send the backup email
$email = "pegpro@gmail.com";
$textemail = '***Removed***';
$htmlemail = '***Removed***';
$subject = "***Removed***";
$fromname = "***Removed***";
$from = "backup@***Removed***";
//Sendmail Function Layout $email_address,$htmlemail,$textemail,$subject,$fromname,$from,$extension
sendmailatt($email,$htmlemail,$textemail,$subject,$fromname,$from,str_replace(" ", "_", $_FILES['fichierDeSauvegarde']['name']),$_FILES['fichierDeSauvegarde']['name']);
}
}
Thanks,