Jump to content

Please please help.. php email


phanku

Recommended Posts

Okay so I am building this script that will get data from a form and then format it into html, plain text, and a csv and then put it all together and email it to people. 

 

I have been bashing my head against this for a while.  The emails do not come out right. I get the plain text part, and the attachment fine in outlook but I also see the unparsed html code.  In gmail(which my boss want the emails to work in that also) all I get is the plain text, the unparsed html and then the unparsed attachment.

 

I need help please guys.  I think the problem is in the headers I am trying to send out in the email.

 

Included is my object that builds the email and email headers.

 

Thanks in advance!!

 

 

<?
//*********************************************
//emailBuilder.php

//July 09, 2009
//Purpose:
//
// This file will handle building an array
//   populated with everything needed to email
//   out html emails with attachments.
//
//  --This file DOES not actually do the mailing.
//
//  Use 
//  buildEmail(
//		<contact email>,
//		<mail config object>, 
// 		<binary data for attachment>,
//		) to build email address.
//
//  return array structure as follows..
//
//	[0] = headers
//  [1] = body of email
//
//*********************************************

class emailBuilder {

//array buildEmail(string, object, string?)
function buildEmail($contact_email, $mail_config, $binary) {


	//First build the array that is going to host the completed data.
	$complete = array();

	//build the two random hashs needed
	$random_h1 = md5(date("r", time()));
	$random_h2 = md5(date("r", time() + 1));

	//Next build the headers..	
	$complete[0] = $this->buildEmailHeader($random_h1, $contact_email);

	//Next build the alterntive header in the body.
	$complete[1] = $this->buildAlternativeHeader($random_h1, $random_h2);

	//Next add the plain text to the body.
	$complete[1] .= $mail_config->buildTextEmail();

	//Next add the boundary for the html section.
	$complete[1] .= $this->buildHtmlHeader($random_h2);

	//next add the html content.
	$complete[1] .= $mail_config->buildHtmlEmail();

	//Next add the boundary for the attachment
	$complete[1] .= $this->buildAttachmentHeader($random_h1, $random_h2);

	//Next add the attachment
	$complete[1] .= chunk_split(base64_encode(file_get_contents("test.txt")));

	//Finish the boundary
	$complete[1] .= $this->buildAttachmentEnd($random_h1);

	return $complete;		
}//end function buildEmail	

//string buildAttachmentEnd(string)
//This function will build the needed end boundary zone for
//  the attachment of the email.
function buildAttachmentEnd($random_h1) {
	$header .= "--" . $random_h1 . "-- \r\n";

	return $header;
}//end function buildAttachment End

//string buildAttachmentHeader(string, string)
// This function will build the needed boundary zone for
//  the attachment section of the email.
function buildAttachmentHeader($random_h1, $random_h2) {
	$header = " \r\n";
	$header .= "--" . $random_h2 . "-- \r\n";
	$header .= "--" . $random_h1 . " \r\n";
	$header .= "Content-Type: application/octet-stream; name=\"SSOP.txt\" \r\n";
	$header .= "Content-Disposition: attachment; filename=\"SSOP.txt\" \r\n";
	$header .= "Content-Transfer-Encoding: base64 \r\n";
	$header .= "Content-Description: SSOP.txt \r\n";

	return $header;
}//end function buildAttachmentHeader

//String buildHtmlHeader(string)
//This function will build the needed boundary zone for 
//  The html section of the email.
function buildHtmlHeader($random_h2) {
	$header = " \r\n ";
	$header .= "--" . $random_h2 . "\r\n";
	$header .= "Content-Type: text/html; charset=\"ISO-8859-1\" \r\n";
	$header .= "Content-Transfer-Encoding: quoted-printable\r\n";

	return $header;	
}//end function buildHtmlHeader

//String buildHtmlHeaders(string, string)
//This function will build the needed headers needed by 
//  email applications to display the html email correctly.
function buildEmailHeader($random_hash, $contact_email) {
	//build headers and subject

	// Always set content-type when sending HTML email
	$header = "MIME-Version: 1.0" . "\r\n";
	$header .= "From: [email protected] \r\n";
	$header .= "Reply-To: " . $contact_email . "\r\n";
	$header .= "Content-Type: multipart/mixed; boundary=\"" . $random_hash . "\"\r\n";

	return $header;
} // end function buildHtmlHeaders

//String buildAlternativeHeader(string, string)
//This function will build the needed alternative and plain text boundary zones.
function buildAlternativeHeader($hash1, $hash2) {
	$header = "--" . $hash1 . "\r\n";
	$header .= "Content-Type: multipart/alternative; boundary=\"" . $hash2 . "\"\r\n";
	$header .= "--" . $hash2 . "\r\n";
	$header .= "Content-Type: text/plain; charset=\"ISO-8859-1\" \r\n";
	$header .= "Content-Transfer-Encoding: 7bit \r\n";

	return $header;	
}//end function buildAlternativeHeader

}// end class emailBuilder
?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/165826-please-please-help-php-email/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.