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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

psimationCommented:
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
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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
psimationCommented:
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
psimationCommented:
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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.