SOLVED - mail ?

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
lewdog23
Forum Newbie
Posts: 1
Joined: Tue May 09, 2006 2:41 pm

SOLVED - mail ?

Post by lewdog23 »

Alright, no need to answer this one as I found a site that had what I was looking for. http://hnsg.net/tutorials/exim_cpanel.html

I didn't change the DNS server as they recommend, but I did make sure it was queueing properly. This cut the script run time down dramatically. 1000 emails used to take us 30+ minutes of the page loading, it now takes 30+ seconds. Much better.




Hello all,

I'm running an e-mail script where I gather the subject, a plain text version of the body and also an html version of the body. I would then like to send it out to ~1000 recipients. The problem that I'm having is that this script takes forever to run. To send out a 1000 emails, it took approx. 30 minutes. It wouldn't be so bad if it would just rip through the list, queue them up on the mail server and then return control to the user, but instead the page gets loaded for the full 30 minutes while it tries to send them out effectively blocking the user from doing anything. Does anyone have any ideas as to how to fix this problem? I believe the mail server we are running is exim and I've heard it doesn't handle queues very well, might that be the problem? If so, does anyone have any recommendations as to what mail server I should switch to.

Here is the code so you can check it out.

Code: Select all

<?php	
	function buildEmailHeader($from, $plain_text, $html_text) {
		$headers = "From: $from\r\n";
		$headers .= "MIME-Version: 1.0\r\n";
		$boundary = uniqid("blahblah");
		$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n\r\n";
		$headers .= "This is a MIME encoded message.\r\n\r\n";
		$headers .= "\n--$boundary\r\n" .
			"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
			"Content-Transfer-Encoding: base64\r\n\r\n";
		$headers .= chunk_split(base64_encode($plain_text));
		$headers .= "\n--$boundary\r\n" .
			"Content-Type: text/html; charset=ISO-8859-1\r\n" .
			"Content-Transfer-Encoding: base64\r\n\r\n";
		$headers .= chunk_split(base64_encode($html_text));
		return $headers;
	}
	
	function buildHTMLBody($header, $body) {
		$output = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>' . $header . '</title>
<style type="text/css">
<!--
body{
	margin: 0;
	background-color: #F3F1E8;
}
#contentDiv{
	font-family: "Lucida Grande", geneva, verdana, helvetica, arial, sans;
	font-size: 12px;
	line-height: 1.6em;
	color: #555345;
	width: 600px;
	margin: 20px auto;
}
#footerDiv{
	border-top: solid 1px #D6D2BA;
	margin-top: 15px;
	padding-top: 10px;
	font-size: 10px;
	text-align: right;
}
h1{
	margin: 15px 0;
	letter-spacing: -0.04em;
	font-size: 22px;
	line-height: 25px;
	color: #610B1A;
}
img{
	border: 0;
}
a:link{
	color: #610B1A;
}
a:visited{
	color: #555345;
}
a:hover{
	color: #9E763C;
}
-->
</style>
</head>
<body>
	<div id="contentDiv">
		<h1>' . $header . '</h1>
		' . $body . '
		<div id="footerDiv"></div>
	</div>
</body>
</html>';
		return $output;
	}
	
	$form_errors = NULL;
	
	$subject = '';
	$header = '';
	$plaintext_body = '';
	$html_body = '';
	
	if ($_POST) {
		
		if (empty($_POST['subject'])) {
			$errors[] = 'Email Subject Field is empty';
			$form_errors['subject'] = TRUE;
		}
		if (empty($_POST['header'])) {
			$errors[] = 'Email Header Field is empty';
			$form_errors['header'] = TRUE;
		}
		if (empty($_POST['plaintext_body'])) {
			$errors[] = 'Plaintext Body Field is empty';
			$form_errors['plaintext_body'] = TRUE;
		}
		if (empty($_POST['html_body'])) {
			$errors[] = 'HTML Body Field is empty';
			$form_errors['html_body'] = TRUE;
		}
		if (!$errors) {
			$h_body = buildHTMLBody($_POST['header'], $_POST['html_body']);
			$header = buildEmailHeader('membership@blahblah.com', $_POST['plaintext_body'], $_POST['html_body']);
			$emails = $db->get_results("SELECT email FROM email_test");  // contains 1000 test email accounts
			$i = 1;
			foreach ($emails as $email) {
				mail($email->email, $_POST['subject'], "", $header);
				$i++;
			}
			echo "$i emails sent out";
		}		
	}
?>
	
<a class="buttonLink" href="index.php?page=admin_member_list"><&nbsp;BACK TO MEMBER LIST</a>
<?php	
	if (isset($errors) && $errors) {
		echo '<ul class="ErrorBox">';
		foreach ($errors as $error) {
			echo "<li>$error</li>";
		}
		echo "</ul>";
	}	
?>
<div class="formRequiredKey"><span class="requiredMark">*</span> - Denotes required field</div>

<form class="admin" name="email_form" method="post" action="#">
<table class="adminFormTable">
	<tr>
		<td width="25%" class="adminFormLabel"><?php echo $fb->buildLabel('Email Subject', 'subject', TRUE, 'subject', $form_errors); ?></td>
		<td><?php echo $fb->buildTextInput('subject', 'subject', 80, $subject, $form_errors); ?></td>
	</tr>
	<tr>
		<td width="25%" class="adminFormLabel"><?php echo $fb->buildLabel('Email Body Header', 'header', TRUE, 'header', $form_errors); ?></td>
		<td><?php echo $fb->buildTextInput('header', 'header', 80, $subject, $form_errors); ?></td>
	</tr>
	<tr>
		<td class="adminFormLabel"><?php echo $fb->buildLabel('Plaintext Email Body', 'plaintext_body', TRUE, 'plaintext_body', $form_errors); ?></td>
		<td><?php echo $fb->buildTextAreaInput('plaintext_body', 'plaintext_body', $plaintext_body, $form_errors); ?></td>
	</tr>
	<tr>
		<td class="adminFormLabel"><?php echo $fb->buildLabel('HTML Email Body', 'html_body', TRUE, 'html_body', $form_errors); ?></td>
		<td><?php $oFCKeditor = new FCKeditor('html_body'); $oFCKeditor->ToolbarSet = 'ClientEdit'; $oFCKeditor->Value = $html_body; $oFCKeditor->Create(); ?></td>
	</tr>	
</table>	
</form>
<div class="topMargin10">
	<a class="buttonLink floatRight" href="#" onClick="document.email_form.submit();">SEND</a>
</div>
Post Reply