Forum Moderators: coopster
I just don't know enough about how it's supposed to work to figure it out.
I've tried just adding it in but it appends onto my html version of the message.
Anyone know how the text/plain could be added into this code?
Thanks!
<?php
#$fileatt = "test.zip"; // Path to the file
#$fileatt_type = "application/octet-stream"; // File Type
#$fileatt_name = "mytest.zip"; // Filename that will be used for the file as the attachment$email_from = "me@me.com"; // Who the email is from
$email_subject = "testing the attachment stuff"; // The Subject of the email
$email_message = "<h1>Hello</h1><b>this is the email message</b> right here"; // Message that the email has in it
$email_to = "me@me.com"; // Who the email is too
$headers = "From: ".$email_from;
// does the top part do the plain text version?
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
/********************************************** First File ********************************************/
$fileatt = "test.zip"; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "mytesting.zip"; // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
/********************************************** Second File ********************************************/
$fileatt = "test.zip"; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "test.zip"; // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
/********************************************** End of File Config ********************************************/
// To add more files just copy the file section again, but make sure they are all one after the other! If they are not it will not work!
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
I don't think you can do both, it has to be one or the other.
Sure you can. Use
Content-Type: multipart/alternative;, then Content-Type: text/plain; for the text part and Content-Type: text/html; And i think you can add the attachment for both alternatives by nesting
Content-Type: multipart/mixed; within each section, but i haven't tried that. Search for "email multipart plain-text" for a good article, or see wikipedia's MIME page.
$body = "
<!--
Hello, you are seeing the text only version of this email. You may ignore the html code below the line.
(message body)
--------------------------
-->
<p>message body</p>
";
Like I said, down and dirty . . . . but it works.
Once I do that I get all the text of everything including the boundaries etc...
It's like the raw text of the message.
So I assume I'm just not nesting things correctly.
I looked through the wikipedia stuff and didn't find any examples though.
Is there any good example of how you can use both plaintext and html in a message with an attachment via php?
With the above script, I can make it be either plain or html and has the attachment... now I would just like to add in the alternate so that it does plain AND html AND attachment.
Any help is appreciated.
Thanks.
$emailContent['plain'] = "abc\n\n";
$emailContent['html'] = '<strong>abc</strong>'."\n";
$emailAttachment['filepath'] = '1.jpg';
$emailAttachment['filename'] = 'testattach.jpg';
$emailAttachment['type'] = 'image/jpeg';emailSend($to, $subject, $emailContent, $emailAttachment);
function emailSend($to, $subject, $content, $attachment =null, $from = null, $cc = null) {
$ob = "_OuterBoundary_000";
$ab = "_AttachBoundary_000";$message = '';
$headers = "MIME-Version: 1.0\r\n";
if($from != '') {
$headers .= "From: ".$from."\n";
$headers .= "Reply-To: ".$from."\n";
}
$headers .= "To: ".$to."\n";
if(isset($cc) && !empty($cc)) {
$headers .= "Cc: ".$cc."\n";
}$headers .= 'Content-Transfer-Encoding: 7bit'."\n";
if(isset($attachment)) {
$headers .= 'Content-Type: multipart/mixed; boundary='.$ab."\n";
$message .= "\n\n--".$ab."\n";
if(!is_array($content)) {
$message .= 'Content-Type: text/plain; charset=ISO-8859-1'."\n";} else {
$message .= 'Content-Type: multipart/alternative; boundary='.$ob."\n";
}} elseif(!is_array($content)) {
$headers .= 'Content-Type: text/plain; charset=ISO-8859-1'."\n";} else {
$headers .= 'Content-Type: multipart/alternative; boundary='.$ob."\n";}
if(!is_array($content)) {
$message = $content;} else {
foreach($content as $contentType => $contentContent) {
$message .= "\n\n--".$ob."\n";
$message .= "Content-Type: text/$contentType; charset=iso-8859-1\n";
$message .= 'Content-Transfer-Encoding: 7bit'."\n\n";
$message .= $contentContent;
}
$message .= "\n--".$ob."--\n";
}if(isset($attachment)) {
$file = fopen($attachment['filepath'],'rb');
$data = fread($file,filesize($attachment['filepath']));
fclose($file);
$data = chunk_split(base64_encode($data));$message .= "\n\n--".$ab."\n";
$message .= 'Content-Transfer-Encoding: base64'."\n";
$message .= 'Content-Type: '.$attachment['type'].'; name="'.$attachment['filename'].'"'."\n";
$message .= "\n".$data;
$message .= "\n\n--".$ab."\n";
}// echo "to=$to, subject=$subject, from=".@$from.", cc=".@$cc."\n";
// echo $headers."\n";
// echo "content=".$message."\n";return mail($to, $subject, $message, $headers);
}
Hope this helps.