In the documentation it looks easy on how to send an attachment, but for some reason I am getting errors. I get the following errors whenever I attach a file.
Code: Select all
Fatal error:
Uncaught Error of type [swift_fileexception] with message [No such file 'test_test.pdf']
@0 swift_file::swift_file() in /home/newherr/public_html/careers/index.php on line 154
@1 swift_file::setpath() in /home/newherr/public_html/core/classes/Swift/File.php on line 52
in /home/newherr/public_html/core/classes/Swift/Errors.php on line 99Below is my script that processes a form which has an option to attach an file. I know the errors have something to do with the pathway of the attached file, but not sure what the correct path is suppose to be. Any help is greatly appreciated.
Code: Select all
if(isset($_POST['employment_name'], $_POST['employment_contact_email'], $_POST['email'], $_POST['first_name'], $_POST['last_name'], $_POST['title'], $_POST['company'], $_POST['address_1'],
$_POST['address_2'], $_POST['city'], $_POST['state'], $_POST['zip'], $_POST['phone'], $_POST['salary_request'], $_POST['comments'])){
// Posted from hidden form
$employment_name = htmlentities(stripslashes($_POST['employment_name']));
$employment_contact_email = htmlentities(stripslashes($_POST['employment_contact_email']));
// Posted from user
$email = htmlentities(stripslashes(trim($_POST['email'])));
$first_name = htmlentities(stripslashes(trim($_POST['first_name'])));
$last_name = htmlentities(stripslashes(trim($_POST['last_name'])));
$title = htmlentities(stripslashes(trim($_POST['title'])));
$company = htmlentities(stripslashes(trim($_POST['company'])));
$address_1 = htmlentities(stripslashes(trim($_POST['address_1'])));
$address_2 = htmlentities(stripslashes(trim($_POST['address_2'])));
$city = htmlentities(stripslashes(trim($_POST['city'])));
$state = htmlentities(stripslashes(trim($_POST['state'])));
$zip = htmlentities(stripslashes(trim($_POST['zip'])));
$phone = htmlentities(stripslashes(trim($_POST['phone'])));
$salary_request = htmlentities(stripslashes(trim($_POST['salary_request'])));
$comments = htmlentities(stripslashes(trim($_POST['comments'])));
require_once $_SERVER['DOCUMENT_ROOT'].'/core/classes/Validator.php';
/// Validate Required fields
$validator = new Validator();
$validator->validateGeneral($first_name, 'Please Enter Your First Name.');
$validator->validateGeneral($last_name, 'Please Enter Your Last Name.');
$validator->validateGeneral($address_1, 'Please Enter Your Address.');
$validator->validateGeneral($city, 'Please Enter Your City.');
$validator->validateGeneral($state, 'Please Enter Your State.');
$validator->validateGeneral($zip, 'Please Enter Your Zip.');
$validator->validateEmail($email, 'Please Enter a Valid Email Address.');
$validator->validateEmail($employment_contact_email, 'Please Enter a Valid Email Address.');
// Check for Valid Phone Number if field is not empty
if(!empty($phone)) {
$validator->validatePhoneNumber($phone, 'Please Enter a Valid Phone Number.');
}
if(isset($_FILES['fileatt']['tmp_name']) && !empty($_FILES['fileatt']['tmp_name'])){
// File Attachment
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
// Set the array of valid file extensions
$allowed_file_types = array("application/msword" => "doc", "application/pdf" => "pdf", "text/plain" => "txt");
// Check the filetype supplied
if (!array_key_exists(strtolower($fileatt_type), $allowed_file_types)) {
$validator->validateGeneral(false, 'You have attached an invalid File Type. Please attach only Microsoft Word documents, .txt or .PDF files.');
}
}
if ($validator->foundErrors()){
$content = '<span class="errors"><strong>Please address the following:</strong><br /> '.$validator->listErrors("<br />"). '</span><br /><br />';
$content .= 'Please Go Back to Previous Page to Fix the problems.<br /><br />';
$content .= '<a href="javascript:history.back(-1);">«Back to Previous Page</a>';
$page = new Page();
$page->SetTitle($employment_name);
$page->SetSubheader($employment_name);
$page->SetMainContent($content);
$page->SetPrinterFriendly(isset($_GET['printable']));
$page->DisplayOutput();
exit();
}
$email_message = "I am interested in the following position: ".$employment_name."\r\n\r\n";
$email_message .= "Name: ".$first_name." ".$last_name."\r\n\r\n";
$email_message .= "Title: ".$title."\r\n\r\n";
$email_message .= "Company: ".$company."\r\n\r\n";
$email_message .= "Address Line 1: ".$address_1."\r\n\r\n";
$email_message .= "Address Line 2: ".$address_2."\r\n\r\n";
$email_message .= "City: ".$city."\r\n\r\n";
$email_message .= "State: ".$state."\r\n\r\n";
$email_message .= "Zip: ".$zip."\r\n\r\n";
$email_message .= "Phone: ".$phone."\r\n\r\n";
$email_message .= "Salary Request: ".$salary_request."\r\n\r\n";
$email_message .= "Comments: ".$comments."\r\n\r\n";
require_once $_SERVER['DOCUMENT_ROOT'].'/core/classes/Swift.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/core/classes/Swift/Connection/SMTP.php';
// Start Swift
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
$subject = "Application for ".$employment_name;
$from = $email;
// Create the message
$message =& new Swift_Message($subject);
$message->attach(new Swift_Message_Part($email_message));
// Start a new list
$recipients =& new Swift_RecipientList();
$recipients->addTo($employment_contact_email);
// $recipients->addTo("lisa-smith@domain.tld"); Uncomment to add another recipient
if(isset($fileatt) && !empty($fileatt)){
// Use the Swift_File class
$message->attach(new Swift_Message_Attachment(new Swift_File($fileatt_name), $fileatt_name, $fileatt_type));
}
if($swift->send($message, $recipients, $from)){
$title = "Application for ".$employment_name." Sent";
$content = "Thank You ".$first_name." ".$last_name."<br />\n";
$content .= "We will get in touch with you as soon as possible.\n";
}
else {
$title = "Sorry";
$content = "There was an Error with your application. Please try again.\n";
}
$page = new Page();
$page->SetTitle($title);
$page->SetSubheader($title);
$page->SetMainContent($content);
$page->SetPrinterFriendly(isset($_GET['printable']));
$page->DisplayOutput();