<?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) {
if(!empty($to) && !empty($from)) {
$this->to = $to;
$this->from = $from;
if(!empty($subject)) {
$this->subject = $subject;
}
if(!empty($message)) {
$this->message = $message;
}
}
$this->mailer = "PHP " . phpversion();
$this->attachments = array();
}
public function SetMailer($mailer) {
if(!empty($mailer) && is_string($mailer)) {
$this->mailer = $mailer;
}
}
public function SetTo($to) {
if(!empty($to) && is_string($to)) {
$this->to = $to;
}
}
public function SetFrom($from) {
if(!empty($from) && is_string($from)) {
$this->from = $from;
}
}
public function SetSubject($subject) {
if(!empty($subject) && is_string($subject)) {
$this->subject = $subject;
}
}
public function AddCC($email) {
if(!empty($email) && is_string($email)) {
if(strlen($this->cc) > 0) {
$this->cc .= ",".$email;
}
else {
$this->cc = $email;
}
}
}
public function AddBCC($email) {
if(!empty($email) && is_string($email)) {
if(strlen($this->bcc) > 0) {
$this->bcc .= ",".$email;
}
else {
$this->bcc = $email;
}
}
}
public function AddMessage($message) {
if(!empty($message) && is_string($message)) {
$this->message = $message;
}
}
public function AddAttachment($attachment) {
if(file_exists($attachment)) {
$attachmentInfo = array();
$this->uid = "==Multipart_Boundary_x{" . md5(uniqid(time())) . "}x";
$attachmentInfo['Attachment'] = $attachment;
$attachmentInfo['Size'] = filesize($attachment);
$fp = fopen($attachment, "rb");
flock($fp, LOCK_SH);
$data = fread($fp, filesize($attachment) + 1);
flock($fp, LOCK_UN);
fclose($fp);
$attachmentInfo['Content'] = chunk_split(base64_encode($data), 64);
$extStart = strrpos($this->attachment, ".");
$attachmentInfo['Extension'] = substr($attachmentInfo['Attachment'], $extStart, (strlen($attachmentInfo['Attachment']) - $extStart));
$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) {
if(empty($to) && !empty($this->to)) { $to = $this->to; }
if(empty($from) && !empty($this->from)) { $from = $this->from; }
if(empty($subject) && !empty($this->subject)) { $subject = $this->subject; }
if(empty($to) || empty($from) || empty($subject)) { return false; }
$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 . "
";
if(!empty($this->attachments) && is_array($this->attachments)) {
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}--";
if(mail($to, $from, "", $headers)) {
return true;
}
return false;
}
private function GenerateRandomFilename($extension) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$salt = "";
for($i = 0; $i < mt_rand(5, 25); $i++) {
$salt.= $chars{$i};
}
$fname = md5($salt . time());
return $fname . $extension;
}
}
?>
source