Page 1 of 1
send attachment with email
Posted: Tue Oct 25, 2005 4:18 pm
by kristolklp
How can I add an attachment to an email via php code? I am using <input type="file" name="usefile"> on my page to locate the file however I can't seem to find a clear example of how to incorporate it into the mail function. I want this all to take place behind the scenes once the file is uploaded. Therefore all I really looking to be able do is add the basename($_FILES['userfile']['tmp_name']) to the email code below as an attachment, nothing fancy.
Code: Select all
$FromEmail = "email_address";
$FromName = "email_name";
$ToSubject = "email_subject";
$EmailBody = "email_message"
$EmailFooter="email_footer";
$Message = $EmailBody.$EmailFooter;
mail($Name." <".$Email.">",$ToSubject, $Message, "From: ".$FromName." <".$FromEmail.">");
Thanks for your help.
Posted: Tue Oct 25, 2005 4:40 pm
by John Cartwright
I love searching.. seems we have a class in our code Code Snipplets forums
viewtopic.php?t=34242&highlight=email+a ... ttachement
Posted: Tue Oct 25, 2005 9:26 pm
by kristolklp
Thanks for the link but I am kind of wanting a stripped down version of that class. I'm a little new to PHP and am uncomfortable with classes and they work (I don't even know how I would call that class). Was hoping there was somewhat of easier way to just add the file to the email. Guess not. Thanks for the help, will continue researching classes do I can figure out how I can use it.
Posted: Tue Oct 25, 2005 10:11 pm
by kristolklp
Ok, I am close (i think) to getting this class to work. I receive an email that contains no attachment but includes the encoded file in the message. I am confused how to pass '$file' to this function.
Code: Select all
function addAttachment ( $filename, $file, $mimetype ) {
$this->files[$filename] = array( "MIME" => $mimetype, "FILE" => $file );
}
here is my code, please help:
Code: Select all
<?php
include("email.php");
#variables
$to ="me@email.com";
$from ="you@email.com";
$subject="File Attachement";
$message = "See attachment...";
if ($_FILES) {
#ALLOWED FILE TYPES
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/tiff",
"image/png",
"image/x-png",
"application/octet-stream");
#IF THE FILE UPLOADS SET USERFILE AND BUILD VARIABLES
if (is_uploaded_file ($_FILES['userfile']['tmp_name'])) {
$userfile = addslashes (fread (fopen($_FILES["userfile"]["tmp_name"], "r"), filesize($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];
}
}
if ($_POST){
$email = new email($to, $from, $subject, $message);
$email->addAttachment ( $file_name, $userfile, $file_type); <---how should I pass the file?
$email->sendEmail();
}
?>
<html>
<head>
<title>Send Email Attachment</title>
</head>
<body>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="24000000">
<input type="file" name="userfile" size="60"><br><br>
<input type="submit" name="submit" value="Send Email">
</form>
</body>
</html>
Thanks!
Posted: Wed Oct 26, 2005 9:43 am
by kristolklp
am I calling the class wrong? Im receiving the email but the attachment as encoded text in the body of the email. Is this class supposed to attach as a file in the email or include in the body?
viewtopic.php?t=34242&highlight=email+a ... ttachement
Any help is greatly appreciated.