/    Sign up×
Community /Pin to ProfileBookmark

emailing attachments

Hi,

I’m looking for a php script to allow web users to email an attached document from a submission form, however I can’t find one that compiles with out errors/ has good documentation/readme file to get going with.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@neilauthorFeb 03.2004 — I'm trying to use the following script at the mo, but i get this error. Where can I get class.Email.php for inclusion?

It sends me a mail and the attachment i think is turned into a load of letters and appears in the body of the message

Warning: Failed opening 'class.Email.php' for inclusion (include_path='.:/usr/share/pear') in /home/john101/public_html/mysharedaccounts/teescreate/send_attatchments3.php on line 4 Email Was Sent


<?

// -- EXAMPLE PHP FILE --

include('class.Email.php');

$email = new Email("You <[email protected]>",

"[email protected]",

"This is a Test Email Message");

$email->TextOnly = false;

$email->Content = "<html><body><font face='verdana' color='red'>".

"Hello World!</font></body></html>";

//** send a copy of this file in the email.

$email->Attach(__FILE__, "text/plain");

//** attach this non-existant image (create first).

$email->Attach("myimage.gif", "image/gif");

$wassent = $email->Send();

echo "Email Was ", ($wassent ? "Sent" : "Not Sent");

?>

<?

//-- EMAIL CLASS FILE TO INCLUDE --

//** ©William Fowler ([email protected])

//**
DECEMBER 15/2003, Version 1.0

if(isset($GLOBALS["emailmsgclass_php"])) { return; } //** onlyinclude once.

$GLOBALS["emailmsgclass_php"] = 1; //**
filewas included.

//** the newline character(s) to be used when generating an email message.

define("EmailNewLine", "rn");

//** the unique X-Mailer identifier for emails sent with this tool.

define("EmailXMailer", "PHP-EMAIL,v1.0 (William Fowler)");

//** the default charset values for both text and HTML emails.

define("EmailTextCharset", "iso-8859-1");

define("EmailHtmlCharset", "us-ascii");

//**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//**
EMAIL_MESSAGE_CLASS_DEFINITION********************************************

//**
The Email class wrappers PHP's mail function into a class capable of

//** sending attachments and HTML email messages. Custom headers can also be

//**
included as if using the mail function.

class Email

{

//** (String) the recipiant email address, or comma separated addresses.

var $To = null;

//** (String) the email address of the message sender.

var $From = null;

//** (String) the subject of the email message.

var $Subject = null;

//** an array of EmailAttachment instances to be sent with this message.

var $Attachments;

//** any custom header information that must be used when sending email.

var $Headers = null;

//** whether email to be sent is a text email or a HTML email.

var $TextOnly = true;

//** the charset of the email to be sent (initially none, let type decide).

var $Charset = null;

//** Create a new email message with the parameters provided.

function Email($to=null, $from=null, $subject=null, $headers=null)

{

$this->To = $to;

$this->From = $from;

$this->Subject = $subject;

$this->Headers = $headers;


$this->Attachments = Array(); //** empty array for attacmnet instances.

}

//**
Returns: Boolean

//** Create a new file attachment for the file (and optionally MIME type)

//**
given. If the file cannot be located no attachment is created and

//** FALSE is returned.

function Attach($pathtofile, $mimetype=null)

{

//** create the appropriate email attachment. If the attachment does not

//**
exist the attachment is not created and FALSE is returned.

$attachment = new EmailAttachment($pathtofile, $mimetype);
if(!$attachment->Exists())
return false;
else
{
$this->Attachments[] = $attachment; //** add the attachment to list.
return true; //** attachment successfully added.
}

}

//** Returns: Boolean

//**
Determine whether or not the email message is ready to be sent. A TO and

//** FROM address are required.

function IsComplete()

{

return (strlen(trim($this->To)) > 0 && strlen(trim($this->From)) > 0);

}

//** Returns: Boolean

//**
Attempt to send the email message. Attach all files that are currently

//** valid. Send the appropriate text/html message. If not complete FALSE is

//**
returned and no message is sent.

function Send()

{

if(!$this->IsComplete()) //** message is not ready to send.

return false; //**
no message will be sent.

//** generate a unique boundry identifier to separate attachments.

$theboundary = md5(uniqid("EMAIL"));

//** the from email address and the current date of sending.

$headers = "From: $this->From " . EmailNewLine .
"Date: " . date("r", time()) . EmailNewLine;


//** add the custom headers here, before important headers so that none are

//**
overwritten by custom values.

if($this->Headers != null && strlen(trim($this->Headers)) > 0)
$headers .= $this->Headers . EmailNewLine;


//** add the custom headers, the MIME encoding version and MIME typr for the

//**
email message, the boundry for attachments, the error message if MIME is

//** not suppported.

$headers .= "X-Mailer: " . EmailXMailer . EmailNewLine .
"MIME-Version: 1.0" . EmailNewLine .
"Content-Type: multipart/mixed; " .
"boundary=$theboundary" . EmailNewLine . EmailNewLine .
"This is a multipart MIME message" . EmailNewLine .
EmailNewLine;


//** determine the proper encoding type and charset for the message body.

$theemailtype = "text/" . ($this->TextOnly ? "plain" : "html");
if($this->Charset == null)
$this->Charset = ($this->TextOnly ? EmailTextCharset : EmailHtmlCharset);


//** add the encoding header information for the body to the content.

$thebody = "--$theboundary" . EmailNewLine .
"Content-Type: $theemailtype; charset=$this->Charset" .
EmailNewLine . "Content-Transfer-Encoding: 8bit" .
EmailNewLine . EmailNewLine . $this->Content . EmailNewLine .
EmailNewLine;


//** loop over the attachments for this email message and attach the files

//**
to the email message body.

foreach($this->Attachments as $attachment)
{
$thebody .= "--$theboundary" . EmailNewLine . $attachment->ToHeader() .
EmailNewLine;
}
$thebody .= "--$theboundary--";


//** attempt to send the email message. Return the operation success.

return mail($this->To, $this->Subject, $thebody, $headers);
}

}

//******************************************END_EMAIL_MESSAGE_CLASS_DEFINITION

//**
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//**
EMAIL_ATTACHMENT_CLASS_DEFINITION*****************************************

//**
The EmailAttachment class links a file in the file system to the

//** appropriate header to be included in an email message. if the file does

//**
not exist the attachment will not be sent in any email messages.

class EmailAttachment

{

//** (String) the full path to the file to be attached.

var $FilePath = null;

//** (String) the MIME type for the file data of this attachment.

var $ContentType = null;

//** Creates a new email attachment ffrom the file path given. If no content

//**
type is given the default 'application/octet-stream' is used.

function EmailAttachment($pathtofile=null, $mimetype=null)

{

//** if no MIME type is provided use the default value specifying binary data.

//**
Otherwise use the MIME type provided.

if($mimetype == null || strlen(trim($mimetype)) == 0)
$this->ContentType = "application/octet-stream";
else
$this->ContentType = $mimetype;

$this->FilePath = $pathtofile; //** save the path to the file attachment.

}

//** Returns: Boolean

//**
Determine whether or not the email attachment has a valid, existing file

//** associated with it.

function Exists()

{

if($this->FilePath == null || strlen(trim($this->FilePath)) == 0)

return false;

else

return file_exists($this->FilePath);

}

//** Returns: String

//**
Generate the appropriate header string for this email attachment. If the

//** the attachment does not exist NULL is returned.

function ToHeader()

{

if(!$this->Exists()) //** no valid attached file exists.

return null; //**
no header text can be generted.

//** add the content type and file name of the attachment.

$header = "Content-Type: $this->ContentType; " .
"name="" . basename($this->FilePath) . """ . EmailNewLine .
"Content-Disposition: attachment; filename="" .
basename($this->FilePath) . """ . EmailNewLine;


//** add the key for the content encoding of the attachment body to follow.

$header .= "Content-Transfer-Encoding: base64" . EmailNewLine .
EmailNewLine;


//** open the file attachment in binary mode and read the contents.

$thefile = fopen($this->FilePath, "rb");
$data = fread($thefile, filesize($this->FilePath));


//** add the attachment data to the header. encode the binary data in BASE64

//**
and break the encoded data into the appropriate chunks.

$header .= chunk_split(base64_encode($data), 76, EmailNewLine) .
EmailNewLine;

return $header; //** return the headers generated by file.

}

}

//**************************************END_EMAIL_ATTACHMENT_CLASS_DEFINITION

//**
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

?>
Copy linkTweet thisAlerts:
@neilauthorFeb 04.2004 — can anyone help please ?
Copy linkTweet thisAlerts:
@pierskFeb 04.2004 — Have you had a look at the [url=http://pear.php.net]PEAR[/url] package, [url=http://pear.php.net/package/Mail]Mail[/url]?
Copy linkTweet thisAlerts:
@neilauthorFeb 04.2004 — no i haven't, i actually just found a script that works for me ? and I'm gunna try to convert that one to my needs.

I'll have a look at pear if i get stuck though,

thanks ?
Copy linkTweet thisAlerts:
@neilauthorFeb 04.2004 — no i haven't, i actually just found a script that works for me ? and I'm gunna try to convert that one to my needs.

I'll have a look at pear if i get stuck though,

thanks ?
Copy linkTweet thisAlerts:
@daed17Feb 05.2004 — I think this might be helpful in your quest...

http://www.sitepoint.com/article/679/5
×

Success!

Help @neil spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...