// Merge -> 10309

This commit is contained in:
rMalie
2011-11-21 11:05:10 +00:00
parent 3bfd9ad4ca
commit 82c9dead2a
365 changed files with 11185 additions and 1571 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

+47
View File
@@ -0,0 +1,47 @@
// JavaScript Document
$(document).ready(function() {
$('#button_test_mailjet').click(function() {
$("#mailjet_test_ok").hide();
$("#mailjet_test_ko").hide();
$("#div_email_test").show(500);
});
$('#button_send_mailjet').click(function() {
var token = $(this).attr('rel');
$("#button_test_mailjet").hide();
$("#image_ajax_mailjet").show();
$("#div_email_test").hide();
$.ajax({
type: 'GET',
url: "../modules/mailjet/ajax.php",
async: true,
cache: false,
dataType : "html",
data: 'token=' + token + '&mailjet_api_key=' + $("#mailjet_api_key").val() + '&mailjet_secret_key=' + $("#mailjet_secret_key").val() + '&email_from=' + escape($("#email_from").val()),
success: function(html)
{
$("#button_test_mailjet").show();
$("#image_ajax_mailjet").hide();
var retour = html.split("|");
if (retour[0] == "true")
{
$("#mailjet_test_ok").show(500);
} else {
$("#mailjet_activation_no").attr('checked', true);
$("#mailjet_error_message").html(retour[1]);
$("#mailjet_test_ko").show(500);
}
},
error: function(jqxhr, status, errorThrown)
{
$("#mailjet_activation_no").attr('checked', true);
$("#mailjet_error_message").html(errorThrown);
$("#mailjet_test_ko").show(500);
}
});
});
});
+67
View File
@@ -0,0 +1,67 @@
<?php
/*
* Copyright (c) 2011 Mailjet SAS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Dream me up
* @copyright 2011 Mailjet SAS
* @version Release: $Revision: 1.4 $
* @license hhttp://opensource.org/licenses/mit-license MIT License
* International Registred Trademark & Property of Mailjet SAS
*/
require('../../config/config.inc.php');
require('mailjet.php');
if (Tools::getValue('token') == '' || Tools::getValue('token') != Configuration::get('MAILJET_TOKEN'))
die('Invalid Token');
$obj_mailjet = new Mailjet();
$email_from = urldecode($_GET['email_from']);
try {
$sujet = $obj_mailjet->l('Mailjet Test E-mail');
$message = $obj_mailjet->l('Hello').",\r\n\r\n".$obj_mailjet->l('This E-mail confirms you that Mailjet has successfully been installed on your shop.');
$result = Mail::sendMailTest(true, "in.mailjet.com", $message, $sujet, "text/plain", $email_from, $email_from, $_GET['mailjet_api_key'], $_GET['mailjet_secret_key'], $smtpPort = 465, "tls");
if ($result === true)
echo "true";
else
{
if ($result == 999)
$result = $obj_mailjet->l('The E-mail was not successfully sent');
echo "false|".$result;
reset_config_mailjet();
}
} catch(Exception $e) {
echo "false|".$e->getMessage();
reset_config_mailjet();
}
function reset_config_mailjet()
{
Configuration::updateValue('MAILJET_ACTIVATE', 0);
Configuration::updateValue('PS_MAIL_METHOD', 1);
Configuration::updateValue('PS_MAIL_SERVER', "");
Configuration::updateValue('PS_MAIL_USER', "");
Configuration::updateValue('PS_MAIL_PASSWD', "");
Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', "");
Configuration::updateValue('PS_MAIL_SMTP_PORT', 25);
}
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>mailjet</name>
<displayName><![CDATA[Mailjet]]></displayName>
<version><![CDATA[1.0]]></version>
<description><![CDATA[This modules sends through Mailjet all email coming from your Prestashop installation]]></description>
<author><![CDATA[]]></author>
<tab><![CDATA[front_office_features]]></tab>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>
+29
View File
@@ -0,0 +1,29 @@
<?php
global $_MODULE;
$_MODULE = array();
$_MODULE['<{mailjet}prestashop>ajax_64b88c69849c8b362e7f97c6eaab574d'] = 'E-mail de test';
$_MODULE['<{mailjet}prestashop>ajax_8b1a9953c4611296a827abf8c47804d7'] = 'Bonjour';
$_MODULE['<{mailjet}prestashop>ajax_c38b28d6adf75b2b2936de1f2168b6e6'] = 'Cet e-mail vous confirme que Mailjet a été correctement installé sur votre boutique.';
$_MODULE['<{mailjet}prestashop>ajax_cfc9dca01a0503813a5f9585ba6780d1'] = 'L\'e-mail n\'a pas pu être envoyé';
$_MODULE['<{mailjet}prestashop>mailjet_c127c5641733ecedcce7c33ed849401e'] = 'Ce module vous permet d\'envoyer vos e-mail PrestaShop via Mailjet';
$_MODULE['<{mailjet}prestashop>mailjet_9ccb2eaeed6c7e95ecaf108373619300'] = 'Ce module est activé mais la clé API ou la clé secrète ne sont pas correctement remplies.';
$_MODULE['<{mailjet}prestashop>mailjet_64b88c69849c8b362e7f97c6eaab574d'] = 'E-mail de test';
$_MODULE['<{mailjet}prestashop>mailjet_8b1a9953c4611296a827abf8c47804d7'] = 'Bonjour';
$_MODULE['<{mailjet}prestashop>mailjet_c38b28d6adf75b2b2936de1f2168b6e6'] = 'Cet e-mail vous confirme que Mailjet a été correctement installé sur votre boutique.';
$_MODULE['<{mailjet}prestashop>mailjet_cfc9dca01a0503813a5f9585ba6780d1'] = 'L\'e-mail n\'a pas pu être envoyé';
$_MODULE['<{mailjet}prestashop>mailjet_c888438d14855d7d96a2724ee9c306bd'] = 'Configuraiton mise à jour';
$_MODULE['<{mailjet}prestashop>mailjet_9e3f29ea93d121e237085def4ee29f74'] = 'Ce module vous permet d\'envoyer vos e-mail PrestaShop (et ceux de la plupart de vos modules) via Mailjet';
$_MODULE['<{mailjet}prestashop>mailjet_f4f70727dc34561dfde1a3c529b6205c'] = 'Configuration';
$_MODULE['<{mailjet}prestashop>mailjet_6ad7efc596f5720291e748ff2185733f'] = 'Clé API Mailjet';
$_MODULE['<{mailjet}prestashop>mailjet_a02496bddaea8e6616bf285fc37973be'] = 'Clé secrète Mailjet';
$_MODULE['<{mailjet}prestashop>mailjet_ed1d12d25d1a1ec139c84015ae766a7f'] = 'Envoyer vos e-mails via Mailjet';
$_MODULE['<{mailjet}prestashop>mailjet_93cba07454f06a4a960172bbd6e2a435'] = 'Oui';
$_MODULE['<{mailjet}prestashop>mailjet_bafd7322c6e97d25b6299b5d6fe8920b'] = 'Non';
$_MODULE['<{mailjet}prestashop>mailjet_63ade95d66cda885393aab6f5f5e4e76'] = 'Authentification réussie ! Votre configuration est correcte.';
$_MODULE['<{mailjet}prestashop>mailjet_424ab525513861737da68b0700b5cd20'] = 'Une erreur est survenue :';
$_MODULE['<{mailjet}prestashop>mailjet_79dfc1f365ed194bb9fc0da20f1a3857'] = 'Si vous ne comprenez pas cette erreur, veuillez contacter';
$_MODULE['<{mailjet}prestashop>mailjet_d496cec7c628e4438ee719315942e2d6'] = ' E-mail de / à :';
$_MODULE['<{mailjet}prestashop>mailjet_94966d90747b97d1f0f206c98a8b1ac3'] = 'Envoyer';
$_MODULE['<{mailjet}prestashop>mailjet_540caa299a719ee320cf416ffae7adca'] = 'Test de configuration';
$_MODULE['<{mailjet}prestashop>mailjet_d4dccb8ca2dac4e53c01bd9954755332'] = 'Sauvegarder la configuration';
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

+213
View File
@@ -0,0 +1,213 @@
<?php
/*
* Copyright (c) 2011 Mailjet SAS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Dream me up
* @copyright 2011 Mailjet SAS
* @version Release: $Revision: 1.4 $
* @license hhttp://opensource.org/licenses/mit-license MIT License
* International Registred Trademark & Property of Mailjet SAS
*/
// Security
if (!defined('_PS_VERSION_'))
exit;
class Mailjet extends Module
{
/*
** Construct Method
**
*/
public function __construct()
{
$this->name = 'mailjet';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->displayName = 'Mailjet';
parent::__construct();
$this->description = $this->l('This modules sends through Mailjet all email coming from your Prestashop installation');
if (Configuration::get('MAILJET_ACTIVATE') == 1 && (strlen(Configuration::get('MAILJET_API_KEY')) < 3 || strlen(Configuration::get('MAILJET_SECRET_KEY')) < 3))
$this->warning = $this->l('The module is activated but api key or secret key are not correctly set.');
// Defines ajax lang variables in way to translate them
$this->l('Mailjet Test E-mail');
$this->l('Hello');
$this->l('This E-mail confirms you that Mailjet has successfully been installed on your shop.');
$this->l('The E-mail was not successfully sent');
}
/*
** Install / Uninstall Methods
**
*/
public function install()
{
// Can't do anything else for retrocompatibility
if (md5_file(dirname(__FILE__).'/override/Message.php') != md5_file(dirname(__FILE__).'/../../tools/swift/Swift/Message.php'))
return false;
if (!@copy(dirname(__FILE__).'/override/Message-mailjet.php', dirname(__FILE__).'/../../tools/swift/Swift/Message.php'))
return false;
// Create Token
Configuration::updateValue('MAILJET_TOKEN', md5(rand()));
// Install module
if (!parent::install())
return false;
return true;
}
public function uninstall()
{
// Can't do anything else for retrocompatibility
if (md5_file(dirname(__FILE__).'/override/Message-mailjet.php') != md5_file(dirname(__FILE__).'/../../tools/swift/Swift/Message.php'))
return false;
if (!@copy(dirname(__FILE__).'/override/Message.php', dirname(__FILE__).'/../../tools/swift/Swift/Message.php'))
return false;
// Uninstall module
Configuration::updateValue('PS_MAIL_METHOD', 1);
Configuration::updateValue('PS_MAIL_SERVER', "");
Configuration::updateValue('PS_MAIL_USER', "");
Configuration::updateValue('PS_MAIL_PASSWD', "");
Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', "");
Configuration::updateValue('PS_MAIL_SMTP_PORT', 25);
if (!Configuration::deleteByName('MAILJET_TOKEN') OR !Configuration::deleteByName('MAILJET_SECRET_KEY') OR !Configuration::deleteByName('MAILJET_API_KEY') OR !parent::uninstall())
return false;
return true;
}
/*
** Form Config Methods
**
*/
public function getContent()
{
global $cookie;
$lang = new Language((int)($cookie->id_lang));
if (!in_array($lang->iso_code, array('fr', 'en', 'es')))
$lang->iso_code = 'en';
$output = '<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/'.$this->name.'/ajax.js"></script>
<p style="margin-bottom: 5px;"><img src="'.__PS_BASE_URI__.'modules/'.$this->name.'/logo-mailjet.jpg" alt="" /></p>';
if (Tools::isSubmit('submitMailjet'))
{
Configuration::updateValue('MAILJET_API_KEY', pSQL(Tools::getValue('mailjet_api_key')));
Configuration::updateValue('MAILJET_SECRET_KEY', pSQL(Tools::getValue('mailjet_secret_key')));
Configuration::updateValue('MAILJET_ACTIVATE', (int)(Tools::getValue('mailjet_activation')));
// If mailjet activation, let's configure
if ((int)Tools::getValue('mailjet_activation') == 1)
{
Configuration::updateValue('PS_MAIL_METHOD', 2);
Configuration::updateValue('PS_MAIL_SERVER', "in.mailjet.com");
Configuration::updateValue('PS_MAIL_USER', pSQL(Configuration::get('MAILJET_API_KEY')));
Configuration::updateValue('PS_MAIL_PASSWD', pSQL(Configuration::get('MAILJET_SECRET_KEY')));
Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', "tls");
Configuration::updateValue('PS_MAIL_SMTP_PORT', 465);
}
else
{
Configuration::updateValue('PS_MAIL_METHOD', 1);
Configuration::updateValue('PS_MAIL_SERVER', "");
Configuration::updateValue('PS_MAIL_USER', "");
Configuration::updateValue('PS_MAIL_PASSWD', "");
Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', "");
Configuration::updateValue('PS_MAIL_SMTP_PORT', 25);
}
$output .= '
<div class="conf confirm">
<img src="../img/admin/ok.gif" alt="" title="" />
'.$this->l('Settings updated').'
</div>';
}
$chk_yes = "";
$chk_no = " checked=\"checked\"";
if ((int)(Tools::getValue('mailjet_activation', Configuration::get('MAILJET_ACTIVATE'))) == 1)
{
$chk_yes = ' checked="checked"';
$chk_no = '';
}
$output .= '
<div>
<p style="margin-bottom:10px;">
<b>'.$this->l('This module sends through Mailjet all email coming from your Prestashop installation (and most third party modules)').'.</b>
</p>
<form action="'.htmlentities($_SERVER['REQUEST_URI']).'" method="post">
<fieldset>
<legend><img src="../img/admin/cog.gif" alt="" class="middle" />'.$this->l('Settings').'</legend>
<label>'.$this->l('Mailjet API Key:').'</label>
<div class="margin-form">
<input type="text" name="mailjet_api_key" id="mailjet_api_key" size="30" value="'.htmlentities(Tools::getValue('mailjet_api_key', Configuration::get('MAILJET_API_KEY'))).'" />
</div>
<hr size="1" style="margin-bottom: 20px;" noshade />
<label>'.$this->l('Mailjet Secret Key').'</label>
<div class="margin-form">
<input type="text" name="mailjet_secret_key" id="mailjet_secret_key" size="30" value="'.htmlentities(Tools::getValue('mailjet_secret_key', Configuration::get('MAILJET_SECRET_KEY'))).'" />
</div>
<hr size="1" style="margin-bottom: 20px;" noshade />
<label style="vertical-align: middle;">'.$this->l('Send Email through Mailjet:').'</label>
<div class="margin-form" style="margin-top: 5px;">
<input type="radio" name="mailjet_activation" value="1" style="vertical-align: middle;" '.$chk_yes.' /> '.$this->l('Yes').'&nbsp;
<input type="radio" name="mailjet_activation" id="mailjet_activation_no" value="0" style="vertical-align: middle;" '.$chk_no.' /> '.$this->l('No').'
</div>
<hr size="1" style="margin-bottom: 20px;" noshade />
<div class="conf confirm" id="mailjet_test_ok" style="display:none">
<img src="../img/admin/ok.gif" alt="" title="" />
'.$this->l('Authentication successful ! Your configuration is correct.').'
</div>
<div class="conf error" id="mailjet_test_ko" style="display:none">
<img src="../img/admin/forbbiden.gif" alt="" title="" />
'.$this->l('An Error has occured : ').'<span id="mailjet_error_message"></span>
<p>'.$this->l('If you don\'t understand this error please contact').' <a href="http://fr.mailjet.com/support" target="_blank">Mailjet Support</a></p>
</div>
<div id="div_email_test" style="display:none">
<p style="text-align:center">'.$this->l('E-mail From / to :').'&nbsp;<input type="text" id="email_from" value="'.htmlentities(Configuration::get('PS_SHOP_EMAIL')).'" size="40" />&nbsp;<input type="button" name="sendTestMailjet" value="'.$this->l('Send').'" class="button" rel="'.htmlentities(Configuration::get('MAILJET_TOKEN')).'" id="button_send_mailjet" /></p>
<hr size="1" style="margin-bottom: 20px;" noshade />
</div>
<center><input type="button" name="testMailjet" value="'.$this->l('Test Configuration').'" class="button" id="button_test_mailjet" /><img src="'.__PS_BASE_URI__.'modules/'.$this->name.'/ajax-mailjet.gif" id="image_ajax_mailjet" style="display:none" />&nbsp;<input type="submit" name="submitMailjet" value="'.$this->l('Save settings').'" class="button" /></center>
</fieldset>
</form>
</div>';
return $output;
}
}
+798
View File
@@ -0,0 +1,798 @@
<?php
/**
* Swift Mailer Message Component
* Composes MIME 1.0 messages meeting various RFC standards
* Deals with attachments, embedded images, multipart bodies, forwarded messages...
* Please read the LICENSE file
* @copyright Chris Corbyn <chris@w3style.co.uk>
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Message
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/ClassLoader.php";
Swift_ClassLoader::load("Swift_Address");
Swift_ClassLoader::load("Swift_Message_Mime");
Swift_ClassLoader::load("Swift_Message_Image");
Swift_ClassLoader::load("Swift_Message_Part");
/**
* Swift Message class
* @package Swift_Message
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Message extends Swift_Message_Mime
{
/**
* Constant from a high priority message (pretty meaningless)
*/
const PRIORITY_HIGH = 1;
/**
* Constant for a low priority message
*/
const PRIORITY_LOW = 5;
/**
* Constant for a normal priority message
*/
const PRIORITY_NORMAL = 3;
/**
* The MIME warning for client not supporting multipart content
* @var string
*/
protected $mimeWarning = null;
/**
* The version of the library (Swift) if known.
* @var string
*/
protected $libVersion = "";
/**
* A container for references to other objects.
* This is used in some very complex logic when sub-parts get shifted around.
* @var array
*/
protected $references = array(
"parent" => array("alternative" => null, "mixed" => null, "related" => null),
"alternative" => array(),
"mixed" => array(),
"related" => array()
);
/**
* Ctor.
* @param string Message subject
* @param string Body
* @param string Content-type
* @param string Encoding
* @param string Charset
*/
public function __construct($subject="", $body=null, $type="text/plain", $encoding=null, $charset=null)
{
parent::__construct();
if (function_exists("date_default_timezone_set") && function_exists("date_default_timezone_get"))
{
date_default_timezone_set(@date_default_timezone_get());
}
$this->setReturnPath(null);
$this->setTo("");
$this->setFrom("");
$this->setCc(null);
$this->setBcc(null);
$this->setReplyTo(null);
$this->setSubject($subject);
$this->setDate(time());
if (defined("Swift::VERSION"))
{
$this->libVersion = Swift::VERSION;
$this->headers->set("X-LibVersion", $this->libVersion);
}
$this->headers->set("MIME-Version", "1.0");
$this->headers->set("X-Mailjet-Partner", "prestashop");
$this->setContentType($type);
$this->setCharset($charset);
$this->setFlowed(true);
$this->setEncoding($encoding);
foreach (array_keys($this->references["parent"]) as $key)
{
$this->setReference("parent", $key, $this);
}
$this->setMimeWarning(
"This is a message in multipart MIME format. Your mail client should not be displaying this. " .
"Consider upgrading your mail client to view this message correctly."
);
if ($body !== null)
{
$this->setData($body);
if ($charset === null)
{
Swift_ClassLoader::load("Swift_Message_Encoder");
if (Swift_Message_Encoder::instance()->isUTF8($body)) $this->setCharset("utf-8");
else $this->setCharset("iso-8859-1");
}
}
}
/**
* Sets a reference so when nodes are nested, operations can be redirected.
* This really should be refactored to use just one array rather than dynamic variables.
* @param string Key 1
* @param string Key 2
* @param Object Reference
*/
protected function setReference($where, $key, $ref)
{
if ($ref === $this) $this->references[$where][$key] = false;
else $this->references[$where][$key] = $ref;
}
/**
* Get a reference to an object (for complex reasons).
* @param string Key 1
* @param string Key 2
* @return Object
*/
protected function getReference($where, $key)
{
if (!$this->references[$where][$key]) return $this;
else return $this->references[$where][$key];
}
/**
* Get the level in the MIME hierarchy at which this section should appear.
* @return string
*/
public function getLevel()
{
return Swift_Message_Mime::LEVEL_TOP;
}
/**
* Set the message id literally.
* Unless you know what you are doing you should be using generateId() rather than this method,
* otherwise you may break compliancy with RFC 2822.
* @param string The message ID string.
*/
public function setId($id)
{
$this->headers->set("Message-ID", $id);
}
/**
* Create a RFC 2822 compliant message id, optionally based upon $idstring.
* The message ID includes information about the current time, the server and some random characters.
* @param string An optional string the base the ID on
* @return string The generated message ID, including the <> quotes.
* @author Cristian Rodriguez <judas.iscariote@flyspray.org>
*/
public function generateId($idstring=null)
{
$midparams = array(
"utctime" => gmstrftime("%Y%m%d%H%M%S"),
"pid" => getmypid(),
"randint" => mt_rand(),
"customstr" => (preg_match("/^(?<!\\.)[a-z0-9\\.]+(?!\\.)\$/iD", $idstring) ? $idstring : "swift") ,
"hostname" => (isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : php_uname("n")),
);
$this->setId(vsprintf("<%s.%d.%d.%s@%s>", $midparams));
return $this->getId();
}
/**
* Get the generated message ID for this message, including the <> quotes.
* If generated automatically, or using generateId() this method returns a RFC2822 compliant Message-ID.
* @return string
* @author Cristian Rodriguez <judas.iscariote@flyspray.org>
*/
public function getId()
{
return $this->headers->has("Message-ID") ? $this->headers->get("Message-ID") : null;
}
/**
* Set the address in the Return-Path: header
* @param string The bounce-detect address
*/
public function setReturnPath($address)
{
if ($address instanceof Swift_Address) $address = $address->build(true);
$this->headers->set("Return-Path", $address);
}
/**
* Return the address used in the Return-Path: header
* @return string
* @param boolean Return the address for SMTP command
*/
public function getReturnPath($smtp=false)
{
if ($this->headers->has("Return-Path"))
{
if (!$smtp) return $this->headers->get("Return-Path");
else
{
$path = $this->headers->get("Return-Path");
if (strpos($path, ">") > strpos($path, "<")) return substr($path, ($start = strpos($path, "<")), ($start + strrpos($path, ">") + 1));
else return "<" . $path . ">";
}
}
}
/**
* Set the address in the From: header
* @param string The address to set as From
*/
public function setFrom($from)
{
if ($from instanceof Swift_Address) $from = $from->build();
$this->headers->set("From", $from);
}
/**
* Get the address used in the From: header
* @return string
*/
public function getFrom()
{
if ($this->headers->has("From")) return $this->headers->get("From");
}
/**
* Set the list of recipients in the To: header
* @param mixed An array or a string
*/
public function setTo($to)
{
if ($to)
{
if (!is_array($to)) $to = array($to);
foreach ($to as $key => $value)
{
if ($value instanceof Swift_Address) $to[$key] = $value->build();
}
}
$this->headers->set("To", $to);
}
/**
* Return the list of recipients in the To: header
* @return array
*/
public function getTo()
{
if ($this->headers->has("To"))
{
$to = $this->headers->get("To");
if ($to == "") return array();
else return (array) $to;
}
}
/**
* Set the list of recipients in the Reply-To: header
* @param mixed An array or a string
*/
public function setReplyTo($replyto)
{
if ($replyto)
{
if (!is_array($replyto)) $replyto = array($replyto);
foreach ($replyto as $key => $value)
{
if ($value instanceof Swift_Address) $replyto[$key] = $value->build();
}
}
$this->headers->set("Reply-To", $replyto);
}
/**
* Return the list of recipients in the Reply-To: header
* @return array
*/
public function getReplyTo()
{
if ($this->headers->has("Reply-To"))
{
$reply_to = $this->headers->get("Reply-To");
if ($reply_to == "") return array();
else return (array) $reply_to;
}
}
/**
* Set the list of recipients in the Cc: header
* @param mixed An array or a string
*/
public function setCc($cc)
{
if ($cc)
{
if (!is_array($cc)) $cc = array($cc);
foreach ($cc as $key => $value)
{
if ($value instanceof Swift_Address) $cc[$key] = $value->build();
}
}
$this->headers->set("Cc", $cc);
}
/**
* Return the list of recipients in the Cc: header
* @return array
*/
public function getCc()
{
if ($this->headers->has("Cc"))
{
$cc = $this->headers->get("Cc");
if ($cc == "") return array();
else return (array) $cc;
}
}
/**
* Set the list of recipients in the Bcc: header
* @param mixed An array or a string
*/
public function setBcc($bcc)
{
if ($bcc)
{
if (!is_array($bcc)) $bcc = array($bcc);
foreach ($bcc as $key => $value)
{
if ($value instanceof Swift_Address) $bcc[$key] = $value->build();
}
}
$this->headers->set("Bcc", $bcc);
}
/**
* Return the list of recipients in the Bcc: header
* @return array
*/
public function getBcc()
{
if ($this->headers->has("Bcc"))
{
$bcc = $this->headers->get("Bcc");
if ($bcc == "") return array();
else return (array) $bcc;
}
}
/**
* Set the subject in the headers
* @param string The subject of the email
*/
public function setSubject($subject)
{
$this->headers->set("Subject", $subject);
}
/**
* Get the current subject used in the headers
* @return string
*/
public function getSubject()
{
return $this->headers->get("Subject");
}
/**
* Set the date in the headers in RFC 2822 format
* @param int The time as a UNIX timestamp
*/
public function setDate($date)
{
$this->headers->set("Date", date("r", $date));
}
/**
* Get the date as it looks in the headers
* @return string
*/
public function getDate()
{
return strtotime($this->headers->get("Date"));
}
/**
* Set the charset of the document
* @param string The charset used
*/
public function setCharset($charset)
{
$this->headers->setAttribute("Content-Type", "charset", $charset);
if (($this->getEncoding() == "7bit") && (strtolower($charset) == "utf-8" || strtolower($charset) == "utf8")) $this->setEncoding("8bit");
}
/**
* Get the charset used in the document
* Returns null if none is set
* @return string
*/
public function getCharset()
{
if ($this->headers->hasAttribute("Content-Type", "charset"))
{
return $this->headers->getAttribute("Content-Type", "charset");
}
else
{
return null;
}
}
/**
* Set the "format" attribute to flowed
* @param boolean On or Off
*/
public function setFlowed($flowed=true)
{
$value = null;
if ($flowed) $value = "flowed";
$this->headers->setAttribute("Content-Type", "format", $value);
}
/**
* Check if the message format is set as flowed
* @return boolean
*/
public function isFlowed()
{
if ($this->headers->hasAttribute("Content-Type", "format")
&& $this->headers->getAttribute("Content-Type", "format") == "flowed")
{
return true;
}
else return false;
}
/**
* Set the message prioirty in the mail client (don't rely on this)
* @param int The priority as a value between 1 (high) and 5 (low)
*/
public function setPriority($priority)
{
$priority = (int) $priority;
if ($priority > self::PRIORITY_LOW) $priority = self::PRIORITY_LOW;
if ($priority < self::PRIORITY_HIGH) $priority = self::PRIORITY_HIGH;
$label = array(1 => "High", 2 => "High", 3 => "Normal", 4 => "Low", 5 => "Low");
$this->headers->set("X-Priority", $priority);
$this->headers->set("X-MSMail-Priority", $label[$priority]);
$this->headers->set("X-MimeOLE", "Produced by SwiftMailer " . $this->libVersion);
}
/**
* Request that the client send back a read-receipt (don't rely on this!)
* @param string Request address
*/
public function requestReadReceipt($request)
{
if ($request instanceof Swift_Address) $request = $request->build();
if (!$request)
{
$this->headers->set("Disposition-Notification-To", null);
$this->headers->set("X-Confirm-Reading-To", null);
$this->headers->set("Return-Receipt-To", null);
}
else
{
$this->headers->set("Disposition-Notification-To", $request);
$this->headers->set("X-Confirm-Reading-To", $request);
$this->headers->set("Return-Receipt-To", $request);
}
}
/**
* Check if a read receipt has been requested for this message
* @return boolean
*/
public function wantsReadReceipt()
{
return $this->headers->has("Disposition-Notification-To");
}
/**
* Get the current message priority
* Returns NULL if none set
* @return int
*/
public function getPriority()
{
if ($this->headers->has("X-Priority")) return $this->headers->get("X-Priority");
else return null;
}
/**
* Alias for setData()
* @param mixed Body
*/
public function setBody($body)
{
$this->setData($body);
}
/**
* Alias for getData()
* @return mixed The document body
*/
public function getBody()
{
return $this->getData();
}
/**
* Set the MIME warning message which is displayed to old clients
* @var string The full warning message (in 7bit ascii)
*/
public function setMimeWarning($text)
{
$this->mimeWarning = (string) $text;
}
/**
* Get the MIME warning which is displayed to old clients
* @return string
*/
public function getMimeWarning()
{
return $this->mimeWarning;
}
/**
* Attach a mime part or an attachment of some sort
* Any descendant of Swift_Message_Mime can be added safely (including other Swift_Message objects for mail forwarding!!)
* @param Swift_Message_Mime The document to attach
* @param string An identifier to use (one is returned otherwise)
* @return string The identifier for the part
*/
public function attach(Swift_Message_Mime $child, $id=null)
{
try {
switch ($child->getLevel())
{
case Swift_Message_Mime::LEVEL_ALTERNATIVE:
$sign = (strtolower($child->getContentType()) == "text/plain") ? -1 : 1;
$id = $this->getReference("parent", "alternative")->addChild($child, $id, $sign);
$this->setReference("alternative", $id, $child);
break;
case Swift_Message_Mime::LEVEL_RELATED:
$id = "cid:" . $child->getContentId();
$id = $this->getReference("parent", "related")->addChild($child, $id, 1);
$this->setReference("related", $id, $child);
break;
case Swift_Message_Mime::LEVEL_MIXED: default:
$id = $this->getReference("parent", "mixed")->addChild($child, $id, 1);
$this->setReference("mixed", $id, $child);
break;
}
$this->postAttachFixStructure();
$this->fixContentType();
return $id;
} catch (Swift_Message_MimeException $e) {
throw new Swift_Message_MimeException("Something went wrong whilst trying to move some MIME parts during an attach(). " .
"The MIME component threw an exception:<br />" . $e->getMessage());
}
}
/**
* Remove a nested MIME part
* @param string The ID of the attached part
* @throws Swift_Message_MimeException If no such part exists
*/
public function detach($id)
{
try {
switch (true)
{
case array_key_exists($id, $this->references["alternative"]):
$this->getReference("parent", "alternative")->removeChild($id);
unset($this->references["alternative"][$id]);
break;
case array_key_exists($id, $this->references["related"]):
$this->getReference("parent", "related")->removeChild($id);
unset($this->references["related"][$id]);
break;
case array_key_exists($id, $this->references["mixed"]):
$this->getReference("parent", "mixed")->removeChild($id);
unset($this->references["mixed"][$id]);
break;
default:
throw new Swift_Message_MimeException("Unable to detach part identified by ID '" . $id . "' since it's not registered.");
break;
}
$this->postDetachFixStructure();
$this->fixContentType();
} catch (Swift_Message_MimeException $e) {
throw new Swift_Message_MimeException("Something went wrong whilst trying to move some MIME parts during a detach(). " .
"The MIME component threw an exception:<br />" . $e->getMessage());
}
}
/**
* Sets the correct content type header by looking at what types of data we have set
*/
protected function fixContentType()
{
if (!empty($this->references["mixed"])) $this->setContentType("multipart/mixed");
elseif (!empty($this->references["related"])) $this->setContentType("multipart/related");
elseif (!empty($this->references["alternative"])) $this->setContentType("multipart/alternative");
}
/**
* Move a branch of the tree, containing all it's MIME parts onto another branch
* @param string The content type on the branch itself
* @param string The content type which may exist in the branch's parent
* @param array The array containing all the nodes presently
* @param string The location of the branch now
* @param string The location of the branch after moving
* @param string The key to identify the branch by in it's new location
*/
protected function moveBranchIn($type, $nested_type, $from, $old_branch, $new_branch, $tag)
{
$new = new Swift_Message_Part();
$new->setContentType($type);
$this->getReference("parent", $new_branch)->addChild($new, $tag, -1);
switch ($new_branch)
{
case "related": $this->setReference("related", $tag, $new);//relatedRefs[$tag] = $new;
break;
case "mixed": $this->setReference("mixed", $tag, $new);//mixedRefs[$tag] = $new;
break;
}
foreach ($from as $id => $ref)
{
if (!$ref) $ref = $this;
$sign = (strtolower($ref->getContentType()) == "text/plain"
|| strtolower($ref->getContentType()) == $nested_type) ? -1 : 1;
switch ($new_branch)
{
case "related": $this->getReference("related", $tag)->addChild($ref, $id, $sign);
break;
case "mixed": $this->getReference("mixed", $tag)->addChild($ref, $id, $sign);
break;
}
$this->getReference("parent", $old_branch)->removeChild($id);
}
$this->setReference("parent", $old_branch, $new); //parentRefs[$old_branch] = $new;
}
/**
* Analyzes the mixing of MIME types in a mulitpart message an re-arranges if needed
* It looks complicated and long winded but the concept is pretty simple, even if putting it
* in code does me make want to cry!
*/
protected function postAttachFixStructure()
{
switch (true)
{
case (!empty($this->references["mixed"]) && !empty($this->references["related"]) && !empty($this->references["alternative"])):
if (!isset($this->references["related"]["_alternative"]))
{
$this->moveBranchIn(
"multipart/alternative", "multipart/alternative", $this->references["alternative"], "alternative", "related", "_alternative");
}
if (!isset($this->references["mixed"]["_related"]))
{
$this->moveBranchIn(
"multipart/related", "multipart/alternative", $this->references["related"], "related", "mixed", "_related");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["related"])):
if (!isset($this->references["mixed"]["_related"]))
{
$this->moveBranchIn(
"multipart/related", "multipart/related", $this->references["related"], "related", "mixed", "_related");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["alternative"])):
if (!isset($this->references["mixed"]["_alternative"]))
{
$this->moveBranchIn(
"multipart/alternative", null, $this->references["alternative"], "alternative", "mixed", "_alternative");
}
break;
case (!empty($this->references["related"]) && !empty($this->references["alternative"])):
if (!isset($this->references["related"]["_alternative"]))
{
$this->moveBranchIn(
"multipart/alternative", "multipart/alternative", $this->references["alternative"], "alternative", "related", "_alternative");
}
break;
}
}
/**
* Move a branch further toward the top of the tree
* @param array The array containing MIME parts from the old branch
* @param string The name of the old branch
* @param string The name of the new branch
* @param string The key of the branch being moved
*/
protected function moveBranchOut($from, $old_branch, $new_branch, $tag)
{
foreach ($from as $id => $ref)
{
if (!$ref) $ref = $this;
$sign = (strtolower($ref->getContentType()) == "text/html"
|| strtolower($ref->getContentType()) == "multipart/alternative") ? -1 : 1;
$this->getReference("parent", $new_branch)->addChild($ref, $id, $sign);
switch ($new_branch)
{
case "related": $this->getReference("related", $tag)->removeChild($id);
break;
case "mixed": $this->getReference("parent", $old_branch)->removeChild($id);
break;
}
}
$this->getReference("parent", $new_branch)->removeChild($tag);
$mixed = $this->getReference("parent", $new_branch);//parentRefs[$new_branch];
$this->setReference("parent", $old_branch, $mixed);//parentRefs[$old_branch] = $mixed;
switch ($new_branch)
{
case "related": unset($this->references["related"][$tag]);
break;
case "mixed": unset($this->references["mixed"][$tag]);
break;
}
}
/**
* Analyzes the mixing of MIME types in a mulitpart message an re-arranges if needed
* It looks complicated and long winded but the concept is pretty simple, even if putting it
* in code does me make want to cry!
*/
protected function postDetachFixStructure()
{
switch (true)
{
case (!empty($this->references["mixed"]) && !empty($this->references["related"]) && !empty($this->references["alternative"])):
if (array_keys($this->references["related"]) == array("_alternative"))
{
$alt = $this->getReference("parent", "related")->getChild("_alternative");
$this->getReference("parent", "mixed")->addChild($alt, "_alternative", -1);
$this->setReference("mixed", "_alternative", $alt);//mixedRefs["_alternative"] = $alt;
$this->getReference("parent", "related")->removeChild("_alternative");
unset($this->references["related"]["_alternative"]);
$this->getReference("parent", "mixed")->removeChild("_related");
unset($this->references["mixed"]["_related"]);
}
if (array_keys($this->references["mixed"]) == array("_related"))
{
$this->moveBranchOut($this->references["related"], "related", "mixed", "_related");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["related"])):
if (array_keys($this->references["mixed"]) == array("_related"))
{
$this->moveBranchOut($this->references["related"], "related", "mixed", "_related");
}
if (isset($this->references["related"]["_alternative"]))
{
$this->detach("_alternative");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["alternative"])):
if (array_keys($this->references["mixed"]) == array("_alternative"))
{
$this->moveBranchOut($this->references["alternative"], "alternative", "mixed", "_alternative");
}
break;
case (!empty($this->references["related"]) && !empty($this->references["alternative"])):
if (array_keys($this->references["related"]) == array("_alternative"))
{
$this->moveBranchOut($this->references["alternative"], "alternative", "related", "_alternative");
}
break;
case (!empty($this->references["mixed"])):
if (isset($this->references["mixed"]["_related"])) $this->detach("_related");
case (!empty($this->references["related"])):
if (isset($this->references["related"]["_alternative"]) || isset($this->references["mixed"]["_alternative"]))
$this->detach("_alternative");
break;
}
}
/**
* Execute needed logic prior to compilation
*/
public function preBuild()
{
$data = $this->getData();
if (!($enc = $this->getEncoding()))
{
$this->setEncoding("8bit");
}
if ($this->getCharset() === null && !$this->numChildren())
{
Swift_ClassLoader::load("Swift_Message_Encoder");
if (is_string($data) && Swift_Message_Encoder::instance()->isUTF8($data))
{
$this->setCharset("utf-8");
}
elseif(is_string($data) && Swift_Message_Encoder::instance()->is7BitAscii($data))
{
$this->setCharset("us-ascii");
if (!$enc) $this->setEncoding("7bit");
}
else $this->setCharset("iso-8859-1");
}
elseif ($this->numChildren())
{
if (!$this->getData())
{
$this->setData($this->getMimeWarning());
$this->setLineWrap(76);
}
if ($this->getCharset() !== null) $this->setCharset(null);
if ($this->isFlowed()) $this->setFlowed(false);
$this->setEncoding("7bit");
}
}
}
+797
View File
@@ -0,0 +1,797 @@
<?php
/**
* Swift Mailer Message Component
* Composes MIME 1.0 messages meeting various RFC standards
* Deals with attachments, embedded images, multipart bodies, forwarded messages...
* Please read the LICENSE file
* @copyright Chris Corbyn <chris@w3style.co.uk>
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Message
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/ClassLoader.php";
Swift_ClassLoader::load("Swift_Address");
Swift_ClassLoader::load("Swift_Message_Mime");
Swift_ClassLoader::load("Swift_Message_Image");
Swift_ClassLoader::load("Swift_Message_Part");
/**
* Swift Message class
* @package Swift_Message
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Message extends Swift_Message_Mime
{
/**
* Constant from a high priority message (pretty meaningless)
*/
const PRIORITY_HIGH = 1;
/**
* Constant for a low priority message
*/
const PRIORITY_LOW = 5;
/**
* Constant for a normal priority message
*/
const PRIORITY_NORMAL = 3;
/**
* The MIME warning for client not supporting multipart content
* @var string
*/
protected $mimeWarning = null;
/**
* The version of the library (Swift) if known.
* @var string
*/
protected $libVersion = "";
/**
* A container for references to other objects.
* This is used in some very complex logic when sub-parts get shifted around.
* @var array
*/
protected $references = array(
"parent" => array("alternative" => null, "mixed" => null, "related" => null),
"alternative" => array(),
"mixed" => array(),
"related" => array()
);
/**
* Ctor.
* @param string Message subject
* @param string Body
* @param string Content-type
* @param string Encoding
* @param string Charset
*/
public function __construct($subject="", $body=null, $type="text/plain", $encoding=null, $charset=null)
{
parent::__construct();
if (function_exists("date_default_timezone_set") && function_exists("date_default_timezone_get"))
{
date_default_timezone_set(@date_default_timezone_get());
}
$this->setReturnPath(null);
$this->setTo("");
$this->setFrom("");
$this->setCc(null);
$this->setBcc(null);
$this->setReplyTo(null);
$this->setSubject($subject);
$this->setDate(time());
if (defined("Swift::VERSION"))
{
$this->libVersion = Swift::VERSION;
$this->headers->set("X-LibVersion", $this->libVersion);
}
$this->headers->set("MIME-Version", "1.0");
$this->setContentType($type);
$this->setCharset($charset);
$this->setFlowed(true);
$this->setEncoding($encoding);
foreach (array_keys($this->references["parent"]) as $key)
{
$this->setReference("parent", $key, $this);
}
$this->setMimeWarning(
"This is a message in multipart MIME format. Your mail client should not be displaying this. " .
"Consider upgrading your mail client to view this message correctly."
);
if ($body !== null)
{
$this->setData($body);
if ($charset === null)
{
Swift_ClassLoader::load("Swift_Message_Encoder");
if (Swift_Message_Encoder::instance()->isUTF8($body)) $this->setCharset("utf-8");
else $this->setCharset("iso-8859-1");
}
}
}
/**
* Sets a reference so when nodes are nested, operations can be redirected.
* This really should be refactored to use just one array rather than dynamic variables.
* @param string Key 1
* @param string Key 2
* @param Object Reference
*/
protected function setReference($where, $key, $ref)
{
if ($ref === $this) $this->references[$where][$key] = false;
else $this->references[$where][$key] = $ref;
}
/**
* Get a reference to an object (for complex reasons).
* @param string Key 1
* @param string Key 2
* @return Object
*/
protected function getReference($where, $key)
{
if (!$this->references[$where][$key]) return $this;
else return $this->references[$where][$key];
}
/**
* Get the level in the MIME hierarchy at which this section should appear.
* @return string
*/
public function getLevel()
{
return Swift_Message_Mime::LEVEL_TOP;
}
/**
* Set the message id literally.
* Unless you know what you are doing you should be using generateId() rather than this method,
* otherwise you may break compliancy with RFC 2822.
* @param string The message ID string.
*/
public function setId($id)
{
$this->headers->set("Message-ID", $id);
}
/**
* Create a RFC 2822 compliant message id, optionally based upon $idstring.
* The message ID includes information about the current time, the server and some random characters.
* @param string An optional string the base the ID on
* @return string The generated message ID, including the <> quotes.
* @author Cristian Rodriguez <judas.iscariote@flyspray.org>
*/
public function generateId($idstring=null)
{
$midparams = array(
"utctime" => gmstrftime("%Y%m%d%H%M%S"),
"pid" => getmypid(),
"randint" => mt_rand(),
"customstr" => (preg_match("/^(?<!\\.)[a-z0-9\\.]+(?!\\.)\$/iD", $idstring) ? $idstring : "swift") ,
"hostname" => (isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : php_uname("n")),
);
$this->setId(vsprintf("<%s.%d.%d.%s@%s>", $midparams));
return $this->getId();
}
/**
* Get the generated message ID for this message, including the <> quotes.
* If generated automatically, or using generateId() this method returns a RFC2822 compliant Message-ID.
* @return string
* @author Cristian Rodriguez <judas.iscariote@flyspray.org>
*/
public function getId()
{
return $this->headers->has("Message-ID") ? $this->headers->get("Message-ID") : null;
}
/**
* Set the address in the Return-Path: header
* @param string The bounce-detect address
*/
public function setReturnPath($address)
{
if ($address instanceof Swift_Address) $address = $address->build(true);
$this->headers->set("Return-Path", $address);
}
/**
* Return the address used in the Return-Path: header
* @return string
* @param boolean Return the address for SMTP command
*/
public function getReturnPath($smtp=false)
{
if ($this->headers->has("Return-Path"))
{
if (!$smtp) return $this->headers->get("Return-Path");
else
{
$path = $this->headers->get("Return-Path");
if (strpos($path, ">") > strpos($path, "<")) return substr($path, ($start = strpos($path, "<")), ($start + strrpos($path, ">") + 1));
else return "<" . $path . ">";
}
}
}
/**
* Set the address in the From: header
* @param string The address to set as From
*/
public function setFrom($from)
{
if ($from instanceof Swift_Address) $from = $from->build();
$this->headers->set("From", $from);
}
/**
* Get the address used in the From: header
* @return string
*/
public function getFrom()
{
if ($this->headers->has("From")) return $this->headers->get("From");
}
/**
* Set the list of recipients in the To: header
* @param mixed An array or a string
*/
public function setTo($to)
{
if ($to)
{
if (!is_array($to)) $to = array($to);
foreach ($to as $key => $value)
{
if ($value instanceof Swift_Address) $to[$key] = $value->build();
}
}
$this->headers->set("To", $to);
}
/**
* Return the list of recipients in the To: header
* @return array
*/
public function getTo()
{
if ($this->headers->has("To"))
{
$to = $this->headers->get("To");
if ($to == "") return array();
else return (array) $to;
}
}
/**
* Set the list of recipients in the Reply-To: header
* @param mixed An array or a string
*/
public function setReplyTo($replyto)
{
if ($replyto)
{
if (!is_array($replyto)) $replyto = array($replyto);
foreach ($replyto as $key => $value)
{
if ($value instanceof Swift_Address) $replyto[$key] = $value->build();
}
}
$this->headers->set("Reply-To", $replyto);
}
/**
* Return the list of recipients in the Reply-To: header
* @return array
*/
public function getReplyTo()
{
if ($this->headers->has("Reply-To"))
{
$reply_to = $this->headers->get("Reply-To");
if ($reply_to == "") return array();
else return (array) $reply_to;
}
}
/**
* Set the list of recipients in the Cc: header
* @param mixed An array or a string
*/
public function setCc($cc)
{
if ($cc)
{
if (!is_array($cc)) $cc = array($cc);
foreach ($cc as $key => $value)
{
if ($value instanceof Swift_Address) $cc[$key] = $value->build();
}
}
$this->headers->set("Cc", $cc);
}
/**
* Return the list of recipients in the Cc: header
* @return array
*/
public function getCc()
{
if ($this->headers->has("Cc"))
{
$cc = $this->headers->get("Cc");
if ($cc == "") return array();
else return (array) $cc;
}
}
/**
* Set the list of recipients in the Bcc: header
* @param mixed An array or a string
*/
public function setBcc($bcc)
{
if ($bcc)
{
if (!is_array($bcc)) $bcc = array($bcc);
foreach ($bcc as $key => $value)
{
if ($value instanceof Swift_Address) $bcc[$key] = $value->build();
}
}
$this->headers->set("Bcc", $bcc);
}
/**
* Return the list of recipients in the Bcc: header
* @return array
*/
public function getBcc()
{
if ($this->headers->has("Bcc"))
{
$bcc = $this->headers->get("Bcc");
if ($bcc == "") return array();
else return (array) $bcc;
}
}
/**
* Set the subject in the headers
* @param string The subject of the email
*/
public function setSubject($subject)
{
$this->headers->set("Subject", $subject);
}
/**
* Get the current subject used in the headers
* @return string
*/
public function getSubject()
{
return $this->headers->get("Subject");
}
/**
* Set the date in the headers in RFC 2822 format
* @param int The time as a UNIX timestamp
*/
public function setDate($date)
{
$this->headers->set("Date", date("r", $date));
}
/**
* Get the date as it looks in the headers
* @return string
*/
public function getDate()
{
return strtotime($this->headers->get("Date"));
}
/**
* Set the charset of the document
* @param string The charset used
*/
public function setCharset($charset)
{
$this->headers->setAttribute("Content-Type", "charset", $charset);
if (($this->getEncoding() == "7bit") && (strtolower($charset) == "utf-8" || strtolower($charset) == "utf8")) $this->setEncoding("8bit");
}
/**
* Get the charset used in the document
* Returns null if none is set
* @return string
*/
public function getCharset()
{
if ($this->headers->hasAttribute("Content-Type", "charset"))
{
return $this->headers->getAttribute("Content-Type", "charset");
}
else
{
return null;
}
}
/**
* Set the "format" attribute to flowed
* @param boolean On or Off
*/
public function setFlowed($flowed=true)
{
$value = null;
if ($flowed) $value = "flowed";
$this->headers->setAttribute("Content-Type", "format", $value);
}
/**
* Check if the message format is set as flowed
* @return boolean
*/
public function isFlowed()
{
if ($this->headers->hasAttribute("Content-Type", "format")
&& $this->headers->getAttribute("Content-Type", "format") == "flowed")
{
return true;
}
else return false;
}
/**
* Set the message prioirty in the mail client (don't rely on this)
* @param int The priority as a value between 1 (high) and 5 (low)
*/
public function setPriority($priority)
{
$priority = (int) $priority;
if ($priority > self::PRIORITY_LOW) $priority = self::PRIORITY_LOW;
if ($priority < self::PRIORITY_HIGH) $priority = self::PRIORITY_HIGH;
$label = array(1 => "High", 2 => "High", 3 => "Normal", 4 => "Low", 5 => "Low");
$this->headers->set("X-Priority", $priority);
$this->headers->set("X-MSMail-Priority", $label[$priority]);
$this->headers->set("X-MimeOLE", "Produced by SwiftMailer " . $this->libVersion);
}
/**
* Request that the client send back a read-receipt (don't rely on this!)
* @param string Request address
*/
public function requestReadReceipt($request)
{
if ($request instanceof Swift_Address) $request = $request->build();
if (!$request)
{
$this->headers->set("Disposition-Notification-To", null);
$this->headers->set("X-Confirm-Reading-To", null);
$this->headers->set("Return-Receipt-To", null);
}
else
{
$this->headers->set("Disposition-Notification-To", $request);
$this->headers->set("X-Confirm-Reading-To", $request);
$this->headers->set("Return-Receipt-To", $request);
}
}
/**
* Check if a read receipt has been requested for this message
* @return boolean
*/
public function wantsReadReceipt()
{
return $this->headers->has("Disposition-Notification-To");
}
/**
* Get the current message priority
* Returns NULL if none set
* @return int
*/
public function getPriority()
{
if ($this->headers->has("X-Priority")) return $this->headers->get("X-Priority");
else return null;
}
/**
* Alias for setData()
* @param mixed Body
*/
public function setBody($body)
{
$this->setData($body);
}
/**
* Alias for getData()
* @return mixed The document body
*/
public function getBody()
{
return $this->getData();
}
/**
* Set the MIME warning message which is displayed to old clients
* @var string The full warning message (in 7bit ascii)
*/
public function setMimeWarning($text)
{
$this->mimeWarning = (string) $text;
}
/**
* Get the MIME warning which is displayed to old clients
* @return string
*/
public function getMimeWarning()
{
return $this->mimeWarning;
}
/**
* Attach a mime part or an attachment of some sort
* Any descendant of Swift_Message_Mime can be added safely (including other Swift_Message objects for mail forwarding!!)
* @param Swift_Message_Mime The document to attach
* @param string An identifier to use (one is returned otherwise)
* @return string The identifier for the part
*/
public function attach(Swift_Message_Mime $child, $id=null)
{
try {
switch ($child->getLevel())
{
case Swift_Message_Mime::LEVEL_ALTERNATIVE:
$sign = (strtolower($child->getContentType()) == "text/plain") ? -1 : 1;
$id = $this->getReference("parent", "alternative")->addChild($child, $id, $sign);
$this->setReference("alternative", $id, $child);
break;
case Swift_Message_Mime::LEVEL_RELATED:
$id = "cid:" . $child->getContentId();
$id = $this->getReference("parent", "related")->addChild($child, $id, 1);
$this->setReference("related", $id, $child);
break;
case Swift_Message_Mime::LEVEL_MIXED: default:
$id = $this->getReference("parent", "mixed")->addChild($child, $id, 1);
$this->setReference("mixed", $id, $child);
break;
}
$this->postAttachFixStructure();
$this->fixContentType();
return $id;
} catch (Swift_Message_MimeException $e) {
throw new Swift_Message_MimeException("Something went wrong whilst trying to move some MIME parts during an attach(). " .
"The MIME component threw an exception:<br />" . $e->getMessage());
}
}
/**
* Remove a nested MIME part
* @param string The ID of the attached part
* @throws Swift_Message_MimeException If no such part exists
*/
public function detach($id)
{
try {
switch (true)
{
case array_key_exists($id, $this->references["alternative"]):
$this->getReference("parent", "alternative")->removeChild($id);
unset($this->references["alternative"][$id]);
break;
case array_key_exists($id, $this->references["related"]):
$this->getReference("parent", "related")->removeChild($id);
unset($this->references["related"][$id]);
break;
case array_key_exists($id, $this->references["mixed"]):
$this->getReference("parent", "mixed")->removeChild($id);
unset($this->references["mixed"][$id]);
break;
default:
throw new Swift_Message_MimeException("Unable to detach part identified by ID '" . $id . "' since it's not registered.");
break;
}
$this->postDetachFixStructure();
$this->fixContentType();
} catch (Swift_Message_MimeException $e) {
throw new Swift_Message_MimeException("Something went wrong whilst trying to move some MIME parts during a detach(). " .
"The MIME component threw an exception:<br />" . $e->getMessage());
}
}
/**
* Sets the correct content type header by looking at what types of data we have set
*/
protected function fixContentType()
{
if (!empty($this->references["mixed"])) $this->setContentType("multipart/mixed");
elseif (!empty($this->references["related"])) $this->setContentType("multipart/related");
elseif (!empty($this->references["alternative"])) $this->setContentType("multipart/alternative");
}
/**
* Move a branch of the tree, containing all it's MIME parts onto another branch
* @param string The content type on the branch itself
* @param string The content type which may exist in the branch's parent
* @param array The array containing all the nodes presently
* @param string The location of the branch now
* @param string The location of the branch after moving
* @param string The key to identify the branch by in it's new location
*/
protected function moveBranchIn($type, $nested_type, $from, $old_branch, $new_branch, $tag)
{
$new = new Swift_Message_Part();
$new->setContentType($type);
$this->getReference("parent", $new_branch)->addChild($new, $tag, -1);
switch ($new_branch)
{
case "related": $this->setReference("related", $tag, $new);//relatedRefs[$tag] = $new;
break;
case "mixed": $this->setReference("mixed", $tag, $new);//mixedRefs[$tag] = $new;
break;
}
foreach ($from as $id => $ref)
{
if (!$ref) $ref = $this;
$sign = (strtolower($ref->getContentType()) == "text/plain"
|| strtolower($ref->getContentType()) == $nested_type) ? -1 : 1;
switch ($new_branch)
{
case "related": $this->getReference("related", $tag)->addChild($ref, $id, $sign);
break;
case "mixed": $this->getReference("mixed", $tag)->addChild($ref, $id, $sign);
break;
}
$this->getReference("parent", $old_branch)->removeChild($id);
}
$this->setReference("parent", $old_branch, $new); //parentRefs[$old_branch] = $new;
}
/**
* Analyzes the mixing of MIME types in a mulitpart message an re-arranges if needed
* It looks complicated and long winded but the concept is pretty simple, even if putting it
* in code does me make want to cry!
*/
protected function postAttachFixStructure()
{
switch (true)
{
case (!empty($this->references["mixed"]) && !empty($this->references["related"]) && !empty($this->references["alternative"])):
if (!isset($this->references["related"]["_alternative"]))
{
$this->moveBranchIn(
"multipart/alternative", "multipart/alternative", $this->references["alternative"], "alternative", "related", "_alternative");
}
if (!isset($this->references["mixed"]["_related"]))
{
$this->moveBranchIn(
"multipart/related", "multipart/alternative", $this->references["related"], "related", "mixed", "_related");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["related"])):
if (!isset($this->references["mixed"]["_related"]))
{
$this->moveBranchIn(
"multipart/related", "multipart/related", $this->references["related"], "related", "mixed", "_related");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["alternative"])):
if (!isset($this->references["mixed"]["_alternative"]))
{
$this->moveBranchIn(
"multipart/alternative", null, $this->references["alternative"], "alternative", "mixed", "_alternative");
}
break;
case (!empty($this->references["related"]) && !empty($this->references["alternative"])):
if (!isset($this->references["related"]["_alternative"]))
{
$this->moveBranchIn(
"multipart/alternative", "multipart/alternative", $this->references["alternative"], "alternative", "related", "_alternative");
}
break;
}
}
/**
* Move a branch further toward the top of the tree
* @param array The array containing MIME parts from the old branch
* @param string The name of the old branch
* @param string The name of the new branch
* @param string The key of the branch being moved
*/
protected function moveBranchOut($from, $old_branch, $new_branch, $tag)
{
foreach ($from as $id => $ref)
{
if (!$ref) $ref = $this;
$sign = (strtolower($ref->getContentType()) == "text/html"
|| strtolower($ref->getContentType()) == "multipart/alternative") ? -1 : 1;
$this->getReference("parent", $new_branch)->addChild($ref, $id, $sign);
switch ($new_branch)
{
case "related": $this->getReference("related", $tag)->removeChild($id);
break;
case "mixed": $this->getReference("parent", $old_branch)->removeChild($id);
break;
}
}
$this->getReference("parent", $new_branch)->removeChild($tag);
$mixed = $this->getReference("parent", $new_branch);//parentRefs[$new_branch];
$this->setReference("parent", $old_branch, $mixed);//parentRefs[$old_branch] = $mixed;
switch ($new_branch)
{
case "related": unset($this->references["related"][$tag]);
break;
case "mixed": unset($this->references["mixed"][$tag]);
break;
}
}
/**
* Analyzes the mixing of MIME types in a mulitpart message an re-arranges if needed
* It looks complicated and long winded but the concept is pretty simple, even if putting it
* in code does me make want to cry!
*/
protected function postDetachFixStructure()
{
switch (true)
{
case (!empty($this->references["mixed"]) && !empty($this->references["related"]) && !empty($this->references["alternative"])):
if (array_keys($this->references["related"]) == array("_alternative"))
{
$alt = $this->getReference("parent", "related")->getChild("_alternative");
$this->getReference("parent", "mixed")->addChild($alt, "_alternative", -1);
$this->setReference("mixed", "_alternative", $alt);//mixedRefs["_alternative"] = $alt;
$this->getReference("parent", "related")->removeChild("_alternative");
unset($this->references["related"]["_alternative"]);
$this->getReference("parent", "mixed")->removeChild("_related");
unset($this->references["mixed"]["_related"]);
}
if (array_keys($this->references["mixed"]) == array("_related"))
{
$this->moveBranchOut($this->references["related"], "related", "mixed", "_related");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["related"])):
if (array_keys($this->references["mixed"]) == array("_related"))
{
$this->moveBranchOut($this->references["related"], "related", "mixed", "_related");
}
if (isset($this->references["related"]["_alternative"]))
{
$this->detach("_alternative");
}
break;
case (!empty($this->references["mixed"]) && !empty($this->references["alternative"])):
if (array_keys($this->references["mixed"]) == array("_alternative"))
{
$this->moveBranchOut($this->references["alternative"], "alternative", "mixed", "_alternative");
}
break;
case (!empty($this->references["related"]) && !empty($this->references["alternative"])):
if (array_keys($this->references["related"]) == array("_alternative"))
{
$this->moveBranchOut($this->references["alternative"], "alternative", "related", "_alternative");
}
break;
case (!empty($this->references["mixed"])):
if (isset($this->references["mixed"]["_related"])) $this->detach("_related");
case (!empty($this->references["related"])):
if (isset($this->references["related"]["_alternative"]) || isset($this->references["mixed"]["_alternative"]))
$this->detach("_alternative");
break;
}
}
/**
* Execute needed logic prior to compilation
*/
public function preBuild()
{
$data = $this->getData();
if (!($enc = $this->getEncoding()))
{
$this->setEncoding("8bit");
}
if ($this->getCharset() === null && !$this->numChildren())
{
Swift_ClassLoader::load("Swift_Message_Encoder");
if (is_string($data) && Swift_Message_Encoder::instance()->isUTF8($data))
{
$this->setCharset("utf-8");
}
elseif(is_string($data) && Swift_Message_Encoder::instance()->is7BitAscii($data))
{
$this->setCharset("us-ascii");
if (!$enc) $this->setEncoding("7bit");
}
else $this->setCharset("iso-8859-1");
}
elseif ($this->numChildren())
{
if (!$this->getData())
{
$this->setData($this->getMimeWarning());
$this->setLineWrap(76);
}
if ($this->getCharset() !== null) $this->setCharset(null);
if ($this->isFlowed()) $this->setFlowed(false);
$this->setEncoding("7bit");
}
}
}