Please Help me with adding an attachment to an email via PHP
Hello what im trying to do is very simple however I guess its just not simple enough for me
Anyway my problem is I can quite make the attachment part of my flash/PHP emailer work.
All I want to do is attach one file to an email, Basically a persons resume.
I have the email part working I just cant seem to get the attachment to attach and send. The file is going to already be on the server, I used a tutorial to get files uploaded.
this is my email actionscript:
//imagePane change to text output
//Allow this domain
System.security.allowDomain("http://localhost/");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
// When the user selects a file, the onSelect() method is called, and
// passed a reference to the FileReference object.
listener.onSelect = function(selectedFile:FileReference):Void {
// Flash is attempting to upload the image.
statusArea.text += "Attempting to upload " + selectedFile.name + "\n";
// Upload the file to the PHP script on the server.
selectedFile.upload("upload.php");
};
// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += "Uploading " + selectedFile.name + "\n";
};
//Possible file upload errors
listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "HTTPError number: "+httpError +"\nFile: "+ file.name;
}
listener.onIOError = function(file:FileReference):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "IOError: "+ file.name;
}
listener.onSecurityError = function(file:FileReference, errorString:String):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "SecurityError: "+SecurityError+"\nFile: "+ file.name;
}
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
uploadBtn.onPress = uploadImage;
imagePane.addEventListener("complete", imageDownloaded);
// Call the uploadImage() function, opens a file browser dialog.
function uploadImage(event:Object):Void {
imageFile.browse([{description: "Text Files", extension: "*.txt;*.rtf;*.doc"}]);
}
// If the filedoes not download, the event object's total property
// will equal -1. In that case, display am error message
function imageDownloaded(event:Object):Void {
if(event.total == -1) {
imagePane.contentPath = "error";
}
}
function downloadImage(file:Object):Void {
imagePane.contentPath = "./files/" + file;
}
here is my upload php:
PHP Code:
<?php
//create the directory if doesn't exists (should have write permissons)
if(!is_dir("./files")) mkdir("./files", 0755);
//move the uploaded file
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);
chmod("./files/".$_FILES['Filedata']['name'], 0777);
?>
and here is my email php:
PHP Code:
<?php
// read the variables from the strings
$sendername = $_REQUEST["sendername"];
$subject = "Resume from website.";
$message = $_REQUEST["message"];
$sender = $_REQUEST["sender"];
// include sender IP in the message and the senders name.
$full_message = "Sender IP: " . $_SERVER['REMOTE_ADDR'] . "\n\n" . "From: " . $sendername . "\n\n" . $message;
$message= $full_message;
// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$sender = stripslashes($sender);
// send the email
if(isset($message) and isset($subject) and isset($sender)and isset($sendername)){
mail("[email protected]", $subject, $message, "From: $sender");
}
?>
The upload script puts the file in a folder called files. How do I put the file into the email?
At this point the only thing left is getting the file to attach to the email and send it, however I cant figure this out. If anyone could help me I would really appreciate it.
Thanks
PS: I have attached my files
PSS: I couldnt attach the fla since its too big so I have attached the swf
Hello, Musicman thank you for the direction. However I really don’t understand the example given. I have examined the code, but I fail to comprehend how it works
Can you give me any direction, to help me better understand how it works?
Once again thank you Musicman for the example as well as your assistance.
just send yourself a mail with an attached file from a regular mail program, and then view "source" or whatever it might be called. You would see something like
Code:
Received: from mycomputer (localhost [127.0.0.1])
by mycomputer (Cyrus v2.2.12) with LMTPA;
Sat, 09 Feb 2008 09:05:09 +0100
From: [email protected]
Date: Sat, 09 Feb 2008 09:05:00 +0100
To: [email protected]
Subject: test mail
Message-ID: <47AD5EAC.mailxC1C11MSUE@mycomputer>
User-Agent: nail 10.5 4/27/03
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="DWUIK1WF69-=-YX1E11LC9D-CUT-HERE-3SFSQ1CAP5-=-O79VB15SMZ"
This is a multi-part message in MIME format.
--DWUIK1WF69-=-YX1E11LC9D-CUT-HERE-3SFSQ1CAP5-=-O79VB15SMZ
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
this is plain text with an attachment
--DWUIK1WF69-=-YX1E11LC9D-CUT-HERE-3SFSQ1CAP5-=-O79VB15SMZ
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="hack1-agenda.pdf"
JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURl
Y29kZT4+CnN0cmVhbQp4nMVbzY4bxxEGcuQlr8BTIAPmZPq/W5dAjhRFgS3I1toGHOQwu5wl
............
L1NpemUgMjYgL1Jvb3QgMSAwIFIgL0luZm8gMiAwIFIKL0lEIFsouEATeE9ylv89Z4EztXN8
pykouEATeE9ylv89Z4EztXN8pyldCj4+CnN0YXJ0eHJlZgoxMDMzNTUKJSVFT0YK
--DWUIK1WF69-=-YX1E11LC9D-CUT-HERE-3SFSQ1CAP5-=-O79VB15SMZ--
So the code is about encoding the attachment into base64, adding those headers and boundaries.