Forum Moderators: coopster

Message Too Old, No Replies

sending plain text and html email messages

sending plain text and html email messages

         

newb2seo

12:20 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Hi. I found this code (below) that does a good job of sending email with attachments. The code is good as is, no errors, does what it says, but I would like to add the area for plain text text/plain in addition to text/html.

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!");
}
?>

andrewsmd

4:07 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think you can do both, it has to be one or the other. The thing is, you should be able to send any kind of text within html if you style and structure it correctly. Can you give me an example of what exactly you are trying to do?

idfer

4:44 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



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.

rocknbil

5:07 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you absolutely can't get your multipart to work for text vs. html, a down and dirty method is

$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.

newb2seo

3:38 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Hi everyone thanks for your input.
I'm still working on this.
I've tried inserting/nesting the Content-Type: multipart/alternate but it is not working.

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.

idfer

1:00 am on Jul 15, 2009 (gmt 0)

10+ Year Member



OK i tried to adapt my mail function to accept attachments, i think i got it working for a single file, although one of the boundaries creeps into the HTML version for some reason (sorry for the lack of comments):

$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.