PHP mailer Script help

That code is for debugging, although it is useful when build mail messages as well.

The array is here, [php]echo " $_FILES: "[/php] not the second portion.

Dan, you had an extra period after the file name: $_FILES[$file_name . ][“type”] ; ?>
And, you took it out, but you also took out the dollar sign. Try:
[php]
<?php echo " $_FILES: " . $_FILES[$file_name]["type"] ; ?>
[/php]
Should work!

( Astonecipher, Dan needed the variables anyways for his other code, so I was just trying to teach him how
to debug his outputs from his forms. Didn’t want to confuse him using the more global arrays. But, your
code for displaying the $_POST array is nice and hopefully, Dan will understand it for future debugging! )

[php]<?php echo " $_FILES: " . $_FILES[$file_name]["type"] ; ?>[/php]
This still presents an issue however. If you are using $_FILES as a label, then you don’t want to treat it as a variable, which double quotes will do. Otherwise, it will show Array, not $_FILES.

This: $_FILES[$file_name] also holds the value, not the key.

Yes, Astonecipher, I was not really reading that part, it should be just:
[php]

<?php echo " FILE-TYPE: " . $_FILES[$file_name]["type"] ; ?>

[/php]
It was just so he can see if he is getting the correct filetype from the file that was posted…

[php
echo “

”;
print_r( $_FILES );
echo “
”;
[/php]

This will output everything stored in the $_FILES array.

Hi Ernie, I tried the above code but all it outputs is;-

“FILE-TYPE:”

my code [php]<?php echo "http://$server_name/fms/documents/uploads/$file_name"; ?>[/php] echos the correct file location.

thanks guys for your constant help on this issue

So, what does this display:

[php]
$real_file = “http://$server_name/fms/documents/uploads/” . $file_name;
echo " FILE-TYPE: " . $_FILES[$real_file][“type”] ;
[/php]

This should display the file type…

It displays the same as previous “FILE-TYPE:” and that is all?!

Well, this only seems to work if the filename is from a posted form that contains the “FILE” type of input
field… I did a little quick research for you and found another way to do this. This is a much better way as
it is the up-to-date way. I tested it on my server using a file already there and it works perfectly.
Sorry for not finding it earlier for you! You would take this output which is the exact filetype and place that
inside your email for the content type…

[php]
$file_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $server_name . “/fms/documents/uploads/” . $file_name)
echo $file_type;
[/php]

One further note on the $_FILES version, I found out you can not place the HTTP:// into the file name as it
creates three slashes like HTTP:///…server name… And, that throws an error, but, it is well hidden and that
might be why it shows nothing. But, this way works much better for you!

::cough:: reply #11

Yes, Astonecipher! The karma goes to you… I was just trying to use his code… Thanks…

Hi ErnieAlex, could you post the full code you used as I am still getting a blank result?!

My code is;-
[php]<?php
if (isset($_POST[‘submit’]))
{
$file_name = $_POST[‘file_name’];
$doc_email = $_POST[‘doc_email’];
$server_name = $_SERVER[‘SERVER_NAME’];
$doc_message = $_POST[‘doc_message’];
$file_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $server_name . “/fms/documents/uploads/” . $file_name);
$username = $_POST[‘username’];
$company = $_POST[‘company’];
}
?>


<?php echo "http://$server_name/fms/documents/uploads/$file_name"; ?>
<?php echo $file_type; ?>
[/php]

karma is goes to both of you guys!

Well, I put a test file on my server and used this code to view the extension of it…
[php]

<?php if (isset($_POST['submit'])) { $file_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), "test.php"); echo "
" . $file_name . "
"; echo $file_type; } ?>
            <form action="" method="post">
            <input type="file" name="file_name" />
            <input type="submit" name="submit" />
            </form>

[/php]
I did also place a small form with a submit button on it to press to start this code working…
It displays this info: text/x-php ( Which is correct for a standard PHP file… ) NOTE that this does not
even use the $_FILES[] array. It just displays the file type of that one file. In your code, you would have
to make sure the file was uploaded first and then use this type of code to grab the file-type…

Hope that helps…

Thanks Ernie, I’ve used your code and created a file “test.php” file within the same directory. Ran the code and nothing… :frowning:

I then moved both files (mailer.php & test.php) into the directory “fms” rather than “fms/documents/” and the code works and out puts “text/x-php” - I suspect this is why my script isn’t functioning properly, do you know why it is doing this as I want the script to work but it causes more issues with moving directories etc.

thanks for your help

What are the permissions for that directory?

Just checked and both folders “fms” and “fms/documents/” are sest to 755

any ideas guys? I’m looking at using this script elsewhere once its working.

thanks

Well, Dan, the code I gave you was just to point to a file and tell you the filetype of it.

You must remember that if you are using a “posted” $_FILES[] array, that is a LOCAL file not a web file.
Therefore, it is not really ready for use on your website. Normally, you would copy the file with the PHP’s
“movefile” function to your folder. Then, you can use the routine to access the file. You can change the
code I gave you to move the file which means it will then be uploaded to the server. When moving the
file to your uploads folder, you tell it what you want the file to be renamed to. After that process is done,
you already know the filename and it’s location and you can plug that into the “finfo_file” code so that this
routine grabs the newly moved file.

Further info on this… Files are not uploaded automatically to a server just because you add a file field in
your form. It is uploaded to the server’s temporary folder and you move it to wherever you want it stored.
If you understand this, good. If not, here is a tutorial on uploading graphics. But, it works shows most of
the other items you need for this. http://www.w3schools.com/php/php_file_upload.asp

Did that make sense to you?

***** EDIT: If you have a file already on your site for testing, just remove the posting of the form and
just have the page display that filetype somewhere with a code line like:
[php]

<?PHP echo $file_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), "/uploads/test.php"); ?>

[/php]
Note that the uploads folder with a starting slash means to start at your root of the site then in the folder…

Hi ErnieAlex,

thanks, I am a little bit familiar with file_uploads (a do have another issue on this to resolve) but the issue I had was emailing a file that was already located on the server.

I’ve played around with the code and finally managed to receive an email with the correct file attached. The downside is that the file uploaded has to be within the same directory as the mailer.php script for some reason, I suspect its something needed on this line;-
[php]$attachment = chunk_split(base64_encode(file_get_contents($_POST[“file_name”] )));[/php]

My current working code is:-
[php]<?php
if (isset($_POST[‘submit’]))
{
$file_name = $_POST[‘file_name’];
$doc_email = $_POST[‘doc_email’];
$doc_message = $_POST[‘doc_message’];
$file_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file_name);
$username = $_POST[‘username’];
$company = $_POST[‘company’];

//define the receiver of the email
$to = $_POST[‘doc_email’];
$subject = “$username from $company Sent you a file”;

//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: [email protected]\r\nReply-To: [email protected]”;

//add boundary string and mime type specification
$headers .= “\r\nContent-Type: multipart/mixed; boundary=“PHP-mixed-”.$random_hash.”"";

//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($_POST[“file_name”] )));

//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 there,

<?php echo $username; ?> from <?php echo $company; ?> sent you a file. <?php echo $username; ?> wrote:- <?php echo $doc_message; ?>

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

Hi there,

<?php echo $username; ?> from <?php echo $company; ?> sent you a file.

<?php echo $username; ?> wrote:-

<?php echo $doc_message; ?>

For further information about this email or for any questions you may have regarding XX please contact [email protected]

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

–PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/<?php echo $filetype; ?>; name="<?php echo $file_name; ?>"
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"; } ?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service