PHP mail function - sending attachments
10-19-2007, 12:11 PM
|
PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
Hello,
What I am currently working on is a web form that emails me the info submitted, along with an attached file, and writes the info to the database (minus the attachment, im not getting that deep yet)
The code is a combination of a mail script I have been using for a while, and a tutorial I found online. Im having trouble combining the two. My first problem is probably something stupid, though im sure there will be more.
Parse error: syntax error, unexpected T_STRING in /home/sliquidc/public_html/thenannygroup/apply.php on line 96
96 is the mail($to_email, $subject ....... line - and also part of the code from the tutorial.
PHP Code:
<?php
function sendMail() { * if (!isset ($_POST['agreement'])) { //Oops, you did not agree to our terms * * die ("<p>Oops!* You did not agree to our Terms of Employment! Click on the back arrow to go back</p>"); * } * else { * *
$from_name = stripslashes($_POST['from_name']); $from_email = stripslashes($_POST['from_email']); $to_email = stripslashes($_POST['to_email']); $firstname = stripslashes($_POST['firstname']); $lastname = stripslashes($_POST['lastname']); $phone = stripslashes($_POST['phone']); $fax = stripslashes($_POST['fax']); $address = stripslashes($_POST['address']); $address2 = stripslashes($_POST['address2']); $state = stripslashes($_POST['state']); $city = stripslashes($_POST['city']); $zip = stripslashes($_POST['zip']); $email = stripslashes($_POST['email']); $available = stripslashes($_POST['available']); $livein = stripslashes($_POST['livein']); $overnights = stripslashes($_POST['overnights']); $travel = stripslashes($_POST['travel']); $fulltime = stripslashes($_POST['fulltime']); $parttime = stripslashes($_POST['parttime']); $comment = stripslashes($_POST['comment']); $agreement = stripslashes($_POST['agreement']); $resume = $_FILES['resume']['tmp_name']; $resume_name = $_FILES['resume']['name']; if (is_uploaded_file($resume)) { //Do we have a file uploaded? $fp = fopen($resume, "rb"); //Open it $data = fread($fp, filesize($resume)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can be emailed fclose($fp); }
//Let's start our headers ini_set(SMTP, "mail.thenannygroup.com"); ini_set(smtp_port, 25); ini_set(sendmail_from, "web@thenannygroup.com"); * * $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 .= "<strong>TheNannyGroup.com</strong> - You have a new nanny applicant!<br><br> <strong>Personal Information</strong><br><br> First Name = {$firstname} Last Name = {$lastname} Phone = {$phone} Fax = {$fax} Address = {$address} Unit = {$address2} State = {$state} City = {$city} Zip = {$zip} Email = {<a href=\"mailto:$email\">$email</a>}<br><br><br> <strong>Employment Information</strong><br><br> Date Available = {$available} Live In = {$livein} Overnights = {$overnights} Travel = {$travel} Full Time = {$fulltime} Part Time = {$parttime}<br><br><br> <strong>Other Information</strong><br><br> Comment = {$comment}\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=\"" . $resume_name . "\"\n"; * * $message .= "Content-Transfer-Encoding: base64\n"; * * $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $resume_name . "\"\n\n"; * * $message .= $data; //The base64 encoded message * * $message .= "\n"; * * $message .= "------=MIME_BOUNDRY_main_message--\n" //Send the message mail("to_name<$to_email>", $subject, $message, $headers); print "Mail Sent. Thank you for your interest."; } } Enter_New_Entry($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement);
function Enter_New_Entry ($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement) {
$cnx = mysql_connect("localhost","**********","**********"); * * if (!$cnx) { * * * * die('Could not connect: ' . mysql_error()); * * }
$db_selected = mysql_select_db('**********', $cnx); if (!$db_selected) { * * die ('Can\'t use Records Database : ' . mysql_error()); }
* * $SQL_Exec_String = "Insert Into ********* (firstname, lastname, phone, fax, address, address2, state, city, zip, email, available, livein, overnights, travel, fulltime, parttime, comment, agreement) * * * * * * Values ('$firstname', '$lastname', '$phone', '$fax', '$address', '$address2', '$state', '$city', '$zip', '$email', '$available', '$livein', '$overnights', '$travel', '$fulltime', '$parttime', '$comment', '$agreement')";
* * $cur = mysql_query($SQL_Exec_String); * * if (!$cur) { * * $message* = 'Invalid query: ' . mysql_error() . "\n"; * * $message .= 'Whole query: ' . $query; * * die($message); }
mysql_close($cnx);* header("location: http://www.thenannygroup.com/verify.php");
}
?>
I thought it was just unescaped quotes on line 96, but i tried to escape them, and it made no difference.
you guys see anything, or have a better direction to point me in?
Thanks
Croix
Last edited by croix; 10-30-2007 at 03:12 PM..
|
|
|
|
10-19-2007, 12:40 PM
|
Re: PHP mail function - sending attachments
|
Posts: 34
Name: DesignersForHire
Location: India
|
This can be because of the header info is not correct
|
|
|
|
10-19-2007, 12:43 PM
|
Re: PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
anything in there catch your eye? The header info is from the tutorial i was working with.
it said that the header info was critical when sending an attachment, and that was the working headers that were posted.
|
|
|
|
10-19-2007, 08:43 PM
|
Re: PHP mail function - sending attachments
|
Posts: 88
Name: programmer
Location: internet
|
Croix,
You forgot a semi-colon at the end of line 93. Anytime you get an unexpected t-string, just look at the previous few lines above your error and 90% of the time its a forgotten semi-colon.
Regards.
|
|
|
|
10-30-2007, 03:01 PM
|
Re: PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
Inet - solved that problem. Thank you.
However, now when I visit the page, it instantly redirects me to the page listed in my header, which should not until the form is submitted.
here is the updated code:
PHP Code:
<?php
function sendMail() { if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy! die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>"); } else { $from_name = stripslashes($_POST['from_name']); $from_email = stripslashes($_POST['from_email']); $to_name = stripslashes($_POST['to_name']); $to_email = stripslashes($_POST['to_email']); $firstname = stripslashes($_POST['firstname']); $lastname = stripslashes($_POST['lastname']); $phone = stripslashes($_POST['phone']); $fax = stripslashes($_POST['fax']); $address = stripslashes($_POST['address']); $address2 = stripslashes($_POST['address2']); $state = stripslashes($_POST['state']); $city = stripslashes($_POST['city']); $zip = stripslashes($_POST['zip']); $email = stripslashes($_POST['email']); $available = stripslashes($_POST['available']); $livein = stripslashes($_POST['livein']); $overnights = stripslashes($_POST['overnights']); $travel = stripslashes($_POST['travel']); $fulltime = stripslashes($_POST['fulltime']); $parttime = stripslashes($_POST['parttime']); $comment = stripslashes($_POST['comment']); $agreement = stripslashes($_POST['agreement']); $resume = $_FILES['resume']['tmp_name']; $resume_name = $_FILES['resume']['name']; if (is_uploaded_file($resume)) { //Do we have a file uploaded? $fp = fopen($resume, "rb"); //Open it $data = fread($fp, filesize($resume)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can be emailed fclose($fp); }
//Let's start our headers ini_set(SMTP, "mail.thenannygroup.com"); ini_set(smtp_port, 25); ini_set(sendmail_from, "web@thenannygroup.com"); $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 .= "TheNannyGroup.com - You have a new nanny applicant! Personal Information First Name = {$firstname} Last Name = {$lastname} Phone = {$phone} Fax = {$fax} Address = {$address} Unit = {$address2} State = {$state} City = {$city} Zip = {$zip} Email = {$email} Employment Information Date Available = {$available} Live In = {$livein} Overnights = {$overnights} Travel = {$travel} Full Time = {$fulltime} Part Time = {$parttime} Other Information Comment = {$comment}\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=\"" . $resume_name . "\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $resume_name . "\"\n\n"; $message .= $data; //The base64 encoded message $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; //Send the message mail("to_name<$to_email>", $subject, $message, $headers); echo "Mail Sent. Thank you for your interest."; } } Enter_New_Entry($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement);
function Enter_New_Entry ($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement) {
$cnx = mysql_connect("localhost","**********","**********"); if (!$cnx) { die('Could not connect: ' . mysql_error()); }
$db_selected = mysql_select_db('*********', $cnx); if (!$db_selected) { die ('Can\'t use Records Database : ' . mysql_error()); }
$SQL_Exec_String = "Insert Into ******** (firstname, lastname, phone, fax, address, address2, state, city, zip, email, available, livein, overnights, travel, fulltime, parttime, comment, agreement) Values ('$firstname', '$lastname', '$phone', '$fax', '$address', '$address2', '$state', '$city', '$zip', '$email', '$available', '$livein', '$overnights', '$travel', '$fulltime', '$parttime', '$comment', '$agreement')";
$cur = mysql_query($SQL_Exec_String); if (!$cur) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); }
mysql_close($cnx); header("location: http://www.thenannygroup.com/verify.php");
}
?>
Last edited by croix; 10-30-2007 at 03:15 PM..
|
|
|
|
10-31-2007, 06:38 AM
|
Re: PHP mail function - sending attachments
|
Posts: 5
Name: Alex
|
Hi Croix,
You've forgot to call sendMail() function. You code now just puts the data into DataBase thru the Enter_New_Entry function redirects you to verify.php. And if you have posted the whole code it will not work because you are setting $firstname, $lastname variables in sendMail() function.
This is how you code should looks like:
PHP Code:
<?
//// functions ///////////////////////////////////////
function sendMail() { if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy! die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>"); } else { $from_name = stripslashes($_POST['from_name']); $from_email = stripslashes($_POST['from_email']); $to_name = stripslashes($_POST['to_name']); $to_email = stripslashes($_POST['to_email']); $firstname = stripslashes($_POST['firstname']); $lastname = stripslashes($_POST['lastname']); $phone = stripslashes($_POST['phone']); $fax = stripslashes($_POST['fax']); $address = stripslashes($_POST['address']); $address2 = stripslashes($_POST['address2']); $state = stripslashes($_POST['state']); $city = stripslashes($_POST['city']); $zip = stripslashes($_POST['zip']); $email = stripslashes($_POST['email']); $available = stripslashes($_POST['available']); $livein = stripslashes($_POST['livein']); $overnights = stripslashes($_POST['overnights']); $travel = stripslashes($_POST['travel']); $fulltime = stripslashes($_POST['fulltime']); $parttime = stripslashes($_POST['parttime']); $comment = stripslashes($_POST['comment']); $agreement = stripslashes($_POST['agreement']); $resume = $_FILES['resume']['tmp_name']; $resume_name = $_FILES['resume']['name']; if (is_uploaded_file($resume)) { //Do we have a file uploaded? $fp = fopen($resume, "rb"); //Open it $data = fread($fp, filesize($resume)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can be emailed fclose($fp); }
//Let's start our headers ini_set(SMTP, "mail.thenannygroup.com"); ini_set(smtp_port, 25); ini_set(sendmail_from, "web@thenannygroup.com"); $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 .= "TheNannyGroup.com - You have a new nanny applicant! Personal Information First Name = {$firstname} Last Name = {$lastname} Phone = {$phone} Fax = {$fax} Address = {$address} Unit = {$address2} State = {$state} City = {$city} Zip = {$zip} Email = {$email} Employment Information Date Available = {$available} Live In = {$livein} Overnights = {$overnights} Travel = {$travel} Full Time = {$fulltime} Part Time = {$parttime} Other Information Comment = {$comment}\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=\"" . $resume_name . "\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $resume_name . "\"\n\n"; $message .= $data; //The base64 encoded message $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; //Send the message mail("to_name<$to_email>", $subject, $message, $headers); echo "Mail Sent. Thank you for your interest.";
//--> adding entry to the DB on successful Mail Send Enter_New_Entry($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement); } }
function Enter_New_Entry ($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement) {
$cnx = mysql_connect("localhost","**********","**********"); if (!$cnx) { die('Could not connect: ' . mysql_error()); }
$db_selected = mysql_select_db('*********', $cnx); if (!$db_selected) { die ('Can\'t use Records Database : ' . mysql_error()); }
$SQL_Exec_String = "Insert Into ******** (firstname, lastname, phone, fax, address, address2, state, city, zip, email, available, livein, overnights, travel, fulltime, parttime, comment, agreement) Values ('$firstname', '$lastname', '$phone', '$fax', '$address', '$address2', '$state', '$city', '$zip', '$email', '$available', '$livein', '$overnights', '$travel', '$fulltime', '$parttime', '$comment', '$agreement')";
$cur = mysql_query($SQL_Exec_String); if (!$cur) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); }
mysql_close($cnx); header("location: http://www.thenannygroup.com/verify.php");
}
//// code ///////////////////////////////////////
sendMail();
?>
|
|
|
|
10-31-2007, 01:38 PM
|
Re: PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
thanks for the reply neo...
the updated code instantly shows me the error that the email address is not there, i.e.
Oops! You forgot to fill out the email address! Click on the back arrow to go back
http://thenannygroup.com/apply_inprogress3.php
not sure what you mean about setting the firstname etc in the sendmail... I have posted all my code, except the form... here it is.
PHP Code:
<form action="http://www.thenannygroup.com/apply_inprogress3.php" method="post"> <input name="from_name" type="hidden" id="from_name" value="The Nanny Group" /> <input name="from_email" type="hidden" id="from_email" value="web@thenannygroup.com" /> <input name="to_name" type="hidden" id="to_name" value="The Nanny Group" /> <input name="to_email" type="hidden" id="to_email" value="applicants@thenannygroup.com" /> <fieldset><legend>Contact Information</legend><br> <label for="firstname">First Name</label> <input name="firstname" type="text" id="firstname" /> <label for="lastname">Last Name</label> <input name="lastname" type="text" id="lastname" /> <label for="phone">Phone</label> <input name="phone" type="text" id="phone" /> <label for="fax">Fax</label> <input name="fax" type="text" id="fax" value="optional"/> <label for="address">Address</label> <input name="address" type="text" id="address" /> <label for="address2">Unit</label> <input name="address2" type="text" id="address2" value="optional"/> <label for="state">State</label> <select name="state" id="state" /> <option>My State</option> <option>Puerto Rico</option> <option>Alabama</option> <option>Alaska</option> <option>Arizona</option> <option>Arkansas</option> <option>California</option> <option>Colorado</option> <option>Conneticut</option> <option>etc</option> </select> <label for="city">City</label><input name="city" type="text" id="city" /> <label for="zip">Zip</label><input name="zip" type="text" id="zip" size="10" /> <label for="email">Email</label><input name="email" type="text" id="email" /> </fieldset> <fieldset> <legend>Employment Information</legend> <label for="company">Date Available</label> <input name="available" type="text" id="available" /> <label for="resume">Resume</label><input name="resume" type="file" id="resume" /> <label for="livein">Live In</label><input name="livein" type="checkbox" id="livein" value="yes" /> <label for="overnights">Overnights</label><input name="overnights" type="checkbox" id="overnights" value="yes" /> <label for="travel">Travel</label><input name="travel" type="checkbox" id="travel" value="yes" /> <label for="fulltime">Full Time</label><input name="fulltime" type="checkbox" id="fulltime" value="yes" /> <label for="parttime">Part Time</label><input name="parttime" type="checkbox" id="parttime" value="yes" /> </fieldset> <fieldset><legend>Additional Information</legend><br> <label for="comment"><div align="center">Questions <br>or<br> Comments</div></label><textarea name="comment" cols="50" rows="4" id="comment">optional</textarea> <img src="http://www.webmaster-talk.com/images/spacer.gif" width="25" height="5" /><input name="agreement" type="checkbox" id="agreement" value="yes" /><img src="http://www.webmaster-talk.com/images/spacer.gif" width="5" height="5" />I agree to the <a href="terms.php" target="_blank">Terms of Employment</a> <p class="submit"><input type="submit" value="Submit" /><p class="reset"><input type="reset" value="Reset" /></p> </form>
Last edited by croix; 10-31-2007 at 01:45 PM..
|
|
|
|
10-31-2007, 02:54 PM
|
Re: PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
OK, so i made a few changes, and now the page shows up, and i can fill out the form, and when I submit, it takes me to the page in my header, like it should, IF i comment out the die line. If it stays in, then the error shows up on the page instantly
Also, the information is added to my database properly.
However, I do not receive the email
updated code:
PHP Code:
<?
//// functions ///////////////////////////////////////
function sendMail() { if (!isset ($_POST['agreement'])) { //Oops, you did not agree to our terms die ("<p>Oops! You did not agree to our terms! Click on the back arrow to go back</p>"); } else { $from_name = stripslashes($_POST['from_name']); $from_email = stripslashes($_POST['from_email']); $to_name = stripslashes($_POST['to_name']); $to_email = stripslashes($_POST['to_email']); $firstname = stripslashes($_POST['firstname']); $lastname = stripslashes($_POST['lastname']); $phone = stripslashes($_POST['phone']); $fax = stripslashes($_POST['fax']); $address = stripslashes($_POST['address']); $address2 = stripslashes($_POST['address2']); $state = stripslashes($_POST['state']); $city = stripslashes($_POST['city']); $zip = stripslashes($_POST['zip']); $email = stripslashes($_POST['email']); $available = stripslashes($_POST['available']); $livein = stripslashes($_POST['livein']); $overnights = stripslashes($_POST['overnights']); $travel = stripslashes($_POST['travel']); $fulltime = stripslashes($_POST['fulltime']); $parttime = stripslashes($_POST['parttime']); $comment = stripslashes($_POST['comment']); $agreement = stripslashes($_POST['agreement']); $resume = $_FILES['resume']['tmp_name']; $resume_name = $_FILES['resume']['name']; if (is_uploaded_file($resume)) { //Do we have a file uploaded? $fp = fopen($resume, "rb"); //Open it $data = fread($fp, filesize($resume)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can be emailed fclose($fp); }
//Let's start our headers ini_set(SMTP, "mail.thenannygroup.com"); ini_set(smtp_port, 25); ini_set(sendmail_from, "web@thenannygroup.com"); $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 .= "TheNannyGroup.com - You have a new nanny applicant! Personal Information First Name = {$firstname} Last Name = {$lastname} Phone = {$phone} Fax = {$fax} Address = {$address} Unit = {$address2} State = {$state} City = {$city} Zip = {$zip} Email = {$email} Employment Information Date Available = {$available} Live In = {$livein} Overnights = {$overnights} Travel = {$travel} Full Time = {$fulltime} Part Time = {$parttime} Other Information Comment = {$comment}\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=\"" . $resume_name . "\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $resume_name . "\"\n\n"; $message .= $data; //The base64 encoded message $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; //Send the message mail("to_name<$to_email>", $subject, $message, $headers); echo "Mail Sent. Thank you for your interest.";
//--> adding entry to the DB on successful Mail Send Enter_New_Entry($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement); } }
function Enter_New_Entry ($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement) {
$cnx = mysql_connect("localhost","**********","**********"); if (!$cnx) { die('Could not connect: ' . mysql_error()); }
$db_selected = mysql_select_db('**********', $cnx); if (!$db_selected) { die ('Can\'t use Records Database : ' . mysql_error()); }
$SQL_Exec_String = "Insert Into ********** (firstname, lastname, phone, fax, address, address2, state, city, zip, email, available, livein, overnights, travel, fulltime, parttime, comment, agreement) Values ('$firstname', '$lastname', '$phone', '$fax', '$address', '$address2', '$state', '$city', '$zip', '$email', '$available', '$livein', '$overnights', '$travel', '$fulltime', '$parttime', '$comment', '$agreement')";
$cur = mysql_query($SQL_Exec_String); if (!$cur) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); }
mysql_close($cnx); header("location: http://www.thenannygroup.com/verify.php");
}
//// code ///////////////////////////////////////
sendMail();
?>
Last edited by croix; 10-31-2007 at 02:57 PM..
|
|
|
|
10-31-2007, 05:38 PM
|
Re: PHP mail function - sending attachments
|
Posts: 522
Name: Gabe Solomon
Location: Romania
|
my recomandation is to use a php class to send the e-mail
phpmailer
__________________
If you like my posts ... TK is appreciated:)
Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
11-01-2007, 06:00 AM
|
Re: PHP mail function - sending attachments
|
Posts: 5
Name: Alex
|
Croix, what error appears on the page?
And solomongaby, is right. It's better to use phpmailer if you want to send more complex emails. Maybe mail() function fails because of SMTP server you are trying to use. Ini_set for SMTP, smtp_port and sendmail_from is for windows only and your site is seems to be running on Linux. Try to comment these lines.
|
|
|
|
11-01-2007, 12:03 PM
|
Re: PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
UPDATE
The code is slightly different now, and now the problem i am having is a different one. Now, the page displays, and when I submit it, the information is written to the database properly. I also receive an email, but there is no text in the email, and there is a blank 0Kb mime-type file attached. Here is a screen shot of the email.
and here's the code:
PHP Code:
<?
//// functions ///////////////////////////////////////
function sendMail() {
if (!isset ($_POST['agreement'])) { //Oops, you did not agree to our terms
die ("<p>Oops! You did not agree to our terms! Click on the back arrow to go back</p>");
}
else {
$from_name = stripslashes($_POST['from_name']);
$from_email = stripslashes($_POST['from_email']);
$to_name = stripslashes($_POST['to_name']);
$to_email = stripslashes($_POST['to_email']);
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$phone = stripslashes($_POST['phone']);
$fax = stripslashes($_POST['fax']);
$address = stripslashes($_POST['address']);
$address2 = stripslashes($_POST['address2']);
$state = stripslashes($_POST['state']);
$city = stripslashes($_POST['city']);
$zip = stripslashes($_POST['zip']);
$email = stripslashes($_POST['email']);
$available = stripslashes($_POST['available']);
$livein = stripslashes($_POST['livein']);
$overnights = stripslashes($_POST['overnights']);
$travel = stripslashes($_POST['travel']);
$fulltime = stripslashes($_POST['fulltime']);
$parttime = stripslashes($_POST['parttime']);
$comment = stripslashes($_POST['comment']);
$agreement = stripslashes($_POST['agreement']);
$resume = $_FILES['resume']['tmp_name'];
$resume_name = $_FILES['resume']['name'];
if (is_uploaded_file($resume)) { //Do we have a file uploaded?
$fp = fopen($resume, "rb"); //Open it
$data = fread($fp, filesize($resume)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can be emailed
fclose($fp);
}
//Let's start our headers
/* ini_set(SMTP, "mail.thenannygroup.com");
ini_set(smtp_port, 25);
ini_set(sendmail_from, "web@thenannygroup.com"); */
$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 .= "TheNannyGroup.com - You have a new nanny applicant!
Personal Information
First Name = {$firstname}
Last Name = {$lastname}
Phone = {$phone}
Fax = {$fax}
Address = {$address}
Unit = {$address2}
State = {$state}
City = {$city}
Zip = {$zip}
Email = {$email}
Employment Information
Date Available = {$available}
Live In = {$livein}
Overnights = {$overnights}
Travel = {$travel}
Full Time = {$fulltime}
Part Time = {$parttime}
Other Information
Comment = {$comment}\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=\"" . $resume_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $resume_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
//Send the message
mail("to_name<$to_email>", $subject, $message, $headers);
echo "Mail Sent. Thank you for your interest.";
//--> adding entry to the DB on successful Mail Send
Enter_New_Entry($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement);
}
}
function Enter_New_Entry
($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement) {
$cnx = mysql_connect("localhost","*********","*********");
if (!$cnx) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('**********', $cnx);
if (!$db_selected) {
die ('Can\'t use Records Database : ' . mysql_error());
}
$SQL_Exec_String = "Insert Into ********* (firstname, lastname, phone, fax, address, address2, state, city, zip, email, available, livein, overnights, travel, fulltime, parttime, comment, agreement)
Values ('$firstname', '$lastname', '$phone', '$fax', '$address', '$address2', '$state', '$city', '$zip', '$email', '$available', '$livein', '$overnights', '$travel', '$fulltime', '$parttime', '$comment', '$agreement')";
$cur = mysql_query($SQL_Exec_String);
if (!$cur) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
mysql_close($cnx);
header("location: http://www.thenannygroup.com/verify.php");
}
//// code ///////////////////////////////////////
if (!empty($_POST)) { sendMail(); }
?>
|
|
|
|
11-01-2007, 02:48 PM
|
Re: PHP mail function - sending attachments
|
Posts: 87
Name: Colin Roy
Location: Dallas, TX
|
OK, i have created another version to try and implement the phpmailer class. you might be able to tell, I dont really know what im doing with this.
from the phpmailer site, i learn that I have to have access to the include directory, or tell the script where to find it.
I dont think i have access to the include directory, so I uploaded the folder to my server, and here is what my code looks like.
I cant get past this error right now:
Warning: require(class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in /home/******/public_html/thenannygroup/apply_inprogress4.php on line 5
Fatal error: require() [function.require]: Failed opening required 'class.phpmailer.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/******/public_html/thenannygroup/apply_inprogress4.php on line 5
PHP Code:
<?
$mail->PluginDir = "/home/******/public_html/thenannygroup/phpmailer"; require("class.phpmailer.php");
//// functions ///////////////////////////////////////
if (isset($_POST['agreement'])) { $from_name = stripslashes($_POST['from_name']); $from_email = stripslashes($_POST['from_email']); $to_name = stripslashes($_POST['to_name']); $to_email = stripslashes($_POST['to_email']); $firstname = stripslashes($_POST['firstname']); $lastname = stripslashes($_POST['lastname']); $phone = stripslashes($_POST['phone']); $fax = stripslashes($_POST['fax']); $address = stripslashes($_POST['address']); $address2 = stripslashes($_POST['address2']); $state = stripslashes($_POST['state']); $city = stripslashes($_POST['city']); $zip = stripslashes($_POST['zip']); $email = stripslashes($_POST['email']); $available = stripslashes($_POST['available']); $livein = stripslashes($_POST['livein']); $overnights = stripslashes($_POST['overnights']); $travel = stripslashes($_POST['travel']); $fulltime = stripslashes($_POST['fulltime']); $parttime = stripslashes($_POST['parttime']); $comment = stripslashes($_POST['comment']); $agreement = stripslashes($_POST['agreement']); $resume = $_FILES['resume']['tmp_name']; $resume_name = $_FILES['resume']['name']; if (is_uploaded_file($resume)) { //Do we have a file uploaded? $fp = fopen($resume, "rb"); //Open it $data = fread($fp, filesize($resume)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can be emailed fclose($fp); }
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */ $message = "TheNannyGroup.com - You have a new nanny applicant! Personal Information First Name = {$firstname} Last Name = {$lastname} Phone = {$phone} Fax = {$fax} Address = {$address} Unit = {$address2} State = {$state} City = {$city} Zip = {$zip} Email = {$email} Employment Information Date Available = {$available} Live In = {$livein} Overnights = {$overnights} Travel = {$travel} Full Time = {$fulltime} Part Time = {$parttime} Other Information Comment = {$comment}\n"; $mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP $mail->Host = "smtp.thenannygroup.com"; // SMTP servers $mail->SMTPAuth = false; // turn on SMTP authentication // $mail->Username = "jswan"; // SMTP username // $mail->Password = "secret"; // SMTP password
$mail->From = "web@thenannygroup.com"; $mail->FromName = "TNG.com"; $mail->AddAddress("applicants@thenannygroup.com","The Nanny Group"); // $mail->AddAddress("ellen@site.com"); // optional name $mail->AddReplyTo("web@thenannygroup.com","TNG.com");
$mail->WordWrap = 50; // set word wrap $mail->AddAttachment("{$resume}"); // attachment // $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); $mail->IsHTML(true); // send as HTML
$mail->Subject = "New Nanny Applicant"; $mail->Body = "{$message}"; // $mail->AltBody = "This is the text-only body";
if(!$mail->Send()) { echo "Message was not sent <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; }
echo "Message has been sent";
//--> adding entry to the DB on successful Mail Send Enter_New_Entry($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement); }
function Enter_New_Entry ($firstname,$lastname,$phone,$fax,$address,$address2,$state,$city,$zip,$email,$available,$livein,$overnights,$travel,$fulltime,$parttime,$comment,$agreement) {
$cnx = mysql_connect("localhost","*********","*********"); if (!$cnx) { die('Could not connect: ' . mysql_error()); }
$db_selected = mysql_select_db('*********', $cnx); if (!$db_selected) { die ('Can\'t use Records Database : ' . mysql_error()); }
$SQL_Exec_String = "Insert Into ******** (firstname, lastname, phone, fax, address, address2, state, city, zip, email, available, livein, overnights, travel, fulltime, parttime, comment, agreement) Values ('$firstname', '$lastname', '$phone', '$fax', '$address', '$address2', '$state', '$city', '$zip', '$email', '$available', '$livein', '$overnights', '$travel', '$fulltime', '$parttime', '$comment', '$agreement')";
$cur = mysql_query($SQL_Exec_String); if (!$cur) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); }
mysql_close($cnx); header("location: http://www.thenannygroup.com/verify.php");
}
//// code ///////////////////////////////////////
// if (!empty($_POST)) { sendMail(); }
?>
Last edited by croix; 11-01-2007 at 02:57 PM..
|
|
|
|
|
« Reply to PHP mail function - sending attachments
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|