Jump to content

code help for attaching uploaded file to email


zinch33

Recommended Posts

Hello

 

I'm completely new with php and need some help. Basically I'm at a level that I can tweak it but I'm still learning on how to write it.

 

I have a form within an html page. This form has multiple text inputs and a upload button. Once the user hits the submit button it triggers the mailer.php.

 

I've done tons of searches online today and in forums and people keep mentioning php mailer as a good tool. I looked into this and was confused on how to implement it.

 

I don't need the info sent to a database, just emailed. The uploaded file just needs to be sent to a temp directory and then deleted after the file is emailed. The inputs would be in the body of the email and the uploaded file would be attached. This is the php code I'm working with for a starting point.

 

Any help would be greatly appreciated.

 

<?PHP 
$to = "email@email.com"; 
$subject = "New Employee Application";
$headers = "From:" . $from;
$forward = 1;
$location = "";


$date = date ("l, F jS, Y");

$redirect = "../thanks.html";

$msg = "New Employee Application. It was submitted on $date.\n\n"; 

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    foreach ($_POST as $key => $value) { 
        $msg .= ucfirst ($key) ." : ". $value . "\n"; 
    }
}
else {
    foreach ($_GET as $key => $value) { 
        $msg .= ucfirst ($key) ." : ". $value . "\n"; 
    }
}

mail($to, $subject, $msg, $headers); 
if ($forward == 1) { 
    header ("Location: $redirect"); 
} 
else { 
    echo "Your application has been sent.";
} 

?> 

 

Link to comment
Share on other sites

So I looked into php mailer. But I'm having trouble putting all the pieces together.

 

The email comes through but I need help coding for the attachment.

I have users uploading a resume, however I won't know the name or file type(pdf or doc). Plus I don't know what directory the file goes once it's uploaded. Do I need to tell it in my html where to go so I can use the specific code below?

 

This is the new code.

 

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsMail();

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com";

$mail->AddAddress("test@test.com");
$mail->Subject = "Test 1";

$mail->IsHTML(true);
$mail->AddAttachment('tmp/test.pdf');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AltBody="To view the message, please use an HTML compatible email viewer!";

if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
   echo "Letter sent";
}
?> 

Link to comment
Share on other sites

Checked your link. It was slightly confusing since I'm a novice at php.

 

Will this help with finding the file that is uploaded by the user and attaching it to the sent email?

 

Not sure where to go with this.

 

for ($i = 0; $i <= 2; $i++)
{
    if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) {
        $mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']);
    }
}

Link to comment
Share on other sites

Yes it's what I'm looking for. I've been looking at php code too long and it's starting to really confuse me. I'm just getting into php and trying to learn it.

 

I tried adding this code to my existing code and I'm still not getting an attachment. Am I placing the code in the correct spot in the script or am I missing something? I have a tmp directory that's 777 that's in the root folder.

 

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsMail();

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com";

$mail->AddAddress("myemail@yahoo.com");
$mail->Subject = "Test 1";

$location = "";
$redirect = "../thanks.html";

$sender_firstname = $_POST['firstname'];
$sender_middlename = $_POST['middlename'];
$sender_lastname = $_POST['lastname'];
$sender_streetaddress = $_POST['streetaddress'];
$sender_apartment = $_POST['apartment'];
$sender_city = $_POST['city'];
$sender_state = $_POST['state'];
$sender_zipcode = $_POST['zipcode'];
$sender_homenumber = $_POST['homenumber'];
$sender_cellnumber = $_POST['cellnumber'];
$sender_email = $_POST['email'];


$mail->IsHTML(true);

for ($i = 0; $i <= 2; $i++)
{
    if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) {
        $mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']);
    }
}
$mail->Body = "New Employee Application <br />\r\n
<br />
Personal Information <br />\r\n
Name: $sender_firstname <br />\r\n
Address: $sender_streetaddress <br />\r\n 
Apartment: $sender_apartment <br />\r\n
City: $sender_city <br />\r\n
State: $sender_state <br />\r\n
Zip: $sender_zipcode <br />\r\n
Home Phone: $sender_homenumber <br />\r\n
Cell Phone: $sender_cellnumber <br />\r\n  
Email: $sender_email <br />\r\n
<br />";
$mail->AltBody="To view the message, please use an HTML compatible email viewer!";

if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
   header ("Location: $redirect"); 
}
?>

Link to comment
Share on other sites

Ok, so I have added code to move the file to the uploads folder. However I'm still getting no attachment. I ran print_r($mail->GetAttachments()); to check to see if it's getting attached but it's returning Array ( )

Warning: Cannot modify header information - headers already sent by (output started at /home/content/37/8916437/html/php/mailer.php:6) in /home/content/37/8916437/html/php/mailer.php on line 176.

 

Here's my latest code.

 

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsMail();

print_r($mail->GetAttachments());

//Settings 
$mail->From="form@company.com";
$mail->FromName="Company Site";
$mail->Sender="form@company.com";
$mail->AddAddress("myemail@yahoo.com");
$mail->Subject = "Test";

$location = "";
$redirect = "../thanks.html";

$allowed_extensions = array("pdf", "doc", "docx");
$upload_folder = "../uploads";

//Get the uploaded file information
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);

//get the file extension of the file
$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);

//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
  if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
  {
    $allowed_ext = true;
  }
}
if(!$allowed_ext)
{
  $errors .= "\n The uploaded file is not supported file type. ".
  " Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
}

//Settings For Inputs
$sender_firstname = $_POST['firstname'];
$sender_middlename = $_POST['middlename'];
$sender_lastname = $_POST['lastname'];
$sender_streetaddress = $_POST['streetaddress'];

$mail->IsHTML(true);

$mail->addAttachment($path_of_uploaded_file);

$mail->Body = "<h1>Form</h1> <br />\r\n
    <u>Personal Information</u> <br />\r\n
    <b>Full Name:</b> $sender_firstname $sender_middlename $sender_lastname <br />\r\n
    <b>Address:</b> $sender_streetaddress <br />\r\n 
    <br />";
$mail->AltBody="To view the message, please use an HTML compatible email viewer!";

if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
   header ("Location: $redirect"); 
}

?> 

Link to comment
Share on other sites

i believe this will solve through PHP and javascript your output buffering problems with the headers and still alllow you to redirect.

 

 

<?php
function redirect($pURL) {
if (strlen($pURL) > 0) {
if (headers_sent()) {
echo "document.location.href='".$pURL."';\n"; }
else {
header("Location: " . $pURL);
}
exit();
}
}
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsMail();

$mail->From="mailer@example.com";
$mail->FromName="My site's mailer";
$mail->Sender="mailer@example.com";

$mail->AddAddress("myemail@yahoo.com");
$mail->Subject = "Test 1";

$location = "";


$sender_firstname = $_POST['firstname'];
$sender_middlename = $_POST['middlename'];
$sender_lastname = $_POST['lastname'];
$sender_streetaddress = $_POST['streetaddress'];
$sender_apartment = $_POST['apartment'];
$sender_city = $_POST['city'];
$sender_state = $_POST['state'];
$sender_zipcode = $_POST['zipcode'];
$sender_homenumber = $_POST['homenumber'];
$sender_cellnumber = $_POST['cellnumber'];
$sender_email = $_POST['email'];


$mail->IsHTML(true);

for ($i = 0; $i <= 2; $i++)
{
    if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) {
        $mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']);
    }
}
$mail->Body = "New Employee Application <br />\r\n
<br />
Personal Information <br />\r\n
Name: $sender_firstname <br />\r\n
Address: $sender_streetaddress <br />\r\n 
Apartment: $sender_apartment <br />\r\n
City: $sender_city <br />\r\n
State: $sender_state <br />\r\n
Zip: $sender_zipcode <br />\r\n
Home Phone: $sender_homenumber <br />\r\n
Cell Phone: $sender_cellnumber <br />\r\n  
Email: $sender_email <br />\r\n
<br />";
$mail->AltBody="To view the message, please use an HTML compatible email viewer!";

if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
   redirect('../thanks.html');
}
?>

Link to comment
Share on other sites

Still banging my head on this one.

 

I looked through the link you supplied as well as the code and tweaked my code. I still can't get anything attached to the email. When I submit via form I've checked my uploads folder and nothing is there. There must be a problem pulling it from the temp folder but I'm not sure what I'm dong wrong. Too new with PHP to troubleshoot this on my own.

 

I also moved the print to after I the file is attached. But now I'm getting this Array ( ) document.location.href='../thanks.html';

 

Here is my latest code:

 

<?php
function redirect($pURL) {
if (strlen($pURL) > 0) {
if (headers_sent()) {
echo "document.location.href='".$pURL."';\n"; }
else {
header("Location: " . $pURL);
}
exit();
}
}
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsMail();

//Settings 
$mail->From="form@company.com";
$mail->FromName="Company Site";
$mail->Sender="form@company.com";
$mail->AddAddress("myemail@yahoo.com");
$mail->Subject = "Test";

$location = "";

$allowed_extensions = array("pdf", "doc", "docx");
$upload_folder = "../uploads"; //<-- this folder must be writeable by the script

//Get the uploaded file information
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);

//get the file extension of the file
$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);

//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
  if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
  {
    $allowed_ext = true;
  }
}
if(!$allowed_ext)
{
  $errors .= "\n The uploaded file is not supported file type. ".
  " Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
}

//Settings For Inputs
$sender_firstname = $_POST['firstname'];
$sender_middlename = $_POST['middlename'];
$sender_lastname = $_POST['lastname'];
$sender_streetaddress = $_POST['streetaddress'];
$sender_apartment = $_POST['apartment'];
$sender_city = $_POST['city'];
$sender_state = $_POST['state'];
$sender_zipcode = $_POST['zipcode'];
$sender_homenumber = $_POST['homenumber'];


$mail->IsHTML(true);

$mail->addAttachment($path_of_uploaded_file);

print_r($mail->GetAttachments());

$mail->Body = "<h1>Test</h1> <br />\r\n
<u>Personal Information</u> <br />\r\n
<b>Full Name:</b> $sender_firstname $sender_middlename $sender_lastname <br />\r\n
<b>Address:</b> $sender_streetaddress <br />\r\n 
<b>Apartment:</b> $sender_apartment <br />\r\n
<b>City:</b> $sender_city <br />\r\n
<b>State:</b> $sender_state <br />\r\n
<b>Zip:</b> $sender_zipcode <br />\r\n
<b>Home Phone:</b> $sender_homenumber <br />\r\n
<br />";
$mail->AltBody="To view the message, please use an HTML compatible email viewer!";


if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
   redirect('../thanks.html');
}

?>

Link to comment
Share on other sites

I turned on errors and this is what I get:

 

Notice: Undefined index: uploaded_file in /home/content/37/8916437/html/php/mailer_employee.php on line 33

 

line 33 in my script is:

$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);

 

Notice: Undefined variable: errors in /home/content/37/8916437/html/php/mailer_employee.php on line 50

 

line 50 is:

  " Only the following file types are supported: ".implode(',',$allowed_extensions);

 

Notice: Undefined index: uploaded_file in /home/content/37/8916437/html/php/mailer_employee.php on line 55

document.location.href='../thanks.html';

 

line 55 is:

$tmp_path = $_FILES["uploaded_file"]["tmp_name"];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.