Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created September 23, 2016 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtarleton/bf9e790863f4beaafec2eeaca45997d1 to your computer and use it in GitHub Desktop.
Save jtarleton/bf9e790863f4beaafec2eeaca45997d1 to your computer and use it in GitHub Desktop.
Helper functions
<?php
class JtAwesomeHelpers
{
public static function SimpleMailer($subject = 'DW Operation Complete', $body='', $htmlbody = '', $to = 'you@yourdomain.org') {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n" .
'From: Your Mailer <noreply@yourdomain.org>' . "\r\n" .
'Reply-To: noreply@yourdomain.org' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = wordwrap($body, 70, "\r\n");
$sent= mail($to, $subject, $htmlbody, $headers);
return $sent;
}
public static function SuperMailer($subject=null, $body=null,$htmlbody=null, $to='you@yourdomain.org',YourCSV $csv=null){
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$boundary = 'PHP-MAIL-BOUNDARY-'.md5(date('r', time()));
$headers = "MIME-Version: 1.0" . PHP_EOL.
"Content-Type: multipart/mixed;boundary=" . $boundary . PHP_EOL;
$csvName = 'your_attachment.csv';
//define the headers we want passed. Note that they are separated with \r\n
$headers .= 'From: Your Mailer <noreply@yourdomain.org>' .PHP_EOL.
'Reply-To: noreply@yourdomain.org' . PHP_EOL.
'X-Mailer: PHP/' . phpversion() . PHP_EOL;o
$attachment = '';
if(isset($csv)){
$csvString =str_replace("\n",PHP_EOL, $csv->getAsString());
$csvName= $csv->filename;
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment=chunk_split(base64_encode($csvString),43,"\r\n");
}
$message = sprintf("Multipart Message coming up".
PHP_EOL . "--%s" .
PHP_EOL . "Content-Type: text/plain; charset=\"iso-8859-1\"" .
PHP_EOL . "Content-Transfer-Encoding: 7bit" . PHP_EOL . "%s" .
PHP_EOL . "--%s" . PHP_EOL . "Content-Type: text/html;charset=\"iso-8859-1\"" .
PHP_EOL . "Content-Transfer-Encoding: 7bit" . PHP_EOL . "%s" .
PHP_EOL . "--%s" . PHP_EOL . "Content-Type: text/csv;charset=\"iso-8859-1\";name=\"%s\"" .
PHP_EOL . "Content-Transfer-Encoding: base64"
. PHP_EOL
."Content-Disposition: attachment;filename=\"%s\"%s". PHP_EOL . "--%s--",
$boundary, $body,
$boundary, $htmlbody,
$boundary, $csvName,$csvName,
"\r\n".''.
"\r\n".$attachment,
$boundary);
$sent= mail($to, $subject, $message, $headers);
return $sent;
}
public static function SimpleLogger($path,$lines,$name='batch-job') {
$path = sprintf('%s/%s-%s-log.txt', $path, $name, date("Y-m-d-H-i-s"));
$fh = fopen( $path, 'a' );
fwrite($fh,$lines);
fclose($fh);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment