View Full Version : Sending an image as attachment with php mail

02-06-2007, 01:30 AM
Hello All,

I have a form that allows a user to enter there name email address subject and text area for comments I attached a spot to upload an image to be attached to the email. Yet I am having trouble with the image showing up on the other end in the email as a .dat file (phpXuBK7g.dat)??? here is some of the code that does this

<form enctype="multipart/form-data" method="post" action="http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">
<br />
<input type="file" name="filename" size="30" />
<input type="submit" name="submit" />

mosMail($_POST['*request_email'], $_POST['*request_name'], email@email.com', Submission', $body, 1 ,'','',$_FILES['filename']['tmp_name']

mosMail calls phpMail and sends the attachment which works.

the other questions I have are follows?
If I do

echo "<PRE>";
echo "</PRE>";

nothin shows up for the input type file? (I thought it was in FF and not IE but now it doesnt show in FF either)? Wondering if anyone can explain why its not showing up?

If I do

echo "<PRE>";
echo "</PRE>";

then I get information like

[oFile1] => Array
[name] => image8.jpg
[type] => image/pjpeg
[tmp_name] => /tmp/phprVw2s6
[error] => 0
[size] => 40241


so I know its there but if I do

echo "<img src=\"".$_FILE['oFile1']['tmp_name']."/".$_FILE['oFile1']['name']."\">

nothing shows up?:confused:

02-06-2007, 02:53 AM
I am not directly familiar with mosMail, but the only thing you are passing to it is the temp name - $_FILES['filename']['tmp_name']. It is probably doing the best it can to use the passed name and since this does not have an extension, it is probably adding a default .dat

If one of the parameters in the mosMail function call is the "name" to give the attachment, than this would allow you to give it the name you want. If not, than you probably need to copy/move_uploaded_file the file into a variable or a file within your web space so that you can give it the name you want.

As to your other questions -

The the $_POST array specifically does not contain anything from a type="file" form field, that is the purpose of the $_FILES array. Nothing is showing up because it is not supposed to. If something did appear at some point in your trials, it was probably because the form was not coded correctly.

As soon as your upload form processing script finishes running, the $_FILES variables and the temp uploaded file are destroyed. So, it is not possible to put a reference to the $_FILES array into an <img src="..." tag. Even if you output this tag from your upload form processing script, the browser will fetch the image some time after the current script finishes running on the server. Another problem that will prevent this from working is that the URL in the <img src="...." tag must refer to a web accessible location so that the browser can fetch it. The default location for the temp uploaded file is not a public accessible location.

02-06-2007, 07:42 AM

Thankyou for the reply.

Ok well using the following

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['filename']['name']);


makes sense however I didnt want to actually store the images on the server just push them through the email, Is there a way of setting it to session or just a variable or do I have to purge all files in the directory everytime?

here is the attached info for mosMail as you can see there is no name parameter for the attachment...

* Mail function (uses phpMailer)
* @param string From e-mail address
* @param string From name
* @param string/array Recipient e-mail address(es)
* @param string E-mail subject
* @param string Message body
* @param boolean false = plain text, true = HTML
* @param string/array CC e-mail address(es)
* @param string/array BCC e-mail address(es)
* @param string/array Attachment file name(s)
* @param string/array Reply-to e-mail address
* @param string/array Reply-to name

Again thanks for the info!