will it be possible to use phpmailer send a attachment?

hello:

i'm using phpmailer to send a form message. and i want to have a attachemnt function. may some expert here have some suggestion? will it be possible to use phpmailer send a attachment?

Thank you so much for your advise!
lilyyanAsked:
Who is Participating?
 
psimationConnect With a Mentor Commented:
I just tested this with a small .gif file and it seems to work...
index.php
 
<form enctype="multipart/form-data" action="upload.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
 
=====================================================================
 
upload.php
 
<?php
  
require("class.phpmailer.php");
function sendmail($toaddress,$body,$upload){
$mail = new PHPMailer();
$mail->IsSMTP();                                      
$mail->Host = "your.smtp.server";  
$mail->SMTPAuth = true; 
$mail->Username = "mailbox_username";
$mail->Password = "mailbox_password";
$mail->From = "you@somewhere.dom";
$mail->FromName = "You";
$mail->AddAddress($toaddress);
$mail->AddReplyTo("you@your.dom", "You");
$mail->WordWrap = 50;                                 
$mail->IsHTML(true);                                  
$mail->Subject = "Subject Here";
$mail->Body = $body;
$mail->AddAttachment($upload);
$mail->Send();
}
$file = $_FILES['userfile']['tmp_name'];
$htmlbody = 'Email body'; 
$address = "test_address@to_check.if_it.works";
sendmail($address,$htmlbody,$file);
//print_r($_FILES);
?>

Open in new window

0
 
psimationConnect With a Mentor Commented:
Yes
You can even embed images.

Please have a look inside your PHPMailer folder ( after extracting the downloaded package) - in the phpdoc folder, go to index.html, and then click on PHPMailer under the Classes menu option on the left to see implementation of the attachment method.

There are also plenty of examples in the examples folder
0
 
lilyyanAuthor Commented:
thanks much for your reply. do i need load the file into a database?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
psimationCommented:
No, you should be able to link to it in the filesystem, ie, when you call the method, you simply include the pathname and filename.

It should however be possible to also do this for a file in a DB, but you would need to include extra stes to manage that
0
 
lilyyanAuthor Commented:
sorry, i guess i didn't say very clearly about my question...

i mean when user upload a file from the client side, where the file goes before it can be attached to the email?
0
 
psimationConnect With a Mentor Commented:
Usually, when you upload a file to the server, it lies in the temp folder accessible to the webserver. You should be able to then  link to it and attach it from there.

Have a look here to see examples of handling file uploads and then accessing them in their temp environment

http://www.php.net/manual/en/features.file-upload.php
0
 
lilyyanAuthor Commented:
welll, the host server I'm using is not allow person to write a drectory. could you suggest that in this situation, how to handle the attachment by phpmail? and i don't want to use database to store the file...

thank you very much!
0
 
psimationConnect With a Mentor Commented:
If you look closely at the file-upload example, you will notice that files get uploaded to the [temp] folder first, so you don't need to have a specific folder to store the uploaded files for what you are trying to do, you can read them straight from the temp folder area as per the example ( and then simply don't implement the steps where the files are renamed and copied into a specific folder - but pass them straight into PHPmailer's attachment method).

However, if your hosting provider has disabled PHP's file upload function altogether, then there is no way that I am aware of for you to get the files from a user's computer "into" your script...
0
 
lilyyanAuthor Commented:
the host server i'm using surport the file uploading, but not allow to move the file to anothor folder.

well, this is what i got when i try to file the temp file name:
Temp file: /tmp/phpTs2ETY

the code attached
----------------------------------------------
my question: i couldn't get a valid  temp. name, how could i attach the file in the email
<?php if ( ($_FILES["file"]["type"] == "application/pdf") && ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"]."<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    }
  }
else
  {
  echo "Invalid file";
  }?>

Open in new window

0
 
psimationCommented:
PS.
uncomment the print_r($_FILES) to see what the temp folder is on your server.

Then you might be able to use the move_uploaded_file function to re-write the filename to the "proper" name.

In my test, i got a PHP_3455.tmp file, which is expected, but a user will not know what to do with that...
Also, filenames with spaces seems to cause the script to NOT give me a tmp name...
0
 
lilyyanAuthor Commented:
Thank you very much ! ! It works so well !!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.