12 Replies - 24038 Views - Last Post: 03 October 2007 - 02:58 PM Rate Topic: -----

#1 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

File Attach and email

Posted 27 September 2007 - 07:43 AM

I am using the following code to e-mail from my website:

<?php

// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "brottmayer@gmail.com";
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Name = Trim(stripslashes($_POST['Name'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$Website = Trim(stripslashes($_POST['Website'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.html\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Website: ";
$Body .= $Website;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_ok.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.html\">";
}

?>



I am trying to figure out how to attach a file along with this e-mail code. How can I do it. Thanks.

B.

Is This A Good Question/Topic? 0
  • +

Replies To: File Attach and email

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: File Attach and email

Posted 27 September 2007 - 11:54 AM

We get this question quite a bit here on the PHP forum because the PHP community hasn't made it really easy for sending mail attachments. The idea is that you have to create a "multipart" email by specifying it in the headers sent in the email. Then you encode the attachment and add it to the email. Sounds complicated huh? It isn't really when you see the script code, but it could be more simplified if they tried.

Below is a great site that shows you how to do this. Essentially you setup a boundary, add to the email header that it is multipart, encode the attachment into MIME base64 format, specify the content type as base64 and that it is an attachment and echo out the encrypted attachment.

Just walk through the code and if you have further questions, present your code and we can work out the bugs if need be. :)

PHP - Send email with attachment example code

Enjoy!
Was This Post Helpful? 0
  • +
  • -

#3 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

Re: File Attach and email

Posted 27 September 2007 - 10:43 PM

View PostMartyr2, on 27 Sep, 2007 - 11:54 AM, said:

We get this question quite a bit here on the PHP forum because the PHP community hasn't made it really easy for


I have the following code:

<?php
//define the receiver of the email
$to = 'mail@mail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>



But I am at a lost trying to implement the upload document as well as to include other data, that is inputted into a form on a web page. For example: I would like to ask the user for an address and phone number, birthdate and etc. How would i include those input boxes into the message of the e-mail? Thanks.

The Rock
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: File Attach and email

Posted 27 September 2007 - 10:57 PM

Whatever fields in your form is going to come to the PHP page as $_POST variables (like you see there in the example for "from") Then you just put those variables out in the body of the message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

Address is: <?php echo $_POST["address"]; ?>
Birthdate is: <?php echo $_POST["birthdate"]; ?>



See how I am adding in the address and birthdate fields into the body of the message? Check to see if there are values in the $_POST global array and then echo them out into the body or the header or wherever you want them to appear in the email.

It is simple as that. :)
Was This Post Helpful? 0
  • +
  • -

#5 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

Re: File Attach and email

Posted 28 September 2007 - 08:18 AM

View PostMartyr2, on 27 Sep, 2007 - 10:57 PM, said:

Whatever fields in your form is going to come to the PHP page as $_POST variables (like you see there in the example for "from") Then you just put those variables out in the body of the message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

Address is: <?php echo $_POST["address"]; ?>
Birthdate is: <?php echo $_POST["birthdate"]; ?>



See how I am adding in the address and birthdate fields into the body of the message? Check to see if there are values in the $_POST global array and then echo them out into the body or the header or wherever you want them to appear in the email.

It is simple as that. :)


I understand now, one last question and then I'll get out of your hair...The file attachment portion of the website you directed me to, I didn't understand how to use that coding to detect a file that is selected by the user. For example: the attachment that they were using was a set file...I want the user to select a file from their computer and be sent through the e-mail. Also, I am confused on how to make the input box with the browse button to have the user look for the file they want to upload. Any help with that would be great. Thanks.

The Rock
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: File Attach and email

Posted 28 September 2007 - 10:37 AM

When you create the page with the form on it, you will simply need to put in a file input element into the form. An example of this is below...

<input type="file" name="userfile"/>



This will create the classic textbox/browse button combo that you see in many web page forms. Now I want you to notice the use of name here as an attribute on that input form element. This name is going to be put into the $_FILES array for PHP access later. It acts very much like the $_POST array did, but give you values on the file.

$_FILES["userfile"]["name"]  // gives you the name of the file they uploaded
$_FILES["userfile"]["size"] // gives you the size of the upload in bytes
$_FILES["userfile"]["tmp_name"] // gives you the temporary name of the file on the server until it is renamed



Plus there are a few more. You can find out more information by looking up $_FILES on the php.net website. Notice in the code example how I am using "userfile" as the first key and then as a second key the various file attributes.

So in short if you define the file upload element in your form you will get the file/browse item which will handle the search and specify a file feature, then have code in your PHP to process the uploaded file using the $_FILES super array and you will be golden. Below is a link to help you understand file uploading in PHP as well...

W3Schools PHP file uploading demo

Good luck to you. :)
Was This Post Helpful? 0
  • +
  • -

#7 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

Re: File Attach and email

Posted 28 September 2007 - 10:57 PM

View PostMartyr2, on 28 Sep, 2007 - 10:37 AM, said:

Good luck to you. :)



Martyr2, I have the following code, but when I implemented it on my website to test it out, it says there is an error by where the file upload is at. Do I have it in the wrong place? Is there some more information that I need to have? Thanks for all of your help!

Here's the code:
<?php
//define the receiver of the email
$to = 'mail@mail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

Address is: <?php echo $_POST["address"]; ?>
Phone Number is: <?php echo $_POST["phone"]; ?>
Birthdate is: <?php echo $_POST["birthdate"]; ?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

<h1>Address is: <?php echo $_POST["address"]; ?></h1>
<p>Phone Number is: <?php echo $_POST["phone"]; ?><br />
Birthdate is: <?php echo $_POST["birthdate"]; ?></p>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();

$_FILES["userfile"]["name"]  // gives you the name of the file they uploaded
$_FILES["userfile"]["size"] // gives you the size of the upload in bytes
$_FILES["userfile"]["tmp_name"] // gives you the temporary name of the file on the server until it is renamed

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>



The Rock
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: File Attach and email

Posted 29 September 2007 - 07:38 PM

Well remember that $_FILES is an array and will RETURN information. So you need to store the values into variables and also remember in PHP lines are ended with a semicolon.

$strfilename = $_FILES["userfile"]["name"];



Notice how I set a variable with the value and end it all with a semicolon. Now the variable $strfilename has the name of the file they uploaded. Lookup $_FILES on php.net to get more examples of how it is used.

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#9 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

Re: File Attach and email

Posted 30 September 2007 - 08:16 AM

View PostMartyr2, on 29 Sep, 2007 - 07:38 PM, said:

Enjoy! :)



Martyr2,

This is what I have:

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
//define the receiver of the email
$to = 'mail@mail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$filename = $_FILES["userfile"]["name"];  // gives you the name of the file they uploaded
//$filesize = $_FILES["userfile"]["size"]; // gives you the size of the upload in bytes
$filetemp = $_FILES["userfile"]["tmp_name"]; // gives you the temporary name of the file on the server until it is renamed

if (is_uploaded_file($filename)) {
 // Read the file to be attached ('rb' = read binary)
 $file = fopen($filename,'rb');
 $data = fread($file,filesize($filename));
 fclose($file);

// Base64 encode the file data
$data = chunk_split(base64_encode($data));
  
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

Address is: <?php echo $_POST["address"]; ?>
Phone Number is: <?php echo $_POST["phone"]; ?>
Birthdate is: <?php echo $_POST["birthdate"]; ?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

<h1>Address is: <?php echo $_POST["address"]; ?></h1>
<p>Phone Number is: <?php echo $_POST["phone"]; ?><br />
Birthdate is: <?php echo $_POST["birthdate"]; ?></p>

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: {$filetype}; name={$filetemp} \n .
Content-Disposition: attachment;\n .
Content-Transfer-Encoding: base64\n\n . $data . \n\n .
--{$mime_boundary}--\n;

<?php echo $filename; ?>
--PHP-mixed-<?php echo $random_hash; ?>-- 
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
</body>
</html>



And I keep getting this error message, when i test it:
Parse error: parse error, unexpected $ in /hsphere/local/home/brottmay/brandonrottmayer.com/test/email.php on line 76.

But when using this coding:
<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
//define the receiver of the email
$to = 'mail@mail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$filename = $_FILES["userfile"]["name"];  // gives you the name of the file they uploaded
//$filesize = $_FILES["userfile"]["size"]; // gives you the size of the upload in bytes
$filetemp = $_FILES["userfile"]["tmp_name"]; // gives you the temporary name of the file on the server until it is renamed

if (is_uploaded_file($filename)) {
 // Read the file to be attached ('rb' = read binary)
 $file = fopen($filename,'rb');
 $data = fread($file,filesize($filename));
 fclose($file);

// Base64 encode the file data
$data = chunk_split(base64_encode($data));
  
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

Address is: <?php echo $_POST["address"]; ?>
Phone Number is: <?php echo $_POST["phone"]; ?>
Birthdate is: <?php echo $_POST["birthdate"]; ?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

<h1>Address is: <?php echo $_POST["address"]; ?></h1>
<p>Phone Number is: <?php echo $_POST["phone"]; ?><br />
Birthdate is: <?php echo $_POST["birthdate"]; ?></p>

--PHP-mixed-<?php echo $random_hash; ?>-- 
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
			"Content-Type: {$filetype};\n" .
			" name=\"{$filetemp}\"\n" .
			"Content-Disposition: attachment;\n" .
			" filename=\"{$filetemp}\"\n" .
			"Content-Transfer-Encoding: base64\n\n" .
			$data . "\n\n" .
			"--{$mime_boundary}--\n";
}

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
</body>
</html>



I receive the e-mail but I don't get the attachment, and it's missing the messages as well. What am I doing wrong in both of them? Thanks in advance for your help.

The Rock
Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: File Attach and email

Posted 30 September 2007 - 09:57 AM

Go back and read the site example again. NOTICE where they put the value $attachment variable. Also notice when you are IN and OUT of php mode because you are trying to use variables when you are not in PHP mode.

Sorry but I can't teach you php scripting. You asked about how to do attachments and I am expecting that you know the scripting basics when accomplishing an intermediate level task such as this.

:)
Was This Post Helpful? 0
  • +
  • -

#11 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

Re: File Attach and email

Posted 01 October 2007 - 08:02 AM

View PostMartyr2, on 30 Sep, 2007 - 09:57 AM, said:

:)


I went back and looked carefully at the example code again and came up with this:

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
//define the receiver of the email
$to = 'mail@mail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//Userfile information
$filename = $_FILES["userfile"]["name"];  // gives you the name of the file they uploaded
$filetype = $_FILES["userfile"]["type"]; // gives you the size of the upload in bytes
$filetemp = $_FILES["userfile"]["tmp_name"]; // gives you the temporary name of the file on the server until it is renamed
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents("$filename")));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

Address is: <?php echo $_POST["address"]; ?>
Phone Number is: <?php echo $_POST["phone"]; ?>
Birthdate is: <?php echo $_POST["birthdate"]; ?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

<h1>Address is: <?php echo $_POST["address"]; ?></h1>
<p>Phone Number is: <?php echo $_POST["phone"]; ?><br />
Birthdate is: <?php echo $_POST["birthdate"]; ?></p>

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: $filetype; name=$filename 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
</body>
</html>



When using this code, I was able to send the e-mail. It shows as if there is an attachment in the e-mail but when I go to open the e-mail, there is no attachment. Also, when I input the data for the required fields of address, phone number, birthdate, is doesn't show up in the email. Any help with this would be great.
Was This Post Helpful? 0
  • +
  • -

#12 The Rock  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 24-September 07

Re: File Attach and email

Posted 02 October 2007 - 10:01 PM

I am trying to send an e-mail with an attachment and I am clueless as to what might be the problem! I have the following code, that works to send the e-mail as well as show that there is an attachment, but the attachment is a blank file and not the one uploaded. How can I do this?

Here's the code:
<?php
//define the receiver of the email
$to = 'brottmayer@gmail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
//define the sender of the email
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//Userfile information
$filename = $_FILES["userfile"]["name"];  // gives you the name of the file they uploaded
$filetype = $_FILES["userfile"]["type"]; // gives you the size of the upload in bytes
$filetemp = $_FILES["userfile"]["tmp_name"]; // gives you the temporary name of the file on the server until it is renamed
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$data = base64_encode($filename);
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

Address is: <?php echo $_POST["address"]; ?>
Phone Number is: <?php echo $_POST["phone"]; ?>
Birthdate is: <?php echo $_POST["bdate"]; ?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

<h1>Address is: <?php echo $_POST["address"]; ?></h1>
<p>Phone Number is: <?php echo $_POST["phone"]; ?><br />
Birthdate is: <?php echo $_POST["bdate"]; ?></p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: $filetype; name=$filename
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $data; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>



The Rockl
Was This Post Helpful? 0
  • +
  • -

#13 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: File Attach and email

Posted 03 October 2007 - 02:58 PM

Well remember that you are attempting to get the file as it is on the server so you can read in the contents. That means that when the file is uploaded it is temporarily given a name on the server until it is renamed. So you will want to see a line like this...

$data = chunk_split(base64_encode(file_get_contents("$filetemp")));



Notice how I use $filetemp and not $filename. $filetemp is the current file name as it is on the server until it is renamed. Now with this line of code you may need to have $filetemp point to the full path on the server and not just the filename. The way it is now it is assuming that the temp file is in the same directory as this processing script. So either you will need to move the file after it is uploaded and before this script is processed or modify the line you see above to point to the location and temporary filename of the file.

So for instance, if the file is uploaded with the temporary name "dkhsd_x" and put in the directory /home/usr/Martyr2/web then you may need to have it specify....

$data = chunk_split(base64_encode(file_get_contents("/home/usr/Martyr2/web/" . $filetemp)));



Hopefully you understand what I am telling you.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1