Skip to content

Instantly share code, notes, and snippets.

@kas-cor
Created August 11, 2016 07:01
Show Gist options
  • Save kas-cor/f0a08372eacca9ed8ea754609d472136 to your computer and use it in GitHub Desktop.
Save kas-cor/f0a08372eacca9ed8ea754609d472136 to your computer and use it in GitHub Desktop.
Small class for sending emails
<?php
/**
* @author kas-cor <[email protected]>
* @link http://github.com/kas-cor repositories
*/
namespace cls;
class Mail {
/**
* Mail from
* @var string
*/
private $mailFrom;
/**
* Name from
* @var string
*/
private $mailName;
/**
* Footer mail
* @var string
*/
private $mailFooter;
/**
* Construct
* @param string $mailFrom
* @param string $mailName
* @param string $mailFooter
*/
public function __construct($mailFrom, $mailName, $mailFooter) {
$this->mailFrom = $mailFrom;
$this->mailName = $mailName;
$this->mailFooter = $mailFooter;
}
/**
* Send mail
* @param string $email Mail to
* @param string $subject Subject text
* @param string $body Body text
* @param array $attach Array files names
* @return mail
*/
public function send($email, $subject, $body, $attach = array()) {
// Footer
$body = $body . $this->mailFooter;
// Param
$un1 = strtoupper(md5("ddkvne5fjgje" . uniqid(time())));
$un2 = strtoupper(md5("3tcvn6cvhdhf" . uniqid(time())));
$name = "=?utf-8?B?" . base64_encode($this->convertToUtf8($this->mailName)) . "?=";
$subject = "=?utf-8?B?" . base64_encode($this->convertToUtf8($subject)) . "?=";
// HEAD
$head = "Reply-To: " . $this->mailName . " <" . $this->mailFrom . ">\r\n";
$head.="From: " . $this->mailName . " <" . $this->mailFrom . ">\r\n";
$head.="To: " . $email . "\r\n";
$head.="Subject: $subject\r\n";
$head.="Organization: =?utf-8?B?" . base64_encode($this->convertToUtf8($this->mailName)) . "?=\r\n";
$head.="X-Mailer: PHP/" . phpversion() . "\r\n";
$head.="Mime-Version: 1.0\r\n";
$head.="Content-Type: multipart/mixed; boundary=" . $un1 . "\r\n";
// BODY
$zag = "--" . $un1 . "\r\n";
$zag.="Content-Type: multipart/alternative; boundary=" . $un2 . "\r\n";
$zag.="\r\n";
// HTML
$zag.="--" . $un2 . "\r\n";
$zag.="Content-Type: text/html; charset=utf-8\r\n";
$zag.="Content-Transfer-Encoding: base64\r\n";
$zag.="\r\n";
// TEXT
$zag.=chunk_split(base64_encode($this->convertToUtf8($body))) . "\r\n";
$zag.="--" . $un2 . "--\r\n";
$zag.="Content-Type: text/plain; charset=utf-8\r\n";
$zag.="Content-Transfer-Encoding: base64\r\n";
$zag.="\r\n";
$zag.=chunk_split(base64_encode($this->convertToUtf8(strip_tags($body)))) . "\r\n";
// Attachment
if (!empty($attach)) {
foreach ($attach as $file) {
$f = fopen($file['filename'], "r");
$content = fread($f, filesize($file['filename']));
fclose($f);
$zag.= "--$un1\r\n";
$zag.= "Content-Type: application/octet-stream; name=\"" . $file['name'] . "\"\r\n";
$zag.= "Content-Transfer-Encoding: base64\r\n";
$zag.= "Content-Disposition: attachment; filename=\"" . $file['name'] . "\"\r\n";
$zag.="\r\n";
$zag.= chunk_split(base64_encode($content)) . "\r\n";
$zag.="\r\n";
}
}
$zag.="--" . $un1 . "--\r\n";
return mail($email, $subject, $zag, $head);
}
/**
* Convert to utf-8, if win-1251
* @param type $text
* @return type
*/
private function convertToUtf8($text) {
if (!preg_match("//u", $text)) {
return iconv("windows-1251", "utf-8", $text);
} else {
return $text;
}
}
}
@kas-cor
Copy link
Author

kas-cor commented Aug 11, 2016

Useing

Without attachments

$mailFrom = "[email protected]";
$mailName = "Org. name";
$mailFooter = "<br /><hr />e-mail: <a href=\"mailto:" . $mailFrom . "\">" . $mailFrom . "</a>";
$mail = New Mail($mailFrom, $mailName, $mailFooter);
$to = "[email protected]";
$subject = "Test message";
$body = "Test\nTest\nTest";
echo $mail->send($to, $subject, $body);

With attachments

$mailFrom = "[email protected]";
$mailName = "Org. name";
$mailFooter = "<br /><hr />e-mail: <a href=\"mailto:" . $mailFrom . "\">" . $mailFrom . "</a>";
$mail = New Mail($mailFrom, $mailName, $mailFooter);
$to = "[email protected]";
$subject = "Test message";
$body = "Test\nTest\nTest";
$attach = array(
    "/path/to/file1",
    "/path/to/file2"
);
echo $mail->send($to, $subject, $body, $attach);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment