Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
Moderator: General Moderators
Todd_Z
Forum Regular
Posts: 708 Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan
Post
by Todd_Z » Thu Jun 09, 2005 3:05 pm
Email.php
Code: Select all
<?PHP
/***************/
/* Email Class */
/***************/
class Email {
var $info;
var $files;
var $mimeBoundary;
function Email ( $to, $from, $subject, $message ) {
$this->info = array( "To" => $to, "From" => $from, "Subject" => $subject, "Message" => $message );
$this->mimeBoundary = "<<<--==+X[".md5(time())."]";
$files = array();
}
function addAttachment ( $filename, $file, $mimetype ) {
$this->files[$filename] = array( "MIME" => $mimetype, "FILE" => $file );
}
function sendEmail ( ) {
$headers = "From: {$this->info['From']}\r\n";
$headers .= "To: {$this->info['To']}\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$this->mimeBoundary."\"";
$message = "This is a multi-part message in MIME format. Your email system does not support this feature. Please contact sender for information.\r\n";
$message .= "\r\n--".$this->mimeBoundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n{$this->info['Message']}\r\n";
$message .= "--".$this->mimeBoundary;
foreach ( $this->files as $filename => $file ) {
$message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n";
$base64 = base64_encode($file['FILE']);
for ( $i=0; $i < strlen($base64) / 76; $i++ )
$message .= substr( $base64, $i*76, 76 )."\r\n";
$message .= "--".$this->mimeBoundary;
}
echo "\nTo: {$this->info['To']}";
echo "\nSubject: {$this->info['Subject']}";
echo "\n\n$headers";
echo "\n\n$message";
echo mail ( $this->info["To"], $this->info["Subject"], $message, $headers ) == true;
}
}
?>
Improvements are encouraged!
feyd | "fixed" old tag syntax
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Tue Aug 16, 2005 10:46 pm
I'm almost positive the last seperator has to have a trailing "--" after it or else its out of spec and some mail clients will choke.
raghavan20
DevNet Resident
Posts: 1451 Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:
Post
by raghavan20 » Fri Sep 02, 2005 6:03 pm
Its a good class.
It would have been better if you had included comments for your code.
If you have got time...can you explain what this piece of code does??
Code: Select all
for ( $i=0; $i < strlen($base64) / 76; $i++ )
$message .= substr( $base64, $i*76, 76 )."\r\n";
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Fri Sep 02, 2005 6:31 pm
It simply brakes up the base64 encoded string in lines of 76 chars long..
I believe netiquette says you shouldn't have lines longer than 80 chars?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 02, 2005 7:31 pm
ahhhh, nostalgia.. I actually still code with that restriction (when possible)
raghavan20
DevNet Resident
Posts: 1451 Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:
Post
by raghavan20 » Fri Sep 02, 2005 8:38 pm
timvw wrote: It simply brakes up the base64 encoded string in lines of 76 chars long..
I believe netiquette says you shouldn't have lines longer than 80 chars?
clarification:
do we do this 'base64 encoding' for all types of files images/music when they are made as attachements to mail? And all files are broken to several lines where each line is less than 80 chars??
can you tell me why the transfer should not contain a line more than 80 chars?
What are the situations when base64 encoding has to be done???
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 02, 2005 8:44 pm
you can encode in any fashion you wish. Base64 is the most commonly accepted form. Reason: the characters universally transfer between various character-sets and platforms.
As for why lines aren't longer than 80 characters: back in the days of dinosaurs, cave-coders had displays that could only go so wide. This width was 80 characters. I'm showing my age a bit there..
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Fri Sep 02, 2005 8:50 pm
A lot of people still do a fair bit of work over ssh/telnet style terminals. These still have the 80 character line length. So I think its still an important consideration for today. Historically the suggestion was to wrap lines closer to 70 on the initial email so that it could support a few levels of nesting without going completely screwy with "> " & "> >" reply indentations.
raghavan20
DevNet Resident
Posts: 1451 Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:
Post
by raghavan20 » Fri Sep 02, 2005 10:27 pm
Code: Select all
foreach ( $this->files as $filename => $file ) {
$message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n";
$base64 = base64_encode($file['FILE']);
for ( $i=0; $i < strlen($base64) / 76; $i++ )
$message .= substr( $base64, $i*76, 76 )."\r\n";
$message .= "--".$this->mimeBoundary;
}
in that particular set of code, this line
Code: Select all
$message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
should be
Code: Select all
$message .= "\r\nContent-Type: {$this->files['MIME']}; name=\"$filename\"\r\n";
the MIME should be the element in the files array not in the info array.
student101
Forum Newbie
Posts: 20 Joined: Sun Sep 21, 2008 4:04 pm
Post
by student101 » Wed Sep 24, 2008 5:10 pm
I noticed your code can send attachments and looks like from the recordset.
How would I be able to use it?
I am new to this class stuff
Todd_Z wrote: Email.php
Code: Select all
<?PHP
/***************/
/* Email Class */
/***************/
class Email {
var $info;
var $files;
var $mimeBoundary;
function Email ( $to, $from, $subject, $message ) {
$this->info = array( "To" => $to, "From" => $from, "Subject" => $subject, "Message" => $message );
$this->mimeBoundary = "<<<--==+X[".md5(time())."]";
$files = array();
}
function addAttachment ( $filename, $file, $mimetype ) {
$this->files[$filename] = array( "MIME" => $mimetype, "FILE" => $file );
}
function sendEmail ( ) {
$headers = "From: {$this->info['From']}\r\n";
$headers .= "To: {$this->info['To']}\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$this->mimeBoundary."\"";
$message = "This is a multi-part message in MIME format. Your email system does not support this feature. Please contact sender for information.\r\n";
$message .= "\r\n--".$this->mimeBoundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n{$this->info['Message']}\r\n";
$message .= "--".$this->mimeBoundary;
foreach ( $this->files as $filename => $file ) {
$message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n";
$base64 = base64_encode($file['FILE']);
for ( $i=0; $i < strlen($base64) / 76; $i++ )
$message .= substr( $base64, $i*76, 76 )."\r\n";
$message .= "--".$this->mimeBoundary;
}
echo "\nTo: {$this->info['To']}";
echo "\nSubject: {$this->info['Subject']}";
echo "\n\n$headers";
echo "\n\n$message";
echo mail ( $this->info["To"], $this->info["Subject"], $message, $headers ) == true;
}
}
?>
student101
Forum Newbie
Posts: 20 Joined: Sun Sep 21, 2008 4:04 pm
Post
by student101 » Mon Sep 29, 2008 5:16 am
Huoliuhi wrote: bump the thread up.
Thanks, it seems that this is a one to one session forum.
I think it's;
Read the manual if you can't find it
Search the forums and if you still can't find it
Post it in the forums I am a bit lost at the Post it in the forums part as nobody except you responded.
Cheers
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Sep 30, 2008 5:29 am
This thread is 3 years old.
If you want to send attachments via email, take a look at SwiftMailer
student101
Forum Newbie
Posts: 20 Joined: Sun Sep 21, 2008 4:04 pm
Post
by student101 » Tue Sep 30, 2008 5:38 am
Yes I am attempting that, doesn't this forum deal with SwiftMail?
JayBird wrote: This thread is 3 years old.
If you want to send attachments via email, take a look at SwiftMailer