Link to home
Start Free TrialLog in
Avatar of Jerry Miller
Jerry MillerFlag for United States of America

asked on

Use php mailer for attachments

I answer a few questions on here, but am fairly green when it comes to php and email attachments so I need some advice. This is not an urgent problem, but needs to be implemented in the next few days as I promised the customer it would be up soon.

After a lot of looking around and limited success, I am attempting to use php mailer to send out an email on a customer's web site. I have the body formatted and it works fine, but I cannot get the attachments to work. It gives an error about not being able to find the file. Go Daddy (the customer's host) names the file some random characters (php4WvrtRE, for example) pre-pended to the actual file name. Could this be some of my issues.

Can someone point me in the right direction as to what variable name I should be using or have code that uses an attachment in php mailer? I have tried many different ways through out the last 48 hours without any good results. The file seems to be uploaded, as I have a routine that checks for it and it returns successful.

The php form is getting POST data from an HTML page and the user is supposed to be able to attach a document (resume, references etc). None of the data is being stored in MySQL or on the server except in a temp directory during the upload process.

I am POSTing all of the fields individually so I can print them into the body of the email that is being sent to their hiring manager. If there is a better way to do that, I am all ears as well.

Any help or advice in this will be appreciated.


<html>
<body>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="text-align: right;"><span style="font-size: 12pt; font-family: &quot;Times New Roman&quot;;"><img src="/Logo.gif" v:shapes="_x0000_i1025" height="110" width="132"></span></td>
<td style="text-align: center;"><big><big style="font-weight: bold;"><big style="font-weight: bold;"><small>LLC</small><br>
</big><small><small> Street,
#524 <br>
city, state <br>
phone 1 <br>
phone 2 (fax)</small></small></big><span style="font-weight: bold;"><small>
</small><br>
</span></big></td>
<td style="text-align: left;"><span style="font-size: 12pt; font-family: &quot;Times New Roman&quot;;"><img src="/Logo.gif" v:shapes="_x0000_i1025" height="110" width="132"></span></td>
</tr>
</tbody>
</table>
<?php


# Vital info
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$MiddleName = $_POST["MiddleName"];
$OtherNames = $_POST["OtherNames"];
$Address = $_POST["Address"];
$City = $_POST["City"];
$State = $_POST["State"];
$Zip = $_POST["Zip"];
$OutsideKY = $_POST["OutsideKY"];
$Where = $_POST["Where"];
$HomePhone = $_POST["HomePhone"];
$CellPhone = $_POST["CellPhone"];
$WorkPhone = $_POST["WorkPhone"];
$EmailAddress = $_POST["EmailAddress"];
$SSN = $_POST["SSN"];
$Unrestricted = $_POST["Unrestricted"];
$Convicted = $_POST["Convicted"];
$conditions = $_POST["conditions"];
$JobOpening = $_POST["JobOpening"];

# Education / Training
$HS = $_POST["HS"];
$HS_major = $_POST["HS_major"];
$College = $_POST["College"];
$College_major = $_POST["College_major"];
$Othertrain = $_POST["Othertrain"];
$Other_major = $_POST["Other_major"];

# Work Experience
# Company 1
$CompanyName = $_POST["CompanyName"];
$Address1= $_POST["Address1"];
$Start = $_POST["Start"];
$End = $_POST["End"];
$Wage = $_POST["Wage"];
$Supervisor = $_POST["Supervisor"];
$Contact = $_POST["Contact"];
$Responsibilities = $_POST["Responsibilites"];
$ReasonLeaving = $_POST["ReasonLeaving"];

# Company 2
$CompanyName2 = $_POST["CompanyName2"];
$Address2= $_POST["Address2"];
$Start2 = $_POST["Start2"];
$End2 = $_POST["End2"];
$Wage2 = $_POST["Wage2"];
$Supervisor2 = $_POST["Supervisor2"];
$Contact2 = $_POST["Contact2"];
$Responsibilities2 = $_POST["Responsibilites2"];
$ReasonLeaving2 = $_POST["ReasonLeaving2"];

# Company 3
$CompanyName3 = $_POST["CompanyName3"];
$Address3= $_POST["Address3"];
$Start3 = $_POST["Start3"];
$End3 = $_POST["End3"];
$Wage3 = $_POST["Wage3"];
$Supervisor3 = $_POST["Supervisor3"];
$Contact3 = $_POST["Contact3"];
$Responsibilities3 = $_POST["Responsibilites3"];
$ReasonLeaving3 = $_POST["ReasonLeaving3"];

# Experience
$Experience = $_POST["Experience"];
$Experience2 = $_POST["Experience2"];
$Experience3 = $_POST["Experience3"];

# References
# reference 1
$NameRef1 = $_POST["NameRef1"];
$RelationRef1 = $_POST["RelationRef1"];
$KnownRef1 = $_POST["KnownRef1"];
$AddressRef1 = $_POST["AddressRef1"];
$ContactsRef1 = $_POST["ContactsRef1"];

# reference 2
$NameRef2 = $_POST["NameRef2"];
$RelationRef2 = $_POST["RelationRef2"];
$KnownRef2 = $_POST["KnownRef2"];
$AddressRef2 = $_POST["AddressRef2"];
$ContactsRef2 = $_POST["ContactsRef2"];

# Availability
$Availability1 = $_POST["Availability1"];
$Availability2 = $_POST["Availability2"];
$Availability3 = $_POST["Availability3"];
$Availability4 = $_POST["Availability4"];

# Interests
$Interests1 = $_POST["Interests1"];
$Interests2 = $_POST["Interests2"];
$Interests3 = $_POST["Interests3"];

# EmergencyContacts
$EmergencyContacts1 = $_POST["EmergencyContacts1"];
$EmergencyContacts2 = $_POST["EmergencyContacts2"];
$EmergencyContacts3 = $_POST["EmergencyContacts3"];

# Signature
$Signature = $_POST["Signature"];
$CurrDate = $_POST["CurrentDate"];

# Processing message for reflecting back to a user for inspection
# Use same message in body of email to send to LLC
# needed to process each one individually so that extra newline characters could be added between sections

# Vital info
$message = nl2br("First Name = $FirstName\n"); 
$message = $message . nl2br("Last Name = $LastName\n");
$message = $message . nl2br("Middle Name = $MiddleName\n");
$message = $message . nl2br("Other Names = $OtherNames\n\n");
$message = $message . nl2br("Address = $Address\n");
$message = $message . nl2br("City = $City\n");
$message = $message . nl2br("State = $State\n");
$message = $message . nl2br("Zip Code = $Zip\n\n");
$message = $message . nl2br("Have you lived or worked outside of Kentucky in the last year? = $OutsideKY\n");
$message = $message . nl2br("If so, where? = $Where\n\n");
$message = $message . nl2br("Home Phone = $HomePhone\n");
$message = $message . nl2br("Cell Phone = $CellPhone\n");
$message = $message . nl2br("Work Phone = $WorkPhone\n");
$message = $message . nl2br("Email Address = $EmailAddress\n");
$message = $message . nl2br("Social Security # = $SSN\n\n");
$message = $message . nl2br("Are you a U.S. Citizen or otherwise authorized to work in the U.S. on an unrestricted basis? = $Unrestricted\n");
$message = $message . nl2br("Have you ever been convicted of a felony? = $Convicted\n");
$message = $message . nl2br("If so, please describe conditions: = $conditions\n");
$message = $message . nl2br("How did you learn about this job opening? = $JobOpening\n\n");
# Education / Training
$message = $message . nl2br("High School = $HS\n");
$message = $message . nl2br("Major/Degree = $HS_major\n");
$message = $message . nl2br("College = $College\n");
$message = $message . nl2br("Major/Degree = $College_major\n");
$message = $message . nl2br("Other Training = $Othertrain\n");
$message = $message . nl2br("Major/Degree = $Other_major\n\n");
# Company 1
$message = $message . nl2br("Company Name: = $CompanyName\n");
$message = $message . nl2br("Address: = $Address1\n");
$message = $message . nl2br("Date Started: = $Start\n");
$message = $message . nl2br("Date Ended: = $End\n");
$message = $message . nl2br("Wage: = $Wage\n");
$message = $message . nl2br("Name of Supervisor: = $Supervisor\n");
$message = $message . nl2br("May we contact? = $Contact\n");
$message = $message . nl2br("Responsibilites: = $Responsibilities\n");
$message = $message . nl2br("Reason for Leaving: = $ReasonLeaving\n\n");
# Company 2
$message = $message . nl2br("Company Name: = $CompanyName2\n");
$message = $message . nl2br("Address: = $Address2\n");
$message = $message . nl2br("Date Started: = $Start2\n");
$message = $message . nl2br("Date Ended: = $End2\n");
$message = $message . nl2br("Wage: = $Wage2\n");
$message = $message . nl2br("Name of Supervisor: = $Supervisor2\n");
$message = $message . nl2br("May we contact? = $Contact2\n");
$message = $message . nl2br("Responsibilites: = $Responsibilities2\n");
$message = $message . nl2br("Reason for Leaving: = $ReasonLeaving2\n\n");
# Company 3
$message = $message . nl2br("Company Name: = $CompanyName3\n");
$message = $message . nl2br("Address: = $Address3\n");
$message = $message . nl2br("Date Started: = $Start3\n");
$message = $message . nl2br("Date Ended: = $End3\n");
$message = $message . nl2br("Wage: = $Wage3\n");
$message = $message . nl2br("Name of Supervisor: = $Supervisor3\n");
$message = $message . nl2br("May we contact? = $Contact3\n");
$message = $message . nl2br("Responsibilites: = $Responsibilities3\n");
$message = $message . nl2br("Reason for Leaving: = $ReasonLeaving3\n\n");
# Experience
$message = $message . nl2br("Describe any work experience or credentials you have that relate to the position for which you are applying: = $Experience\n");
$message = $message . nl2br("Describe any personal experience that you choose to share that relates to the position for which you are applying: = $Experience2\n");
$message = $message . nl2br("Describe any skills, personal characteristics or other qualifications that relate to the position for which you are applying: = $Experience3\n\n");
# References
# reference 1
$message = $message . nl2br("Name: = $NameRef1\n");
$message = $message . nl2br("Relationship: = $RelationRef1\n");
$message = $message . nl2br("How long has this person known you? = $KnownRef1\n");
$message = $message . nl2br("Mailing address, including zip: = $AddressRef1\n");
$message = $message . nl2br("PhoneContact(s): = $ContactsRef1\n\n");
# reference 2
$message = $message . nl2br("Name: = $NameRef2\n");
$message = $message . nl2br("Relationship: = $RelationRef2\n");
$message = $message . nl2br("How long has this person known you? = $KnownRef2\n");
$message = $message . nl2br("Mailing address, including zip: = $AddressRef2\n");
$message = $message . nl2br("PhoneContact(s): = $ContactsRef2\n\n");
# Availability
$message = $message . nl2br("When can you start to work? = $Availability1\n");
$message = $message . nl2br("How many hours a week would you like to work? = $Availability2\n");
$message = $message . nl2br("When ARE you available to work (days and times)? = $Availability3\n");
$message = $message . nl2br("When are you NOT available to work (days and times)? = $Availability4\n\n");

# Interests
$message = $message . nl2br("What are your hobbies or favorite ways to spend time? = $Interests1\n");
$message = $message . nl2br("What do you think you are good at? = $Interests2\n");
$message = $message . nl2br("What organizations do you belong to or participate in? = $Interests2\n\n");

# EmergencyContacts
$message = $message . nl2br("Emergency Contact Name = $EmergencyContacts1\n");
$message = $message . nl2br("Emergency Contact Relationship = $EmergencyContacts2\n");
$message = $message . nl2br("Emergency Contact Phone Number(s): = $EmergencyContacts3\n\n");
$message = $message . nl2br("I certify that the facts set forth in this application for employment are true and complete to the best of my knowledge. I understand that if I am employed, false statements on this application shall be considered sufficient cause for dismissal. Company is hereby authorized to make any investigations of my prior educational and employment history.\n"); 
# Signature
$message = $message . nl2br("Signature = $Signature\n");
$message = $message . nl2br("Date = $CurrentDate\n");
echo nl2br("Please verify all of the information is correct / complete, then click the submit button at the bottom of the page\n\n");
#echo basename($_FILES["file_name"]["tmp_name"]);
#echo basename($_FILES["file_name"]["name"]);
echo $message;
$uploaddir = '/tmp/';
$uploadfile = $uploaddir . basename($_FILES['file_name']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_name']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
$filedata = array($_FILES["file_name"]);
#$filename = $_FILES["file_name"]["name"];
?>
<form method="post" action="<?php echo $PHP_SELF;?>" enctype="multipart/form-data">	
<?php
# Email parameters
if (isset($_POST['flag']) && $_POST['flag'] != "") {
#$filedata = $_FILES["file_name"]["tmp_name"];
#$filename = $_FILES["file_name"]["name"];
require("class.phpmailer.php");

$To = "je@insightbb.com";
$From = "me@jr.com";
$FromName = "LLC";
$Subject = "Employment Application";
$Message = $message;
$EmailAddress = $_POST["EmailAddress"];

$Mail = new PHPMailer();
  
  $Mail->From = $From;
  $Mail->FromName = $FromName;
  $Mail->AddAddress($To);
  #$Mail->AddAddress($EmailAddress);
  $Mail->AddReplyTo($From);
    # look for existence of the attachment
  #if(!empty($_FILES["file_name"]["name"])) {
	$Mail->AddAttachment($filedata,'Application/msword');
		#}
  
  $HTML = true;
  $Mail->WordWrap = 50; // set word wrap
  $Mail->IsHTML($HTML);
  
  $Mail->Subject  = $Subject;
  $Mail->Body = $Message;
  
  if($Mail->Send())
  {
   echo "Mail sent to LLC";
  }
  else
  {
    echo "Message Not Sent<br/>";
  }
}
 #mail($to,$subject,$message,$headers);
?>
<div style="text-align: center;">Click BACK to return to the online application, SUBMIT to email to Realizations LLC<br><br>
<input type=hidden name="flag" value="1">
<input value="Back" type="button"  onClick="history.go(-1);return true;"><br><br>
<input value="Submit" name="Submit" type="submit"><br>
</div>
</form>
</body>
</html>

Open in new window

Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

My first question would be , is the file you are trying to attach a large one?
ASKER CERTIFIED SOLUTION
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jerry Miller

ASKER

The file is only 27KB, I will have time to test the other thing a bit later.

Thanks,
Jerry
Hey, Jerry, this is a great question.

This may not be exactly what you want in your wheelhouse, but with several years of email experience, it is my above-all recommendation.  I started using broadcast email to keep in touch with clients before there was "spam" and I gave up on it in 2008.  I still send several thousand emails a week, but now I am doing it more and more with online services, like Constant Contact.  Here is why...

There are no real "standards" for email.  Any email reader gets to define its own HTML engine.  Think www-browser standards are loose?  Wait till you try to get HTML email to render the same way on Gmail, Yahoo, Hotmail, Outlook, Blackberry, iPhone, Droid, etc., etc.  You are in for a long and non-value-added task list.  It only grows with each new email program and each new handheld device.  No "fun" there, I assure you.

There are no real standards for "spam" prevention.  The SPF is a great idea, but without legal enforcement, I just don't see it becoming the standard it should be.  What this means to you is that your automated email messages may or may not get marked as spam by the recipients email reader program.  The only thing you don't know is when or why you will suddenly be branded a spammer.  Your client will expect you to debug this, at no charge, and to hold them harmless for your actions.

Why choose Constant Contact (or others - there are good competitors)?  Because they have full time employees who deal with all the email issues, so you do not have to.  They have APIs to help you use their service.  They are embarrassingly inexpensive.  They are reliable.  They comply with the law (don't laugh - the USA anti-spam laws can get you sent to prison for what might seem like a simple mistake).  In short, they do the work that you really do not want to do, even if you do not know what that work is yet.

Hope that helps bring some perspective from the 50,000 foot level, ~Ray
Thanks for the comments Ray.

I don't think that this would work for my particular situation. I am only creating a web form where people can submit an employment application and attach a resume (references, etc). It is not overly complicated as I have found a lot of great examples online, but I am struggling with getting the attachment part correct.

galexander07, I will test your syntax change in a couple of hours (or less).

Thanks galexander07 for pointing me in the right direction. I was using an incorrect variable, but also I was doing something weird as well.

If I try to use a submit button on the page, it changes the variable name and cannot find it (of course). If I let it use the data from the original HTTP POST, it works fine with the following line.

$Mail->AddAttachment($path,$name,'base64','Application/msword');

I have some more work to do, but will ask it in another question.
I was calling the variable incorrectly and it resulting in the "file not found" error