Last active
December 15, 2015 14:19
-
-
Save kcmckell/5273838 to your computer and use it in GitHub Desktop.
A PHP file containing useful utilities for backing up files and databases on a Linux server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MailChunker { | |
public function __construct( $addr = "", $sub = "", $msg = "" , $from = "", $file = "", $maxfsize = "9MB") { | |
$this->addr = $addr; # Comma-separated string of email addresses. | |
$this->sub = $sub; | |
$this->msg = $msg; | |
$this->from = $from; | |
$this->fname = $file; | |
$this->maxfsize = $maxfsize; | |
}; | |
public function send() { | |
$this->fsize = filesize($this->fname); | |
// Check for valid type of maxfsize. Should be of the form 123B or 98MB, etc. | |
if (! is_string($this->maxfsize)) { | |
// If the max file size has been specified as a number, assume it is in Bytes. | |
$this->maxfsize = $this->maxfsize . "B"; | |
}; | |
if ($this->fsize <= filesize2bytes($this->maxfsize) { | |
// Send single chunk. | |
mail_file($this->addr, $this->sub, $this->msg, $this->from, $this->fname); | |
} else { | |
// Break up and send chunks. | |
} | |
} | |
}; | |
/** | |
* Source: | |
* http://www.barattalo.it/2010/01/10/sending-emails-with-attachment-and-html-with-php/ | |
*/ | |
function mail_file( $to, $subject, $messagehtml, $from, $fileatt, $replyto="" ) { | |
// handles mime type for better receiving | |
$ext = strrchr( $fileatt , '.'); | |
$ftype = ""; | |
if ($ext == ".doc") $ftype = "application/msword"; | |
if ($ext == ".jpg") $ftype = "image/jpeg"; | |
if ($ext == ".gif") $ftype = "image/gif"; | |
if ($ext == ".zip") $ftype = "application/zip"; | |
if ($ext == ".pdf") $ftype = "application/pdf"; | |
if ($ext == ".gz") $ftype = "applicatoin/gzip"; | |
if ($ftype=="") $ftype = "application/octet-stream"; | |
// read file into $data var | |
$file = fopen($fileatt, "rb"); | |
$data = fread($file, filesize( $fileatt ) ); | |
fclose($file); | |
// split the file into chunks for attaching | |
$content = chunk_split(base64_encode($data)); | |
$uid = md5(uniqid(time())); | |
// build the headers for attachment and html | |
$h = "From: $from\r\n"; | |
if ($replyto) $h .= "Reply-To: ".$replyto."\r\n"; | |
$h .= "MIME-Version: 1.0\r\n"; | |
$h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; | |
$h .= "This is a multi-part message in MIME format.\r\n"; | |
$h .= "--".$uid."\r\n"; | |
$h .= "Content-type:text/html; charset=iso-8859-1\r\n"; | |
$h .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; | |
$h .= $messagehtml."\r\n\r\n"; | |
$h .= "--".$uid."\r\n"; | |
$h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n"; | |
$h .= "Content-Transfer-Encoding: base64\r\n"; | |
$h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n"; | |
$h .= $content."\r\n\r\n"; | |
$h .= "--".$uid."--"; | |
// send mail | |
return mail( $to, $subject, strip_tags($messagehtml), str_replace("\r\n","\n",$h) ) ; | |
}; | |
/** | |
* Converts human readable file size (e.g. 10 MB, 200.20 GB) into bytes. | |
* | |
* @param string $str | |
* @return int the result is in bytes | |
* @author Svetoslav Marinov | |
* @author http://slavi.biz | |
*/ | |
function filesize2bytes($str) { | |
$bytes = 0; | |
$bytes_array = array( | |
'B' => 1, | |
'KB' => 1024, | |
'MB' => 1024 * 1024, | |
'GB' => 1024 * 1024 * 1024, | |
'TB' => 1024 * 1024 * 1024 * 1024, | |
'PB' => 1024 * 1024 * 1024 * 1024 * 1024, | |
); | |
$bytes = floatval($str); | |
if (preg_match('#([KMGTP]?B)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { | |
$bytes *= $bytes_array[$matches[1]]; | |
} | |
$bytes = intval(round($bytes, 2)); | |
return $bytes; | |
}; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment