/ Published in: PHP
A simple very easy to use class for sending emails with / without attachments.
Expand |
Embed | Plain Text
<?php class SimpleMail { public $to; public $from; public $fromName; public $subject; public $cc; public $bcc; public $message; private $uid; private $mailer; private $attachments; public function __construct($to = null, $from = null, $subject = null, $message = null) { $this->to = $to; $this->from = $from; $this->subject = $subject; } $this->message = $message; } } } public function SetMailer($mailer) { $this->mailer = $mailer; } } public function SetTo($to) { $this->to = $to; } } public function SetFrom($from) { $this->from = $from; } } public function SetSubject($subject) { $this->subject = $subject; } } public function AddCC($email) { $this->cc .= ",".$email; } else { $this->cc = $email; } } } public function AddBCC($email) { $this->bcc .= ",".$email; } else { $this->bcc = $email; } } } public function AddMessage($message) { $this->message = $message; } } public function AddAttachment($attachment) { $attachmentInfo['Attachment'] = $attachment; $attachmentInfo['Name'] = self::GenerateRandomFilename($attachmentInfo['Extension']); $attachmentInfo['Mime'] = "application/octet-stream; name=\"{$attachmentInfo['Name']}\" "; $this->attachments[] = $attachmentInfo; } } public function SendMessage($to = null, $from = null, $subject = null) { $headers = ""; $headers.= "From: {$this->fromName} <{$from}> "; $headers.= "Reply-To: {$to} "; $headers.= "Subject: {$subject} "; $headers.= "X-Mailer: {$this->mailer} "; $headers.= "MIME-Version: 1.0 "; $headers.= "Content-Type: multipart/mixed; boundary=\"{$this->uid}\" "; $headers.= "--{$this->uid} "; $headers.= "Content-Type: text/plain; charset=\"iso-8859-1\" "; $headers.= "Content-Trasfer-Encoding: 7bit "; $headers.= $this->message . " "; foreach($this->attachments as $attactment) { $headers.= "--{$this->uid} "; $headers.= "Content-Type: application/octet-stream; name=\"{$attactment['Name']}\" "; $headers.= "Content-Transfer-Encoding: base64 "; $headers.= "Content-Disposition: attachment; filename=\"{$attactment['Name']}\" "; $headers.= $attactment['Content'] . " "; } } $headers.= "--{$this->uid}--"; return true; } return false; } private function GenerateRandomFilename($extension) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $salt = ""; $salt.= $chars{$i}; } return $fname . $extension; } } ?>
You need to login to post a comment.
