Skip to content

Commit

Permalink
composer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr-ru committed Oct 2, 2020
1 parent 529a79b commit 724c452
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
39 changes: 20 additions & 19 deletions MultipartEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

/**
* PHP Multipart Mailer
* @author Rebel http://www.aleksandr.ru
* @author Aleksandr http://www.aleksandr.ru
* @version 1.0 18.04.2012
* @version 1.0.1 01.10.2013 ('\r\n' replaced with '\n' when building message)
* @version 1.0.2 06.03.2015 ($add_cid parameter added, html parsing for cid src, 'multipart/related' header in case of embedded images)
* @version 1.0.3 22.09.2015 (added $charset parameter to constructor for non utf8 usage)
* @version 1.0.4 25.12.2017 (added CSS "url('image.ext')" replacement with attachement CID)
* @version 1.0.4 25.12.2017 (added CSS "url('image.ext')" replacement with attachment CID)
* @version 1.0.5 20.03.2020 (added backtrace and optional exception for send errors)
* @version 1.0.6 23.07.2020 (image CID fix when send multiple times)
* @version 1.0.7 02.10.2020 (composer support)
*/
class MultipartEmail
{
Expand All @@ -19,7 +20,7 @@ class MultipartEmail
protected $from;
protected $to;
protected $subject;
protected $attachements = array();
protected $attachments = array();
protected $headers = array();

private $att_counter = 0;
Expand Down Expand Up @@ -156,12 +157,12 @@ function getSubject()
function addAttachement($file, $mime_type, $name, $file_is_data = false, $add_cid = false)
{
$this->att_counter++;
$this->attachements[$this->att_counter]['data'] = $file_is_data ? $file : file_get_contents($file);
$this->attachements[$this->att_counter]['mime_type'] = $mime_type;
$this->attachements[$this->att_counter]['name'] = $name;
$this->attachements[$this->att_counter]['cid'] = $add_cid ? uniqid('MPM-cid-') : null;
if(!$this->attachements[$this->att_counter]['data'] || !$this->attachements[$this->att_counter]['mime_type'] || !$this->attachements[$this->att_counter]['name']) {
unset($this->attachements[$this->att_counter]);
$this->attachments[$this->att_counter]['data'] = $file_is_data ? $file : file_get_contents($file);
$this->attachments[$this->att_counter]['mime_type'] = $mime_type;
$this->attachments[$this->att_counter]['name'] = $name;
$this->attachments[$this->att_counter]['cid'] = $add_cid ? uniqid('MPM-cid-') : null;
if(!$this->attachments[$this->att_counter]['data'] || !$this->attachments[$this->att_counter]['mime_type'] || !$this->attachments[$this->att_counter]['name']) {
unset($this->attachments[$this->att_counter]);
trigger_error("Failed to add an attachement!", E_USER_WARNING);
return false;
}
Expand All @@ -176,8 +177,8 @@ function addAttachement($file, $mime_type, $name, $file_is_data = false, $add_ci
*/
function removeAttachement($id)
{
if(isset($this->attachements[$id])) {
unset($this->attachements[$id]);
if(isset($this->attachments[$id])) {
unset($this->attachments[$id]);
return true;
} else {
trigger_error("Attachement not found");
Expand Down Expand Up @@ -239,18 +240,18 @@ function send($throw_exception = false)

$html = $this->html;

$multipart = ($html || sizeof($this->attachements));
$multipart = ($html || sizeof($this->attachments));
$related = false; // default i.e. no embedded images in html

$boundary_part = uniqid('MPM-part-');
$boundary_alt = uniqid('MPM-alt-');

if($html && sizeof($this->attachements)) {
foreach($this->attachements as $i => $a) if($a['cid'] && preg_match("/^image/", $a['mime_type'])) {
$html = preg_replace("/src=(['\"]?)".addslashes($this->attachements[$i]['name'])."(['\"]?)/i",
"src=$1cid:{$this->attachements[$i]['cid']}$2", $html);
$html = preg_replace("/url\((['\"]?)".addslashes($this->attachements[$i]['name'])."(['\"]?)\)/i",
"url(cid:{$this->attachements[$i]['cid']})", $html);
if($html && sizeof($this->attachments)) {
foreach($this->attachments as $i => $a) if($a['cid'] && preg_match("/^image/", $a['mime_type'])) {
$html = preg_replace("/src=(['\"]?)".addslashes($this->attachments[$i]['name'])."(['\"]?)/i",
"src=$1cid:{$this->attachments[$i]['cid']}$2", $html);
$html = preg_replace("/url\((['\"]?)".addslashes($this->attachments[$i]['name'])."(['\"]?)\)/i",
"url(cid:{$this->attachments[$i]['cid']})", $html);
$related = true;
}
}
Expand Down Expand Up @@ -314,7 +315,7 @@ function send($throw_exception = false)

/* attachements */

foreach($this->attachements as $att) {
foreach($this->attachments as $att) {
$att['name'] = "=?UTF-8?B?".base64_encode($this->toUTF8($att['name']))."?=";

$message .= "--$boundary_part\n";
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# MultipartEmail

A simple class to send email with attachments via PHP. Features:
- Send UTF8 email with base64 encoded all parts
- Send email with HTML and text parts (alternative)
- Send email with attachments (multipart)
- Send HTML email with images (embed, multipart related)

Emails is sent by built-in mail() function, no direct connection to SMTP server required. Class uses any input charset: UTF8 (default), Windows-1251 etc.

See example below.

## MultipartEmail (ru)

Максимально простой класс для отправки почты с вложениями на PHP. Возможности:
- Отправка почты в UTF8 с кодированием в base64 всех частей сообщения
- Отправка почты с HTML и текстовой частями (alternative)
Expand All @@ -11,8 +23,7 @@

Пример использования:

<?php
require('MultipartEmail.php');
<?php
$email = new MultipartEmail();
$email->setFrom("Me <[email protected]>");
$email->setSubject("Поздравляем с отправкой почты!");
Expand All @@ -21,4 +32,3 @@
$email->setHtml("<h1>Дорогие товарищи!</h1> <img src=\"image.jpg\"> ...");
$email->addAttachement('/path/to/file.jpg', 'image/jpeg', 'image.jpg', false, true);
$email->send();
?>
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "aleksandr.ru/multipart-email",
"type": "library",
"description": "Multiprt email sender",
"keywords": ["email", "multipart", "related", "alternative"],
"homepage": "https://github.com/Aleksandr-ru/MultipartEmail",
"license": "MIT",
"authors": [
{
"name": "Aleksandr.ru",
"email": "[email protected]",
"homepage": "http://aleksandr.ru",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/Aleksandr-ru/MultipartEmail/issues"
},
"require": {
"php": ">=5.3.0",
"ext-iconv": "*"
},
"autoload": {
"psr-0": {
"MultipartEmail": ""
}
}
}

0 comments on commit 724c452

Please sign in to comment.