MIME problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

MIME problem...

Post by ex247 »

Code: Select all

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
  $headers = "\nMIME-Version: 1.0\n" . 
            "Content-Type: multipart/mixed;\n" . 
            " boundary="{$mime_boundary}"" . 
            "From: "$list_name" <$owner_email>\r\nReply-To: $owner_email\r\nX-Mailer: Newsletter V$version by Gregory Mills\r\n";
  $message .= "This is a multi-part message in MIME format.\n\n" . 
            "--&#123;$mime_boundary&#125;\n" . 
            "Content-Type:text/html; charset="iso-8859-1"\n" . 
            "Content-Transfer-Encoding: 7bit\n\n" . 
            $message . "\n\n" . 
            "--&#123;$mime_boundary&#125;\n" . 
            "Content-Type: &#123;$fileatt_type&#125;;\n" . 
            " name="&#123;$fileatt_name&#125;"\n" . 
            //"Content-Disposition: attachment;\n" .
            //" filename="&#123;$fileatt_name&#125;"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data . "\n\n" . "--&#123;$mime_boundary&#125;--\n";

is that setup correct? becuase it seems to be printing the attachemtn binary code in the message of the email instead of an attachment
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

bump?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Are you encoding the actual attachment file?
You will need to use something like chunk_split(base64_encode($file)) to encode any attachments before you add them to the email.

Are you converting the email body (not header info) to a standard email format before you send it?
This is a standard function I use to encode mime emails before I send them.

Code: Select all

<?php
function convertToStandard($txt, $length=76)
{
$lines	= preg_split("/(?:\r\n|\r|\n)/", $txt);
$eol	= "\r\n";
$escape	= '=';
$output	= '';
		
while(list(, $line) = each($lines))
{
$linlen = strlen($line);
$newline = '';

for($i=0; $i<$linlen; $i++)
{
$char = substr($line, $i, 1);
$dec  = ord($char);

if(($dec == 32) AND ($i == ($linlen - 1)))
{
$char = '=20';
}
elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126))
{
$char = $escape.strtoupper(sprintf('%02s', dechex($dec)));
}

if((strlen($newline) + strlen($char)) >= $length)
{
$output  .= $newline.$escape.$eol;
$newline  = '';
}

$newline .= $char;
}

$output .= $newline.$eol;
}

return $output;
}

?>
There are loads of things you might be doing wrong.
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

that function, is it auto call, or do i have to define it into the send msg function?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

If you're using a class() as your email sender then add that function into the class() and send your prebuilt email body to it with
$myEmailBody = $this->convertToStandard($myEmailBody)

If you're just using functions that are not part of a class() then you pretty much do the same thing..
$myEmailBody = convertToStandard($myEmailBody).

That will convert your email body text into a standard which all emails use.

It won't report any errors within your email though so if you get any MIME stuff incorrect this function() won't find them...
that's down to you to sort out.
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

but how come it prints this ins the body:

Code: Select all

--==Multipart_Boundary_x87ca754d789f9d6274fc67d0153bb9eax
Content-Type: application/octet-stream;
 name="image.gif"
Content-Transfer-Encoding: base64

ZDg6YW5ub3VuY2UzNTpodHRwOi8vdG9ycmVudGJpdHMub3JnL2Fubm91bmNlLnBocDEzOmNyZWF0
aW9uIGRhdGVpMTA3MTc1NTA2NmU0OmluZm9kNTpmaWxlc2xkNjpsZW5ndGhpNzY1ZTQ6cGF0aGw1
MDowMC0ycGFjLWRqX3NweWRhX3ByZXNlbnRzX2Jsb29kX21vbmV5LTIwMDMtdGFzLm0zdWVlZDY6
bGVuZ3RoaTE0MjIyZ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0Ll9ib25lX3RodWdzLXRh
cy5tcDNlZWQ2Omxlbmd0aGk0MTAyODE1ZTQ6cGF0aGwyODoxMC0ycGFjLW5pZ2dhX25hdHVyZS10
YXMubXAzZWVkNjpsZW5ndGhpNTg3NTE3MWU0OnBhdGhsNDE6MTEtMnBhYy1maWdodF9tdXNpY19m
dC5famF5LXpfbmFzLXRhcy5tcDNlZWQ2Omxlbmd0aGk2MDU3NjEwZTQ6cGF0aGw0NDoxMi0ycGFj
LXVudGlsX3RoZV9lbmRfb2ZfdGltZV9mdC5fcmwtdGFzLm1wM2VlZDY6bGVuZ3RoaTYxNjA0Mjhl
NDpwYXRobDMxOjEzLTJwYWMtZG93bl9mb3JfYV90aHVnLXRhcy5tcDNlZWQ2Omxlbmd0aGk2MTk4
NjcxZTQ6cGF0aGw0NDoxNC0ycGFjLWdldF9pdF93ZXRfZnQuXzExMl9fbHVkYWNyaXMtdGFzLm1w
M2VlZDY6bGVuZ3RoaTgxNTcyMjhlNDpwYXRobDQxOjE1LTJwYWMtY2hpbGxfd2l0aF9tZV9mdC5f

--==Multipart_Boundary_x87ca754d789f9d6274fc67d0153bb9eax--
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

That's how images and attachments are included in the emails. If it's showing up the actual email as text though (when you read it) then there's something wrong with the way you have built the email.

If there is one thing wrong, or one incorrect MIME tag in the email then it won't display as intended.

The top part is also incorrect it should look something like this.. for an attachment anyway..

Code: Select all

Content-Type: application/octet-stream;
      name="myFile.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
      filename="myFile.zip"
You will need to keep any 'tabs' in the email (notice the tabs in the above text) and can use chr(9) to add tabs to the text.

Your best bet is to hunt down a pre-made PHP emailer and see how the various different emails are structured. Try searching Google to find some.
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

Code: Select all

$mime_boundary = "==Multipart_Boundary_x&#123;$semi_rand&#125;x"; 
  $headers = "\nMIME-Version: 1.0\n" . 
            "Content-Type: multipart/mixed;\n" . 
            " boundary="&#123;$mime_boundary&#125;"" . 
            "From: "$list_name" <$owner_email>\r\nReply-To: $owner_email\r\nX-Mailer: Newsletter V$version by Gregory Mills\r\n";
  $message .= "This is a multi-part message in MIME format.\n\n" . 
            "--&#123;$mime_boundary&#125;\n" . 
            "Content-Type:text/html; charset="iso-8859-1"\n" . 
            "Content-Transfer-Encoding: 7bit\n\n" . 
            $message . "\n\n" . 
            "--&#123;$mime_boundary&#125;\n" . 
            "Content-Type: &#123;$fileatt_type&#125;;\n" . 
            " name="&#123;$fileatt_name&#125;"\n" . 
			"Content-Transfer-Encoding: base64\n" .
            "Content-Disposition: attachment;\n" .
            " filename="&#123;$fileatt_name&#125;"\n" .
            $data . "\n\n" . "--&#123;$mime_boundary&#125;--\n";
is that correct to you? becuase its the same setup as many i have found
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I don't know... it depends on how you are building the email and if you are including just one attachment, multiple attachments, embeded images in the actual HTML and so on.

This is a function I use to build a very basic email with X amount of attachments.. it's from my own email class() I've created so there are other functions which are also required.. but it might help you out a bit.

Code: Select all

<?php

	function buildTA()
	{
		$this->headers = ""
		. "MIME-Version: 1.0".NL
		. "Content-Type: multipart/mixed;".NL.chr(9)
		. "boundary="----=_SIROR4_MIME_A"".NL
		. "X-Priority: ".$this->priority[0].NL
		. "X-MSMail-Priority: ".$this->priority[1].NL
		. "X-Mailer: SIRO R4 Mimemail 1.0".NL
		. "X-UIDL: SIRO R4 ".time()."".NL;

		$this->body = ""
		. "This is a multi-part message in MIME format.".NL.NL
		. "------=_SIROR4_MIME_A".NL
		. "Content-Type: multipart/alternative;".NL.chr(9)
		. "boundary="----=_SIROR4_MIME_B"".NL.NL
		. "------=_SIROR4_MIME_B".NL
		. "Content-Type: text/plain;".NL.chr(9)
		. "charset="ISO-8859-1"".NL
		. "Content-Transfer-Encoding: quoted-printable".NL.NL
		. $this->message.NL.NL
		. "------=_SIROR4_MIME_B".NL
		. "Content-Type: text/html;".NL.chr(9)
		. "charset="ISO-8859-1"".NL
		. "Content-Transfer-Encoding: quoted-printable".NL.NL;

		$html = ""
		. "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">".NL
		. "<html>".NL
		. "<head>".NL
		. "<title> ".$this->subject." </title>".NL
		. "</head>".NL
		. "<body>".NL
		. nl2br($this->message).NL
		. "</body>".NL
		. "</html>".NL;

		$html = str_replace("[MESSAGE]", (nl2br($this->message)), $html);
		$html = $this->convertToStandard($html);

		$this->body.= ""
		. $html
		. "------=_SIROR4_MIME_B--".NL.NL
		. $this->addAttachments("------=_SIROR4_MIME_A")
		. "------=_SIROR4_MIME_A--";
	}

?>
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

there a link to any class i can edit to put my own variables in to send the mail?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You might find something at http://www.hotscripts.com
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Here's a link to an open source PHP Email Class() which I've just found on Google for you. It has everything you need in it to send an email with an attachment and stuff.

http://docs.xaraya.com/code-docs/phpxre ... ource.html

That's the best I can do I'm afraid. It's very difficult trying to explain to someone how to build a mime-based email... check out the link and take it from there.
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

thx man,... i found that...

but heres my problem now:

Code: Select all

function admin_write_msg() &#123;
 global $username, $password, $owner_email, $list_name, $admin_keyword;
echo "
<form name="upload" method="post" enctype="multipart/form-data">
<input type="hidden" name="username" value="$username">
<input type="hidden" name="password" value="$password">
<input type="hidden" name="action" value="$admin_keyword">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<table align="center" border="0">
<tr><td colspan="2" align="center" valign="top"><font size="+1"><b>Send A Message:</b><br>&nbsp;</td></tr>
<tr><td align="left" valign="top"><b>From: </b></td><td align="left" valign="top">"$list_name" <$owner_email></td></tr>
<tr><td align="left" valign="top"><b>Subject: </b></td><td align="left" valign="top"><input type="text" name="subject" size="35"></td></tr>
<tr><td align="left" valign="top"><b>Message: </b></td><td align="left" valign="top"><textarea rows="15" cols="65" wrap="off" name="message"></textarea></td></tr>
<tr><td align="left" valign="top"><b>Attachment: </b></td><td align="left" valign="top"><input name="attach" type="file"></td></tr>
<tr><td></td><td align="left" valign="top"><input type=submit class='submit' name='action' value='Send'>&nbsp;<input type="reset" value="Clear"></td></tr></table>
</form>";

  $addresses = @file($list_file) or die("<center><b>The list data file could not be opened.</b><br>Check the path and permissions.</center>");
  $addresses&#1111;] = $owner_email;
if (isset($_POST&#1111;'subject'])) &#123;
	
	if ($attach != 'none') &#123;
	$fname = './'.$attach_name;  // make a real filename
	// Get the content-type of the uploaded file

	if (preg_match("!/x\-.+!i", $attach_type)) 
		$type = OCTET;
	else
		$type = $attach_type;

	$from = sprintf("'%s' <%s>", $name, $email) ;
	copy($attach, $fname); //do error checking if need 	
	$mime = new MIME_mail("$list_name <$owner_email>", $addresses, "$subject", "$message", "Cc: $email");
	$mime->fattach($fname, "Resume of $name", $type);
	$mime->send_mail();
	echo "Dear $name<p>Your resume has been emailed and a copy sent to you<br>";
	unlink($attach); 	// remove the uploaded file
	&#125; else &#123;
	echo "Dear $name<p>You have not submitted your resume.  Please use the browse button to attach it and click send!<br>";
	&#125;
&#125;

&#125;
it just doesnt send, somethign is wrong there.
the mime vars are set in the class
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Go through the top part of the class() and make sure that all of the variables are set-up to how you want them. Also make sure you are sending the class() all the info it needs.

You'll just have to take your time and go through the class() step by step to make sure it's right for you. I'm sure you could find some simpler ones if you need to. Use Google and search for something like "PHP Email Class" etc.
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

ok, i think my problem is it wont reed from a file,

do i need to define a new var and then somehow make it reed files from a nother file (be it a text or a dat file)
Post Reply