PHP ATTACMENT PROBLEM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrelusive
Forum Newbie
Posts: 4
Joined: Fri Jun 24, 2011 10:56 am

PHP ATTACMENT PROBLEM

Post by mrelusive »

Hy everyone, i found lot of code in the internet for sending mail with attachment, but i not sucsed to make it to work.
Here is my code and if someone can help me i will bi thankefull to him.

<?php

$userName=$_POST["userName"];
$userEmail=$_POST["userEmail"];
$userComments=$_POST["userComments"];

$userAddress=$_POST["userAddress"];
$userPhone=$_POST["userPhone"];

$itemList=$_POST["itemList"];
$totalItems=$_POST["totalItems"];
$totalPrice=$_POST["totalPrice"];


$MailTo = "$userEmail, nidvanitri@msn.com";


$Subject = "Poruka";

$Body = "<font size=5><font color=#0066FF><b><u>Lista elemenata koriscenih u planeru</u>: </b></font></font><br><br><font size=2><B><font color=#0066FF>Od: </font></B>$userName<BR><font

size=2><B><font color=#0066FF>Address: </font></B>$userAddress<BR><B><font color=#0066FF>Telefon: </font></B>$userPhone<BR><font size=2><B><font color=#0066FF>Email: </font></B><A HREF=mailto:$userEmail>$userEmail</A><BR><BR><B><font color=#0066FF>Elementi: </font></B><BR>$itemList<BR><BR><BR><B><font color=#0066FF>Ukupno elemenata: </font></B>$totalItems<BR><B><font color=#0066FF>Ukupna cena: </font></B>$totalPrice<BR><BR><B><font color=#0066FF>Komentar korisnika: </font></B>$userComments<BR>";

$headers = "Content-type: text/html; charset=iso-8859-1\r\n";

$headers .= "From: Online planer";

$sendMail = mail($MailTo, "$Subject", "$Body", "$headers");

if(sendMail) {
echo ("&mailSent=USPELO!");}
else {
echo ("&mailSent=NEUSPELO!");}


?>
User avatar
RJGonzalez
Forum Newbie
Posts: 6
Joined: Fri Jun 24, 2011 10:49 pm
Location: Florida. USA

Re: PHP ATTACMENT PROBLEM

Post by RJGonzalez »

may be i am not seeing right but i don't see any code for attachment

here you can find a full file upload for free just for the visit. hope it works for what you need secureDOT149hostDOTcom/downloads.php
mrelusive
Forum Newbie
Posts: 4
Joined: Fri Jun 24, 2011 10:56 am

Re: PHP ATTACMENT PROBLEM

Post by mrelusive »

There is no code for attachment in this code.
I am looking if someone can help me to integrated attach in this code.
Because i found lot of code that is working and non make to work with this.
User avatar
RJGonzalez
Forum Newbie
Posts: 6
Joined: Fri Jun 24, 2011 10:49 pm
Location: Florida. USA

Re: PHP ATTACMENT PROBLEM

Post by RJGonzalez »

The file i am pointing you to does it all, all you need to do is download edit the line by replacing the existent email and replacing yours the getting the code from the html page and placing it where ever you want the form all attachments will be sent to you email with all the field on the form.

Visit my site and click on the live support button on the top right and will talk a bit.
User avatar
RJGonzalez
Forum Newbie
Posts: 6
Joined: Fri Jun 24, 2011 10:49 pm
Location: Florida. USA

Re: PHP ATTACMENT PROBLEM

Post by RJGonzalez »

or if hat you need is a simple code you can try this

Code: Select all

The HTML form with file upload box

The code for an HTML form with a file upload box is given below. User can click on the 'Browse' button to select the file from his/her local machine.
<form method="POST" name="email_form_with_php"
action="php-form-action.php" enctype="multipart/form-data">
 
<label for='name'>Name: </label>
<input type="text" name="name" >
 
<label for='email'>Email: </label>
<input type="text" name="email" >
 
<label for='message'>Message:</label>
<textarea name="message"></textarea>
 
<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="uploaded_file">
 
<input type="submit" value="Submit" name='submit'>
</form>

The form will look like this:

Please note that we have added:
"enctype="multipart/form-data"

while defining the <form> tag. This is to tell the browser that this form will be used to upload files. Then we have added the "name" and "email" fields to collect the user info. The third form field is the file upload box.
<input type="file" name="uploaded_file">

On hitting the "Submit" button, the form data along with the file data is posted to the script pointed to by the 'action' attribute of the form.
Getting the uploaded file in the PHP script

In the PHP script, we will first validate the submission and if the validation succeeds, we will send the submission by email.

We can access the uploaded file and its different attributes by using the $_FILES array. This array will contain the name, size, path and other attributes of the uploaded file. The code below gets the name, type and size of the uploaded file:
//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);
 
$size_of_uploaded_file =
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs

The code above is getting the different attributes of the uploaded file from the $_FILES[] array.
Validating the size and extension of the uploaded file

Suppose we don't want to allow files greater than the size of 100KB and we only want to allow image files to be uploaded. The validation code goes like this:
//Settings
$max_allowed_file_size = 100; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");
 
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
  $errors .= "\n Size of file should be less than $max_allowed_file_size";
}
 
//------ 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);
}

In the above code we are validating the file size and type. We have the maximum allowed file ($max_allowed_file_size) size set to 100KB. The $allowed_extensions array cotains the file extensions of all allowed file types.
The validation code checks to see whether the file extension matches any of the extensions in the $allowed_extensions array.

If there are errors found in the validation, the error is displayed. Else we proceed with sending the email.
Copy the uploaded file

Now, its time to send the uploaded file with the user message to the recipient's email address.

First of all we shall copy the file to a folder on the server.
//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';
  }
}

This code copies the uploaded file to the 'uploads' folder. You can change the uploads folder by updating $upload_folder.
Please make sure that "uploads" folder has "777" permissions.
Sending the Email

The next step is to compose and send the email. We will use the Pear library for composing and sending the email. ( see the Pear installation instructions below ) The pear classes PEAR::Mail and PEAR::Mail_Mime are used for sending the email with the attachment.

First, we need to include the pear library files for these classes.
include_once('Mail.php');
include_once('Mail_Mime/mime.php');

The code below composes and sends the email
$message = new Mail_mime();
 
$message->setTXTBody($text);
 
$message->addAttachment($path_of_uploaded_file);
 
$body = $message->get();
 
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
 
$headers = $message->headers($extraheaders);
 
$mail = Mail::factory("mail");
 
$mail->send($to, $headers, $body);

Mail_mime() class helps in composing a MIME message. In the code above, a Mail_mime object is created, the text body is updated ( $message->setTXTBody($text); ) and the attachment is added ( $message->addAttachment(file) )

The MIME encoded message is then sent using the Mail class.
mrelusive
Forum Newbie
Posts: 4
Joined: Fri Jun 24, 2011 10:56 am

Re: PHP ATTACMENT PROBLEM

Post by mrelusive »

I need code to be integrated into my code, because my code getting data from swf. And i need to add into my code attach file code. I dont need only code for php attach, i found lot of it on the web, i need someone who can integrated attach into my code to work.
mrelusive
Forum Newbie
Posts: 4
Joined: Fri Jun 24, 2011 10:56 am

Re: PHP ATTACMENT PROBLEM

Post by mrelusive »

Can anyone help?
Post Reply