php mail with attachment woes
Posted: Tue Nov 11, 2008 4:33 pm
i am about to tear my hair out because i am having so many problems with this e-mail attachment
. The e-mail gets sent but most mail clients do not see the message or attachment (outlook seems to be the exception). if anyone can see what i have done wrong please let me know. Please don't laugh i know i suck at php
.
edit :: added color coding, sorry for not including that the first time around 
Code: Select all
<?
$title = 'Create Print Portal Login';
include("header.php");
$field_1 = $_POST['field_1'];
$field_2 = $_POST['field_2'];
$field_3 = $_POST['field_3'];
$field_4 = $_POST['field_4'];
$field_5 = $_POST['field_5'];
$field_6 = $_POST['field_6'];
$field_7 = $_POST['field_7'];
$field_8 = $_POST['field_8'];
$field_9 = $_POST['field_9'];
$field_10 = $_POST['field_10'];
$field_11 = $_POST['field_11'];
$field_12 = $_POST['field_12'];
$field_13 = $_POST['field_13'];
?>
<table width="803" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="24" align="left" valign="top"></td>
<td width="140" align="left" valign="top"><?php include_once ('../brighton.mi/inc/left.php') ?></td>
<td align="left" valign="top"><table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="600" height='465' align="left" valign="top"><div style="padding-left:17px; padding-top:30px; padding-right:28px;"><center><h1>Create a Lavida Print Portal Logon</h1><br />
Fields marked with <span style="color : #ff0000;">*</span> are required<br />
<table>
<tr><td colspan="4"><form method="post" action="<?php echo $php_self;?>">
<select name="field_1">
<option <?php if(!isset($_POST['field_1'])) {echo "selected";} ?> value="NS">Choose a Social Title</option>
<option <?php if($_POST['field_1'] === 'Mr.') {echo "selected";}?> value="Mr.">Mr.</option>
<option <?php if($_POST['field_1'] === 'Mrs.') {echo "selected";}?> value="Mrs.">Mrs.</option>
<option <?php if($_POST['field_1'] === 'Miss') {echo "selected";}?> value="Miss">Miss.</option>
<option <?php if($_POST['field_1'] === 'Ms.') {echo "selected";}?> value="Ms.">Ms.</option>
<option <?php if($_POST['field_1'] === 'Dr.') {echo "selected";}?> value="Dr.">Dr.</option>
</select></td></tr><tr>
<td /><td>First Name<span style="color : #ff0000;">*</span></td><td><input type="text" Name='field_2' value="<?php echo $field_2; ?>"></td>
<td>Last Name<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_3 value="<?php echo $field_3; ?>"></td></tr>
<td /><td>Title</td><td><input type="text" Name=field_4 value="<?php echo $field_4; ?>"></td>
<td>LaVida Email<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_5 value="<?php echo $field_5; ?>"></td></tr>
<td /><td>Street Address<span style="color : #ff0000;">*</span></td><td colspan="3"><input type="text" size="55" Name=field_6 value="<?php echo $field_6; ?>"></td></tr>
<td /><td>City<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_7 value="<?php echo $field_7; ?>"></td>
<td>State<span style="color : #ff0000;">*</span></td><td><select name=field_8 value="<?php echo $field_8; ?>">
<option <?php if(!isset($_POST['field_8'])) {echo "selected";}?> value="NS">Select A State</option>
<option <?php if($_POST['field_8'] === 'AL') {echo "selected";}?> value="AL">Alabama</option>
<option <?php if($_POST['field_8'] === 'AK') {echo "selected";}?> value="AK">Alaska</option>
<option <?php if($_POST['field_8'] === 'AZ') {echo "selected";}?> value="AZ">Arizona</option>
<option <?php if($_POST['field_8'] === 'AR') {echo "selected";}?> value="AR">Arkansas</option>
<option <?php if($_POST['field_8'] === 'CA') {echo "selected";}?> value="CA">California</option>
<option <?php if($_POST['field_8'] === 'CO') {echo "selected";}?> value="CO">Colorado</option>
<option <?php if($_POST['field_8'] === 'CT') {echo "selected";}?> value="CT">Connecticut</option>
<option <?php if($_POST['field_8'] === 'DE') {echo "selected";}?> value="DE">Delaware</option>
<option <?php if($_POST['field_8'] === 'DC') {echo "selected";}?> value="DC">District of Columbia</option>
<option <?php if($_POST['field_8'] === 'FL') {echo "selected";}?> value="FL">Florida</option>
<option <?php if($_POST['field_8'] === 'GA') {echo "selected";}?> value="GA">Georgia</option>
<option <?php if($_POST['field_8'] === 'HI') {echo "selected";}?> value="HI">Hawaii</option>
<option <?php if($_POST['field_8'] === 'ID') {echo "selected";}?> value="ID">Idaho</option>
<option <?php if($_POST['field_8'] === 'IL') {echo "selected";}?> value="IL">Illinois</option>
<option <?php if($_POST['field_8'] === 'IN') {echo "selected";}?> value="IN">Indiana</option>
<option <?php if($_POST['field_8'] === 'IA') {echo "selected";}?> value="IA">Iowa</option>
<option <?php if($_POST['field_8'] === 'KS') {echo "selected";}?> value="KS">Kansas</option>
<option <?php if($_POST['field_8'] === 'KY') {echo "selected";}?> value="KY">Kentucky</option>
<option <?php if($_POST['field_8'] === 'LA') {echo "selected";}?> value="LA">Louisiana</option>
<option <?php if($_POST['field_8'] === 'ME') {echo "selected";}?> value="ME">Maine</option>
<option <?php if($_POST['field_8'] === 'MD') {echo "selected";}?> value="MD">Maryland</option>
<option <?php if($_POST['field_8'] === 'MA') {echo "selected";}?> value="MA">Massachusetts</option>
<option <?php if($_POST['field_8'] === 'MI') {echo "selected";}?> value="MI">Michigan</option>
<option <?php if($_POST['field_8'] === 'MN') {echo "selected";}?> value="MN">Minnesota</option>
<option <?php if($_POST['field_8'] === 'MS') {echo "selected";}?> value="MS">Mississippi</option>
<option <?php if($_POST['field_8'] === 'MO') {echo "selected";}?> value="MO">Missouri</option>
<option <?php if($_POST['field_8'] === 'MT') {echo "selected";}?> value="MT">Montana</option>
<option <?php if($_POST['field_8'] === 'NE') {echo "selected";}?> value="NE">Nebraska</option>
<option <?php if($_POST['field_8'] === 'NV') {echo "selected";}?> value="NV">Nevada</option>
<option <?php if($_POST['field_8'] === 'NH') {echo "selected";}?> value="NH">New Hampshire</option>
<option <?php if($_POST['field_8'] === 'NJ') {echo "selected";}?> value="NJ">New Jersey</option>
<option <?php if($_POST['field_8'] === 'NM') {echo "selected";}?> value="NM">New Mexico</option>
<option <?php if($_POST['field_8'] === 'NY') {echo "selected";}?> value="NY">New York</option>
<option <?php if($_POST['field_8'] === 'NC') {echo "selected";}?> value="NC">North Carolina</option>
<option <?php if($_POST['field_8'] === 'ND') {echo "selected";}?> value="ND">North Dakota</option>
<option <?php if($_POST['field_8'] === 'OH') {echo "selected";}?> value="OH">Ohio</option>
<option <?php if($_POST['field_8'] === 'OK') {echo "selected";}?> value="OK">Oklahoma</option>
<option <?php if($_POST['field_8'] === 'OR') {echo "selected";}?> value="OR">Oregon</option>
<option <?php if($_POST['field_8'] === 'PA') {echo "selected";}?> value="PA">Pennsylvania</option>
<option <?php if($_POST['field_8'] === 'RI') {echo "selected";}?> value="RI">Rhode Island</option>
<option <?php if($_POST['field_8'] === 'SC') {echo "selected";}?> value="SC">South Carolina</option>
<option <?php if($_POST['field_8'] === 'SD') {echo "selected";}?> value="SD">South Dakota</option>
<option <?php if($_POST['field_8'] === 'TN') {echo "selected";}?> value="TN">Tennessee</option>
<option <?php if($_POST['field_8'] === 'TX') {echo "selected";}?> value="TX">Texas</option>
<option <?php if($_POST['field_8'] === 'UT') {echo "selected";}?> value="UT">Utah</option>
<option <?php if($_POST['field_8'] === 'VT') {echo "selected";}?> value="VT">Vermont</option>
<option <?php if($_POST['field_8'] === 'VA') {echo "selected";}?> value="VA">Virginia</option>
<option <?php if($_POST['field_8'] === 'WA') {echo "selected";}?> value="WA">Washington</option>
<option <?php if($_POST['field_8'] === 'WV') {echo "selected";}?> value="WV">West Virginia</option>
<option <?php if($_POST['field_8'] === 'WI') {echo "selected";}?> value="WI">Wisconsin</option>
<option <?php if($_POST['field_8'] === 'WY') {echo "selected";}?> value="WY">Wyoming</option>
</select>
</td></tr>
<td /><td>Country<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_9 value="<?php echo $field_9; ?>"></td>
<td>Zip<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_10 value="<?php echo $field_10; ?>"></td></tr>
<td /><td>Phone<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_11 value="<?php echo $field_11; ?>"></td>
<td>Fax</td><td><input type="text" Name=field_12 value="<?php echo $field_12; ?>"></td></tr>
<td /><td>User Name<span style="color : #ff0000;">*</span></td><td><input type="text" Name=field_13 value="<?php echo $field_13; ?>"></td>
<td>Password<span style="color : #ff0000;">*</span></td><td><input type="password" Name=field_14></td></tr>
<td /><td>Confirm Password<span style="color : #ff0000;">*</span></td><td><input type="password" Name=confirm></td></tr><TD />
<td colspan="4" align="center"><input type="submit" value="Submit" name="submit">
</form>
</table>
<?include("csvscript.php"); ?></center>
<td width="39" align="left" valign="top" style="background-image: url('../brighton.mi/images2/39pxbgright.jpg');"></td></tr>
<tr>
<td colspan="2" align="left" valign="top"><img src="../brighton.mi/images2/massage_banner.jpg" width="639" height="148" alt="Low prices every day for a 1 hour massage with no strings attached" border="0" usemap="#Map" /></td>
</tr>
</table>
</table>
<?
include("../brighton.mi/inc/footer.php");
?>
</body>
</html>
Code: Select all
<?
//function quote_encapse($str)
//{
// return '"'.$str.'"';
//}
if(isset ($_POST['submit'])) {
if(!$_POST['field_2']) echo('<span style="color : #ff0000;font-size : 22pt;line-height : 22pt;line-height : 22pt;">Alert: First Name field was blank.</span>');
elseif($_POST['field_1'] === 'NS') {
echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, choose a Social Title.</span>");
}
elseif(!$_POST['field_2']) echo('<span style="color : #ff0000;font-size : 22pt;line-height : 22pt;">Alert: First Name field was blank.</span>');
elseif(!$_POST['field_3']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Last Name field was blank.</span>");
elseif(!$_POST['field_5']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Email field was blank.</span>");
elseif(!preg_match("/@lavidamassage.com/i", $_POST['field_5'])) {
echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Email field is not a LaVida E-mail or is incorrect.</span>");
}
elseif(!$_POST['field_6']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Street Address field was blank.</span>");
elseif(!$_POST['field_7']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, City field was blank.</span>");
elseif($_POST['field_8'] === 'NS') {
echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Choose a State.</span>");
}
elseif(!$_POST['field_9']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Country field was blank.</span>");
elseif(!$_POST['field_10']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Zip field was blank.</span>");
elseif(!preg_match("/(\d+)/", $field_10)) {echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Zip field can not contain letters.</span>");}
elseif(strlen($field_10) != 5) echo ("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>The Zip Code is not 5 numbers in length.");
elseif(!$_POST['field_11']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Phone field was blank.</span>");
elseif(!preg_match("/(\d)\.(\d+)\.(\d+)/", $field_11)) {echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Phone field can not contain characters other than numbers and periods and must be in the format XXX.XXX.XXXX.</span>");}
elseif(strlen($field_11) != 12) { echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>The Phone field must be 12 characters long including 3 periods.");
}
elseif($_POST[field_12]){
$i = "1";
if(!preg_match("/(\d)\.(\d+)\.(\d+)/", $field_12)) {echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Fax field can not contain characters other than numbers and periods and must be in the format XXX.XXX.XXXX.</span>");
}
elseif(strlen($field_12) != 12) {echo ("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>The Fax field must be 12 characters long including 3 periods.</span>");
}
elseif(!$_POST['field_13']) {echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Username field was blank.</span>");
}
elseif(!$_POST['field_14']) {echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Password field was blank.</span>");
}
elseif($_POST['field_14'] != $_POST['confirm']) {
echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Passwords do not match.</span>");
;
}
}
elseif ($i != "1") {
if(!$_POST['field_13']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Username field was blank.</span>");
elseif(!$_POST['field_14']) echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Password field was blank.</span>");
elseif($_POST['field_14'] != $_POST['confirm']) {
echo("<span style='color : #ff0000;font-size : 22pt;line-height : 22pt;'>Alert: Hey {$_POST['field_2']}, Passwords do not match.</span>");
return false;
}
}
// Checkbox handling
$field_1_opts = $_POST['field_1'];
//". $_POST['field_1'][1]."
//". $_POST['field_1'][2];
// Saving $_POST['field_1'] and initializing array
$fileLine = array(0=>$field_1_opts);
$filename = 'newlavidauser.csv';
foreach($_POST as $name => $value) {
/* This is my lazy way of changing the code,
so I don't have to do a ton of typing
All it does is go through the POSTed variables
and if the name that is given has the word
"field_" in it, it will store it to the CSV file */
if($name == "field_1") {
// Just continue to the next one
// we already saved this one
continue;
}
if(strpos($name, "field_") > -1) {
$fileLine[] = $value;
}
}
// This will put quotes around our values
//$fileLine = array_map("quote_encapse", $fileLine);
// Turning fileLine from an array into a string
// separating each element with a comma
$fileLine = implode(",", $fileLine);
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w'))
{
echo "Could not open the database. Please contact the admin.";
exit;
}
if (fwrite($handle, $fileLine) === FALSE) {
// Security issue: do not expose writable filenames
// to the public, even if there's an error!
echo "Could not save. Please contact the admin.";
exit;
}
fclose($handle);
} else {
echo "The database is not available right now. Please contact the admin.";
}
if($_POST['confirm'] != "") {
//define the receiver of the email
//$to = 'Account Creation <info@printindy.com>';
$to = '"Account Creation Test" <tim@auctionittoday.com>';
//define the subject of the email
$subject = 'New LaVida Print Portal User';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: \"{$_POST['field_2']} {$_POST['field_3']}\" <{$_POST['field_5']}>\r\nReply-To: \"{$_POST['field_2']} {$_POST['field_3']}\" <{$_POST['field_5']}>";
//add boundary string and mime type specification
$headers .= "\r\nBcc: info@lavidamassage.com";
$headers .= "\r\nMIME-Version: 1.0";
$headers .= "\r\nContent-Type: multipart/related;\r\n type=\"multipart/alternative\";\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$headers .= "\r\nContent-Disposition: inline";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('newlavidauser.csv')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
This is a multi-part message in MIME format.
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative;
boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Attached you will find the csv data file for the newest LaVida Print portal user. Please reply to this e-mail when the account is created to let the new user know.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<p>Attached you will find the csv data file for the newest LaVida Print portal user. Please reply to this e-mail when the account is created to let the new user know.</p>
</body>
</html>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/csv;
name="newlavidauser.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="newlavidauser.csv"
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
if (mail($to,$subject,$message,$headers))
{
print("<span style='color : #008000;font-size : 14pt;'><p>The account submission was successfull. You will recieve an email once your account has been created.</p></span>");
}
else
{
print('<span style="color : #ff0000;font-size : 22pt;line-height : 22pt;"><p>There was an error submitting your account.</p></span>');
}
}
}
?>