Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

will it be possible to use phpmailer send a attachment?

Posted on 2008-11-13
11
Medium Priority
?
592 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:lilyyan
  • 6
  • 5
11 Comments
 
LVL 17

Assisted Solution

by:psimation
psimation earned 2000 total points
ID: 22949602
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
 

Author Comment

by:lilyyan
ID: 22949977
thanks much for your reply. do i need load the file into a database?
0
 
LVL 17

Expert Comment

by:psimation
ID: 22950673
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lilyyan
ID: 22951198
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
 
LVL 17

Assisted Solution

by:psimation
psimation earned 2000 total points
ID: 22951928
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
 

Author Comment

by:lilyyan
ID: 22963701
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
 
LVL 17

Assisted Solution

by:psimation
psimation earned 2000 total points
ID: 22963916
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
 

Author Comment

by:lilyyan
ID: 22963973
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
 
LVL 17

Accepted Solution

by:
psimation earned 2000 total points
ID: 22964156
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
 
LVL 17

Expert Comment

by:psimation
ID: 22964199
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
 

Author Comment

by:lilyyan
ID: 22969266
Thank you very much ! ! It works so well !!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question