Files
spot/inc/email.php

106 lines
2.9 KiB
PHP

<?php
class Email extends PhpObject {
private $sServName;
private $sTemplate;
/**
*
* @var Translator[]
*/
private $asTranslators;
/**
*
* @var Mask[]
*/
private $asTemplates;
private $asDests;
public function __construct($sServName, $sTemplate='') {
$this->sServName = $sServName;
$this->setTemplate($sTemplate);
$this->asDests = array();
}
public function setTemplate($sTemplate) {
$this->sTemplate = $sTemplate;
$this->asTranslators = array();
$this->asTemplates = array();
}
private function getTemplate($sLanguage) {
if(!array_key_exists($sLanguage, $this->asTemplates)) {
$this->asTranslators[$sLanguage] = new Translator($sLanguage);
$this->buildTemplate($sLanguage);
}
return array('subject'=>$this->asTranslators[$sLanguage]->getTranslation('conf_subject'), 'email'=>$this->asTemplates[$sLanguage]);
}
private function buildTemplate($sLanguage) {
$oTemplate = new Mask($this->sTemplate, $this->asTranslators[$sLanguage]);
switch($this->sTemplate) {
case 'confirmation':
break;
case 'update':
break;
}
$this->asTemplates[$sLanguage] = $oTemplate;
}
/**
* Set Target User Info
* @param array $asDests Contains: id_user, name, email, language, active
*/
public function setDestInfo($asDests) {
if(array_key_exists('email', $asDests)) $asDests = array($asDests);
$this->asDests = $asDests;
}
public function send() {
$sEOL = "\r\n";
foreach($this->asDests as $asDest) {
//Message
$asTemplate = $this->getTemplate($asDest['language']);
$oEmail = $asTemplate['email'];
//Unsubscribe Link
$sUnsubLink = $this->sServName.'?a=unsubscribe_email&id='.$asDest['id_user'];
$oEmail->setTag('unsubscribe_link', $sUnsubLink);
//Email Content
$sHtmlMessage = $oEmail->getMask();
$sPlainMessage = strip_tags(str_replace('<br />', "\n", $sHtmlMessage));
//Email
$iBoundary = uniqid("HTMLEMAIL");
$sHeaders =
'From: Spotty <spot@lutran.fr>'.$sEOL.
'Reply-To: Spotty <spot@lutran.fr>'.$sEOL.
'List-Unsubscribe: <mailto:unsubscribe@'.parse_url($this->sServName)['host'].'?subject=unsubscribe>, <'.$sUnsubLink.'>'.$sEOL.
'List-Unsubscribe-Post: List-Unsubscribe=One-Click'.$sEOL.
'MIME-Version: 1.0'.$sEOL.
'Content-Type: multipart/alternative; boundary="'.$iBoundary.'"'.$sEOL;
$sBody =
'--'.$iBoundary.$sEOL. //Plain Message
'Content-Type: text/plain; charset=UTF-8'.$sEOL.
'Content-Transfer-Encoding: base64'.$sEOL.
chunk_split(base64_encode($sPlainMessage)).$sEOL.
'--'.$iBoundary.$sEOL. //HTML Message
'Content-Type: text/html; charset=UTF-8'.$sEOL.
'Content-Transfer-Encoding: base64'.$sEOL.
chunk_split(base64_encode($sHtmlMessage)).$sEOL.
'--'.$iBoundary.'--';
//Send
if(!mail($asDest['email'], $asTemplate['subject'], $sBody, $sHeaders)) $this->addError('Could not send '.$this->sTemplate.' email to '.$asDest['email']);
}
}
}