Location: PHPKode > projects > Assignment Calculator > research-project-calculator-20100624/rpc/include/email_functions.php
<?php
/**********************************************************
Function: build_multipart_headers
Author: Michael Berkowski
Last Modified: September 2007
***********************************************************
Purpose:
Creates email headers for a message of type multipart/mime
This will include a plain text part and HTML.
**********************************************************/ 
function build_multipart_headers($boundary_rand)
{
	global $EMAIL_FROM_DISPLAY_NAME, $EMAIL_FROM_ADDRESS, $CALC_PATH, $CALC_TITLE, $SERVER_NAME;
	// Using \n instead of \r\n because qmail doubles up the \r and screws everything up!
	$crlf = "\n";
	$message_date = date("r");
	
	// Construct headers for multipart/mixed MIME email.  It will have a plain text and HTML part
	$headers = "X-Calc-Name: $CALC_TITLE" . $crlf;
	$headers .= "X-Calc-Url: http://{$SERVER_NAME}/{$CALC_PATH}" . $crlf;
	$headers .= "MIME-Version: 1.0" . $crlf;
	$headers .= "Content-type: multipart/alternative;" . $crlf;
	$headers .= "   boundary=__$boundary_rand" . $crlf;
	$headers .= "From: $EMAIL_FROM_DISPLAY_NAME <$EMAIL_FROM_ADDRESS>" . $crlf;
	$headers .= "Sender: $EMAIL_FROM_DISPLAY_NAME <$EMAIL_FROM_ADDRESS>" . $crlf;
	$headers .= "Reply-to: $EMAIL_FROM_DISPLAY_NAME <$EMAIL_FROM_ADDRESS>" . $crlf;
	$headers .= "Return-Path: $EMAIL_FROM_DISPLAY_NAME <$EMAIL_FROM_ADDRESS>" . $crlf;
	$headers .= "Date: $message_date" . $crlf;
	$headers .= "Message-Id: $boundary_rand@$SERVER_NAME" . $crlf;
	
	return $headers;
}
/**********************************************************
Function: build_multipart_body
Author: Michael Berkowski
Last Modified: September 2007
***********************************************************
Purpose:
Builds the email body content to go with the headers from
build_multipart_headers()
**********************************************************/ 
function build_multipart_body($plain_text_message, $html_message, $boundary_rand)
{
	//$crlf = "\r\n";
	$crlf = "\n";
	$boundary = "__" . $boundary_rand;
	
	// Begin constructing the MIME multipart message
	$multipart_message = "This is a multipart message in MIME format." . $crlf . $crlf;
	$multipart_message .= "--{$boundary}{$crlf}Content-type: text/plain; charset=\"us-ascii\"{$crlf}Content-Transfer-Encoding: 7bit{$crlf}{$crlf}";
	$multipart_message .= $plain_text_message . $crlf . $crlf;
	$multipart_message .= "--{$boundary}{$crlf}Content-type: text/html; charset=\"iso-8859-1\"{$crlf}Content-Transfer-Encoding: 7bit{$crlf}{$crlf}";
	$multipart_message .= $html_message . $crlf . $crlf;
	$multipart_message .= "--{$boundary}--$crlf$crlf";
	
	return $multipart_message;
}
/**********************************************************
Function: build_step_email_body_text
Author: Michael Berkowski
Last Modified: September 2007
***********************************************************
Purpose:
Returns a plain text version of the email body to be used
for individually sent step reminders
**********************************************************/ 
function build_step_email_body_text($stepnum, $arr_instructions, $dates, $query_string, $teacher_info ,$name, $class, $project_id)
{
	global $CALC_PATH, $CALC_TITLE, $SERVER_NAME;

	$step_email_body =<<<BODY

$CALC_TITLE
Step $stepnum: {$arr_instructions["step$stepnum"]["title"]}

Name: $name
Class: $class

BODY;

	$step_email_body .= build_text_single_step($stepnum, $arr_instructions, $dates, $query_string, $teacher_info);
	$step_email_body .= "\n\n";	
	$step_email_body .=<<<FOOTER
The $CALC_TITLE offers suggestions, but be sure to check with your teacher to find out the best working schedule for your assignment!	
	
If you would like to stop receiving further reminders for this project, click the link below:
http://$SERVER_NAME/$CALC_PATH/deleteproject.php?proj=$project_id
	
FOOTER;
	// Wrap text to 78 chars per line
	// Convert any remaining HTML <br /> to \r\n
	// Strip out any remaining HTML tags.
	$step_email_body = strip_tags(linebreaks_html2text(wordwrap($step_email_body, 78, "\n")));
	return $step_email_body;
}
/**********************************************************
Function: build_step_email_body_html
Author: Michael Berkowski
Last Modified: September 2007
***********************************************************
Purpose:
Same as above, but with HTML
**********************************************************/ 
function build_step_email_body_html($stepnum, $arr_instructions, $dates, $query_string, $teacher_info, $name, $class, $project_id)
{
	global $CALC_PATH, $CALC_TITLE, $SERVER_NAME;
	$styles = build_html_styles();
	$step_email_body =<<<BODY
<html>
<head>
	<title> $CALC_TITLE </title>
	$styles
</head>
<body>
<h1> $CALC_TITLE Schedule </h1>
<strong>Name:</strong> 	$name <br />
<strong>Class:</strong>	$class <br />
	
BODY;
	
	$step_email_body .= build_html_single_step($stepnum, $arr_instructions, $dates, $query_string, $teacher_info);
	
	$step_email_body .=<<<FOOTER
<p>
	The $CALC_TITLE offers suggestions, but be sure to check with your teacher to find out the best working schedule for your assignment!
</p>
<p>
	If you would like to stop receiving further reminders for this project, 
	<a href="http://{$SERVER_NAME}/$CALC_PATH/deleteproject.php?proj=$project_id">click this link.</a>
</p>
</body>
</html>
	
FOOTER;
	return $step_email_body;
}

/**********************************************************
Function: build_html_styles
Author: Michael Berkowski
Last Modified: September 2007
***********************************************************
Purpose:
Just returns a string of <style /> for the HTML message body
**********************************************************/ 
function build_html_styles()
{
	$styles =<<<STYLES
	<style type="text/css">
		body { font-family: Arial, sans-serif; font-size: 85%; }
		h1 { font-size: 120%; }
		table { border: none; }
		tr { vertical-align: top; }
		img {	display: none; }
		hr { border: 0; }
	</style>

STYLES;
	return $styles;
}

/**********************************************************
Function: linebreaks_html2text
Author: Michael Berkowski
Last Modified: October 2007
***********************************************************
Purpose:
Convert <br /> html tags to \n line breaks
**********************************************************/ 
function linebreaks_html2text($in_string)
{
	$out_string = "";
	$arr_br = array("<br>", "<br />", "<br/>");
	$out_string = str_replace($arr_br, "\n", $in_string);
	return $out_string;
}

?>
Return current item: Assignment Calculator