Page 1 of 1

PHP Mail Issues - Blank Attachements, A Few Blank Emails

Posted: Mon Aug 23, 2010 2:30 pm
by tymlls05
I am using the script below to send one email to multiple recipients that are pulled from my database. The email sends correctly to a majority of the recipients but a few users (gmail for example) can't see the body of the message. What would cause the body to be blank?

I am using tinymce to format the body of the messages but usually nothing more than bold/italics/underline print and font face. Would tinymce be causing the problem?

My second problem is that though I can attach files to the email fine, whether I attach a file or not there is always an extra, blank, file attached that has no extention. How can I cure this also?

I'm sorry if I am just asking for help with too little information provided but this is my first attempt at using a php mailing system and am not sure what other information needs to be provided.

Thanks!

Code: Select all

<head>

<link rel="stylesheet" type="text/css" href="content.css" />

<!-- TinyMCE -->
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	
	// O2k7 skin (silver)
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : "textbody",
		theme : "advanced",
		skin : "o2k7",
		skin_variant : "black",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,autosave",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "http://agents.sallison.com/app/webroot/mail/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
</script>
<!-- /TinyMCE -->
</head>
<body>
<div width="768px"><center>
<div style="background-color:lightgreen; text-align:center;  width:650px; "><a href="http://agents.sallison.com" style="font-size:16px;"><< Return To Agent Website</a></div>
<?php

$action = $_REQUEST['action'];
global $action;


function showForm() {

//Connect To The SQL Server
	$con=mysql_connect(myhost','user','Pass4user'); 
	if (!$con){
		die('Could not connect: ' . mysql_error());
	}

	//Select DB
	mysql_select_db("mydb", $con);
	$result = mysql_query('SELECT * FROM users WHERE `email` IS NOT NULL AND `user_group_id` =3 OR `user_group_id` =4');
	$to_recipient='';
	while($row = mysql_fetch_array($result)){
		$to_recipient=$to_recipient.$row['full_name'].' <'.$row['email'].'>, ';
	}
	$to_recipient=substr_replace($to_recipient ,"",-2);
?>
<div style="font-size:20px; margin-top:14px; width:650px;">This Email Will Be Sent To All San Angelo Agents<br></font>
<table style="width:650px;">
	<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
	<input type="hidden" name="action" value="send" />
	<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
	<tr style="display:none;">
		<td>Recipient Name:</td>
		<td><input name="to_name" size="50" value="<? echo $to_recipient ?>"/></td>
	</tr>
	<tr>
		<td>Your Name:</td>
		<td><input name="from_name" size="50" /></td>
	</tr>
	<tr>
		<td>Your Email:</td>
		<td><input name="from_email" size="50" /></td>
	</tr>
	<tr>
		<td>Topic:</td>
		<td><input name="subject" size="50" /></td>
	</tr>
	<tr>
		<td>Message:</td><td></td>
	</tr>
	<tr>
		<td colspan=2><textarea id="textbody" name="body" rows="10" cols="50"></textarea></td>
	</tr>
	<tr>
		<td>Attach File:</td>
		<td ><input type="file" name="attachment" size="50" /></td>
	</tr>
	<tr>
	<td colspan=2>
	<p style="text-align:justify; font-size:11px; font-weight:normal; background-color:lightgreen;">Please allow up to 15 minutes for recipients to get this email. If you attach a large file it could take up to 30 minutes or more to get to all intended recipients.
	<input type="submit" value="Send Email" style="font-size:18px; color:green; font-weight:bold;" /></td>
	</tr>
  </table>
<?php
}

function sendMail() {
  if (!isset ($_POST['body'])) { //Oops, forgot your email addy!
    die ("<p>Oops!  You forgot to type a message!</p>");
  }
  else {
    $to_name = $_POST['to_name'];
    $from_name = stripslashes($_POST['from_name']);
    $subject = stripslashes($_POST['subject']);
    $body = stripslashes($_POST['body']);
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name']; 
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
      $fp = fopen($attachment, "rb"); //Open it
      $data = fread($fp, filesize($attachment)); //Read it
      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
        fclose($fp);
    }
    //Let's start our headers
	$headers = 'To: '.$to_name.'' . "\r\n";
    $headers .= "From: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; 
    $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; 
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "------=MIME_BOUNDRY_main_message \n"; 
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; 
    
    $message = "------=MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; 
    $message .= "Content-Transfer-Encoding: quoted-printable\n"; 
    $message .= "\n"; 
    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $message .= "$body\n";
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_message_parts--\n"; 
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message\n"; 
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message--\n"; 

    // send the message
    if (mail('"'.$to_name.'"', $subject, $message, $headers)){ 
		print "<div style=\"background-color:lightyellow;\">Your email has been sent to all of the Scott Allison Real Estate Agents</div>";
	}else{
		echo "Failed To Send To '$to_name', $subject, $message, $headers";
	}
  }
}

?>

<!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" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style="css" type="text/css">
      <!--
      body {
        margin: 0px;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
      }
      a {color: #0000ff}
      -->
    </style>
  </head>
  <body>

<?php
switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}
?>

  </body>
</html>