Help with cid:

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

Post Reply
Megrain
Forum Newbie
Posts: 8
Joined: Thu Aug 11, 2005 4:48 am
Location: South Africa

Help with cid:

Post by Megrain »

HI Guys I'm new to php and need some help I can find the problem on my system
here are the code

Code: Select all

<?
	//set timeout to never timeout
	set_time_limit(1000);
	$unsubscribe_link = "http://mycompany/unsubscribe.php";
	//$unsubscribe_link = "http://mycompany/unsubscribe.php";

	include("../connect.php");

	function getFilename($str){
		$arr = explode("/",$str);
		return $arr[count($arr)-1];
	}
	
	function breakHTML($str){
		$new_str="";
        $inTag = false;
        $cnt=0;
        for ($i=0;$i<strlen($str);$i++)
          {
                if ($str[$i]=="<" && $str[$i+1] != " ")
                $inTag = true;
                if ($str[$i]==">" && $inTag)
                    $inTag = false;
                if ($cnt>=70 && !$inTag && ($str[$i+1]==" " || $str[$i+1]=="<"))
                {
                    $new_str.="$str[$i]\r\n";
                    $cnt=0;
                }
                else 
                    $new_str.="$str[$i]";
                $cnt++;
        }
        return $new_str;
	}

	//getImages parse html code and returns a list of images in an array
	//the last element in the array contains the html img-tags ready for e-mail
	function getImages($text){
		$params="";
		$arr=explode("<img ",$text);			//break the html up in smaller pieces
		for ($i=1;$i<count($arr);$i++){			//start at the 2nd piece and loop through the pieces
			$arr2 = explode("src=\"",$arr[$i]);
			$imgsrc=$arr2[1];
			$pos = strpos($imgsrc,"\"");		//get the position of the "
			$theString=substr($imgsrc,0,$pos); 	//get the string between src=" and "
			//print "$theString<br><br>";
			$text=str_replace("$theString","cid:".getFilename($theString),$text);//replace all these ocurrences with the text we want
			$params[]=getFilename($theString);	//add to array
		}
		$params[]=$text;	//add the email-ready-html-code to the end of the array
		return $params;		//return the array
	}

	function fix_eq($txt){
		return str_replace("=\"","=3D\"",$txt);
	}

	//site files on
	$site = dirname($_SERVER["SCRIPT_FILENAME"]);
	$site1 = substr($site,0,strrpos($site,"/"));
	
	//loop around lists
	$sql="select * from lists order by name";
	$listres=query($sql);
	$numrows1=numrows($listres);

	$line1=0;
	$recipients="";
	while ($line1 < $numrows1)
	{
		$displist=getArray($listres,$line1);
		$listvar="list$displist[record]";
		$listval=$$listvar;
		if ($listval=="Y")
		{
			//loop around email addresses in this list
			$sql="select email from addresses where listid=$displist[record] and active='1'";
			$addressres=query($sql);
			$numrows2=numrows($addressres);
			if ($numrows2 > 0)
			{
				$line2=0;
				while ($line2 < $numrows2)
				{
					$dispaddress=getArray($addressres,$line2);
					$recipients[]="$dispaddress[email]";
					$line2++;
				}
			}
		}
		$line1++;
	}
	
	//draw up the email in html format
	$msg ="<html>\r\n";
	$msg.="<body>\r\n";			
	$msg.="<table width=3D\"63\" border=3D\"0\" align=3D\"center\" cellpadding=3D\"1\" cellspacing=3D\"0\">\r\n";
	$msg.="<tr>\r\n";
	$msg.="<td bgcolor=3D\"#336666\"><table width=3D\"42%\" border=3D\"0\" cellspacing=3D\"0\" cellpadding=3D\"0\">\r\n";
	$msg.="<tr>\r\n";
	$msg.="<td><img name=3D\"newsletter0\" src=3D\"cid:newsletter1.jpg\" width=3D\"241\" height=3D\"95\" border=3D\"0\" alt=3D\"\" /><img name=3D\"newsletter1\" src=3D\"cid:newsletter2.jpg\" width=3D\"273\" height=3D\"95\" border=3D\"0\" alt=3D\"\" /><img name=3D\"newsletter2\" src=3D\"cid:newsletter3.jpg\" width=3D\"266\" height=3D\"95\" border=3D\"0\" alt=3D\"\" /></td>\r\n";
	$msg.="</tr>\r\n";
	$msg.="<tr>\r\n";
	$msg.="<td valign=3D\"top\" background=3D\"cid:newsletter4.jpg\"><p><img name=3D\"newsletter3\" src=3D\"cid:newsletter4.jpg\" width=3D\"780\" height=3D\"3\" border=3D\"0\" alt=3D\"\" /></p>\r\n";
	$msg.="<table width=3D\"100%\" border=3D\"0\" cellspacing=3D\"0\" cellpadding=3D\"10\">\r\n";
	$msg.="<tr>\r\n";
	$msg.="<td>\r\n";
	$msg.="<TABLE WIDTH=3D\"750\">\r\n";

	$news_rs = query("select * from newsletter where record=$newsletterid");
	if (numrows($news_rs) > 0) {
		$news_ds = getArray($news_rs,0);
		$msg .= "<tr><td><font color=3D\"#CCCCCC\" size=3D\"2\" face=3D\"Arial, Helvetica, sans-serif\"><b>$news_ds[title]</b> - ".date("d-m-Y",$news_ds[date])."</font></td></tr>\r\n";
		$params=getImages($news_ds[article]);
		$html = $params[count($params)-1];
		$html=fix_eq($html);
		//$html=str_replace(">",">\n",$html);
		$html=breakHTML($html);
		$new_mess = $html;
			
		$msg .= "<tr><td>$new_mess</td></tr>\r\n";
	}
	$msg.="<tr><td><a href=3D\"$unsubscribe_link\">Click here to unsubscribe.</a></td></tr>\r\n";
	$msg.="</table>\r\n";
	$msg.="</td>\r\n";
	$msg.="</tr>\r\n";
	$msg.="</table>\r\n";
	$msg.="</td>\r\n";
	$msg.="</tr>\r\n";
	$msg.="<tr>\r\n";
	$msg.="<td><img name=3D\"newsletter5\" src=3D\"cid:newsletter6.jpg\" width=3D\"241\" height=3D\"88\" border=3D\"0\" alt=3D\"\" /><img name=3D\"newsletter6\" src=3D\"cid:newsletter7.jpg\" width=3D\"273\" height=3D\"88\" border=3D\"0\" alt=3D\"\" /><img name=3D\"newsletter7\" src=3D\"cid:newsletter8.jpg\" width=3D\"266\" height=3D\"88\" border=3D\"0\" alt=3D\"\" /></td>\r\n";
	$msg.="</tr>\r\n";
	$msg.="</table></td>\r\n";
	$msg.="</tr>\r\n";
	$msg.="</table>\r\n";
	$msg.="</body>\r\n";
	$msg.="</html>\r\n";

	//generate a boundary
	$boundary=strtoupper(md5(uniqid(time())));
	
	//generate another boundary
	$boundary2=strtoupper(md5(uniqid(time()+3000)));

	$headers = "From:<info@dlink.co.za>\n";
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-Type: multipart/related;\n\tboundary=\"$boundary\"\n";
	
	$body = "This is a multi-part message in MIME format.\n\n";

	$body .= "--$boundary\n";
	$body .= "Content-Type: multipart/alternative;\n\tboundary=\"$boundary2\"\n\n";
	
	$body .= "--$boundary2\n";
	$body .= "Content-Type: text/plain;\n\tcharset=\"utf-8\"\n";
	$body .= "Content-Transfer-Encoding: quoted-printable\n\n";
	
	$body .= "--$boundary2\n";
	$body .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
	$body .= "Content-Transfer-Encoding: quoted-printable\n\n";
	
	//where text should go
	$body .= $msg;
	
	$body .= "\n\n--$boundary2--";

	//this function attaches images to the email
	function attach($subdir,$filename)
	{
		global $boundary;
		global $site;

		if (strstr($filename,"jp"))
			$type="image/jpeg";
		if (strstr($filename,"gif"))
			$type="image/gif";

	    //open file for reading
		if ($subdir == "pics") $file="$subdir/$filename";
		else $file = "$subdir/$filename";
		//die($file);
		
		$fp=fopen($file,"r");
		$filesize=filesize($file);
		//read contents of file into a variable
		$contents=fread($fp,$filesize);
		//close file
		fclose($fp);
		//encode the file
		$attachment=chunk_split(base64_encode($contents));
		$att .= "\n\n--$boundary\n";
		$att .= "Content-Type: $type;\n\tname=\"$file\"\n";
		$att .= "Content-Transfer-Encoding: base64\n";
		$att .= "Content-Disposition: inline; filename=\"$filename\"\n";
		$att .= "Content-ID: <$filename>";
		$att .= "\n\n$attachment";
		return $att;
	}

	//attach pics
	$body.=attach("pics","newsletter1.jpg");
	$body.=attach("pics","newsletter2.jpg");
	$body.=attach("pics","newsletter3.jpg");
	$body.=attach("pics","newsletter4.jpg");
// 	$body.=attach("pics","newsletter5.jpg");
	$body.=attach("pics","newsletter6.jpg");
	$body.=attach("pics","newsletter7.jpg");
	$body.=attach("pics","newsletter8.jpg");
		
	
	//loop around the other images
	for ($a=0;$a<(count($params)-1);$a++)
		$body.=attach("images",$params[$a]);

	//final email boundary
	$body .= "\n\n--$boundary--";

	$numaddresses=count($recipients);
	$nummails=ceil($numaddresses/200);


	//loop around number of emails
	for ($a=1;$a<=$nummails;$a++)
	{
		$addresses="";
		$start=($a-1)*200;
		$end=$start+200;
		if ($end > $numaddresses)
			$end=$numaddresses;
		//take 200 addresses at a time
		for ($b=$start;$b<$end;$b++)
		{
			//get rid of last comma at end of each list
			$len=strlen($recipients[$a]);
			$addresses.="$recipients[$b],";
		}
		//get rid of last comma
		$len=strlen($addresses);
		$addresses=substr($addresses,0,$len-1);
		$theseheaders=$headers . "bcc: $addresses";
		mail("",$subject,$body,$theseheaders);
	}

	closeDB();
	print "<script language='javascript'>alert('The email has been successfully emailed to the mailing list!');";
	print "location.href='news_admin.php';</script>";
?>

Every time I sent the email all I get is the text and not the pictures if I have alook @ the source it says cid:newsletter1.jpg

what and I doing wrong can you please help.
Last edited by Megrain on Wed Nov 09, 2005 4:59 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Put tags around your (masses of) code, please.

What is 'cid:' ?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

@ quick glance :)

Code: Select all

$listvar="list$displist[record]";
should be

Code: Select all

$listvar=$displist['record'];
Megrain
Forum Newbie
Posts: 8
Joined: Thu Aug 11, 2005 4:48 am
Location: South Africa

Post by Megrain »

Nope should be like that

Code: Select all

$listvar="list$displist[record]";
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Ok, can you tell us what problem are you facing... whatabouts of that CID :?:
Megrain
Forum Newbie
Posts: 8
Joined: Thu Aug 11, 2005 4:48 am
Location: South Africa

Post by Megrain »

This is part of email system but the images don't show in the mails

I only get <img src="cid:filename">
I'm using linux and PHP 4.3.11

any system setting I need for MIME to work 100% ?

here is the result of the email.

Code: Select all

<html>
<body>
<table width="63" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td bgcolor="#336666"><table width="42%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img name="newsletter0" src="cid:newsletter1.jpg" width="241" height="95" border="0" alt="" /><img name="newsletter1" src="cid:newsletter2.jpg" width="273" height="95" border="0" alt="" /><img name="newsletter2" src="cid:newsletter3.jpg" width="266" height="95" border="0" alt="" /></td>
</tr>
<tr>
<td valign="top" background="cid:newsletter4.jpg"><p><img name="newsletter3" src="cid:newsletter4.jpg" width="780" height="3" border="0" alt="" /></p>
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>
<TABLE WIDTH="750">
<tr><td><font color="#CCCCCC" size="2" face="Arial, Helvetica, sans-serif"><b>Testing 101</b> - 09-11-2005</font></td></tr>
<tr><td><p>Email Testing 101 <br />Starts in 5 ,4 ,3 ,2 ,1 </p><p>and off it goes.
</p><p>Please visite <a href="http://company/">
</a></p><p><img src="cid:des6500.gif" align="baseline" /></p></td></tr>
<tr><td><a href="http://company/unsubscribe.php">Click here to unsubscribe.</a></td></tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img name="newsletter5" src="cid:newsletter6.jpg" width="241" height="88" border="0" alt="" /><img name="newsletter6" src="cid:newsletter7.jpg" width="273" height="88" border="0" alt="" /><img name="newsletter7" src="cid:newsletter8.jpg" width="266" height="88" border="0" alt="" /></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

but you yourself are replacing the path with cid: in your code here...

Code: Select all

$text=str_replace("$theString","cid:".getFilename($theString),$text);//replace all these ocurrences with the text we want
:?
Megrain
Forum Newbie
Posts: 8
Joined: Thu Aug 11, 2005 4:48 am
Location: South Africa

Post by Megrain »

Ok I see what you mean. But if I remove the line only the Image des6500.gif is added and it still don't work in the email.
Megrain
Forum Newbie
Posts: 8
Joined: Thu Aug 11, 2005 4:48 am
Location: South Africa

Thanx for the Help

Post by Megrain »

HI Thanx for the help I found my Problem I'm Using Postfix and Linux, and somewhere on the postfix server it don't handle any MIME requests so the code works fine on Windows and Apache. Do you now or any Good setup guide to enable MIME on Postfix or and other setup guide for MTA servers.
THanx Again for the Help.
Megrain
Never give up and never quit.
Megrain
Forum Newbie
Posts: 8
Joined: Thu Aug 11, 2005 4:48 am
Location: South Africa

Solution

Post by Megrain »

Postfix don't Support MIME BASE64
Vistit http://openwebmail.org/openwebmail/down ... s/?C=D;O=D and download the MIME BASE 64 install and of you Go.
Post Reply