mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-02 08:42:15 +09:00
Merge pull request #460 from kijin/pr/advanced-mailer
고급 메일 발송 모듈의 기능을 라이믹스에 포함
This commit is contained in:
commit
9a7445b677
141 changed files with 5770 additions and 683 deletions
|
|
@ -1,36 +1,12 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Mail class
|
||||
* Mail class for XE Compatibility
|
||||
*
|
||||
* @author Kijin Sung <kijin@kijinsung.com>
|
||||
*/
|
||||
class Mail
|
||||
class Mail extends Rhymix\Framework\Mail
|
||||
{
|
||||
/**
|
||||
* Properties for compatibility with XE Mail class
|
||||
*/
|
||||
public $content = '';
|
||||
public $content_type = 'html';
|
||||
public $attachments = array();
|
||||
public $cidAttachments = array();
|
||||
|
||||
/**
|
||||
* Properties used by Advanced Mailer
|
||||
*/
|
||||
public $error = null;
|
||||
public $caller = null;
|
||||
public $message = null;
|
||||
public static $transport = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->message = \Swift_Message::newInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parameters for using Gmail
|
||||
*
|
||||
|
|
@ -57,14 +33,13 @@ class Mail
|
|||
*/
|
||||
public static function useSMTP($auth = null, $host = null, $user = null, $pass = null, $secure = null, $port = 25)
|
||||
{
|
||||
self::$transport = \Swift_SmtpTransport::newInstance($host, $port, $secure);
|
||||
self::$transport->setUsername($user);
|
||||
self::$transport->setPassword($pass);
|
||||
$local_domain = self::$transport->getLocalDomain();
|
||||
if (preg_match('/^\*\.(.+)$/', $local_domain, $matches))
|
||||
{
|
||||
self::$transport->setLocalDomain($matches[1]);
|
||||
}
|
||||
self::setDefaultDriver(Rhymix\Framework\Drivers\Mail\SMTP::getInstance(array(
|
||||
'smtp_host' => $host,
|
||||
'smtp_port' => $port,
|
||||
'smtp_security' => $secure,
|
||||
'smtp_user' => $user,
|
||||
'smtp_pass' => $pass,
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -76,7 +51,7 @@ class Mail
|
|||
}
|
||||
|
||||
/**
|
||||
* Set Sender (From:)
|
||||
* Set the sender (From:).
|
||||
*
|
||||
* @param string $name Sender name
|
||||
* @param string $email Sender email address
|
||||
|
|
@ -84,36 +59,17 @@ class Mail
|
|||
*/
|
||||
public function setSender($name, $email)
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->message->setFrom(array($email => $name));
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->errors[] = array($e->getMessage());
|
||||
}
|
||||
$this->setFrom($email, $name ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Sender (From:)
|
||||
* Get the sender.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSender()
|
||||
{
|
||||
$from = $this->message->getFrom();
|
||||
foreach($from as $email => $name)
|
||||
{
|
||||
if($name === '')
|
||||
{
|
||||
return $email;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $name . ' <' . $email . '>';
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
return $this->getFrom() ?: false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -125,14 +81,8 @@ class Mail
|
|||
*/
|
||||
public function setReceiptor($name, $email)
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->message->setTo(array($email => $name));
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->errors[] = array($e->getMessage());
|
||||
}
|
||||
$this->message->setTo(array());
|
||||
return $this->addTo($email, $name ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -142,40 +92,8 @@ class Mail
|
|||
*/
|
||||
public function getReceiptor()
|
||||
{
|
||||
$to = $this->message->getTo();
|
||||
foreach($to as $email => $name)
|
||||
{
|
||||
if($name === '')
|
||||
{
|
||||
return $email;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $name . ' <' . $email . '>';
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Subject
|
||||
*
|
||||
* @param string $subject The subject
|
||||
* @return void
|
||||
*/
|
||||
public function setTitle($subject)
|
||||
{
|
||||
$this->message->setSubject(strval($subject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Subject
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->message->getSubject();
|
||||
$list = $this->getRecipients();
|
||||
return $list ? array_first($list) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -186,96 +104,8 @@ class Mail
|
|||
*/
|
||||
public function setBCC($bcc)
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->message->setBcc(array($bcc));
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->errors[] = array($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ReplyTo
|
||||
*
|
||||
* @param string $replyTo
|
||||
* @return void
|
||||
*/
|
||||
public function setReplyTo($replyTo)
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->message->setReplyTo(array($replyTo));
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->errors[] = array($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Return Path
|
||||
*
|
||||
* @param string $returnPath
|
||||
* @return void
|
||||
*/
|
||||
public function setReturnPath($returnPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->message->setReturnPath($returnPath);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->errors[] = array($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Message ID
|
||||
*
|
||||
* @param string $messageId
|
||||
* @return void
|
||||
*/
|
||||
public function setMessageID($messageId)
|
||||
{
|
||||
$this->message->getHeaders()->get('Message-ID')->setId($messageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set references
|
||||
*
|
||||
* @param string $references
|
||||
* @return void
|
||||
*/
|
||||
public function setReferences($references)
|
||||
{
|
||||
$headers = $this->message->getHeaders();
|
||||
$headers->addTextHeader('References', $references);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set message content
|
||||
*
|
||||
* @param string $content Content
|
||||
* @return void
|
||||
*/
|
||||
public function setContent($content)
|
||||
{
|
||||
$content = preg_replace_callback('/<img([^>]+)>/i', array($this, 'replaceResourceRealPath'), $content);
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type of message content (html or plain text)
|
||||
*
|
||||
* @param string $mode The type
|
||||
* @return void
|
||||
*/
|
||||
public function setContentType($type = 'html')
|
||||
{
|
||||
$this->content_type = $type === 'html' ? 'html' : '';
|
||||
$this->message->setBcc(array());
|
||||
return $this->addBcc($bcc);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -285,7 +115,7 @@ class Mail
|
|||
*/
|
||||
public function getPlainContent()
|
||||
{
|
||||
return chunk_split(base64_encode(str_replace(array("<", ">", "&"), array("<", ">", "&"), $this->content)));
|
||||
return chunk_split(base64_encode(htmlspecialchars($this->message->getBody())));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -295,19 +125,19 @@ class Mail
|
|||
*/
|
||||
public function getHTMLContent()
|
||||
{
|
||||
return chunk_split(base64_encode($this->content_type != 'html' ? nl2br($this->content) : $this->content));
|
||||
return chunk_split(base64_encode($this->content_type != 'text/html' ? nl2br($this->message->getBody()) : $this->message->getBody()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add file attachment
|
||||
*
|
||||
* @param string $filename File name to attach
|
||||
* @param string $original_filename Real path of file to attach
|
||||
* @param string $filename File name to attach
|
||||
* @return void
|
||||
*/
|
||||
public function addAttachment($filename, $original_filename)
|
||||
public function addAttachment($original_filename, $filename)
|
||||
{
|
||||
$this->attachments[$original_filename] = $filename;
|
||||
return $this->attach($original_filename, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -317,21 +147,9 @@ class Mail
|
|||
* @param string $cid Content-CID
|
||||
* @return void
|
||||
*/
|
||||
public function addCidAttachment($original_filename, $cid)
|
||||
public function addCidAttachment($original_filename, $cid = null)
|
||||
{
|
||||
$this->cidAttachments[$cid] = $original_filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace resourse path of the files
|
||||
*
|
||||
* @see Mail::setContent()
|
||||
* @param array $matches Match info.
|
||||
* @return string
|
||||
*/
|
||||
public function replaceResourceRealPath($matches)
|
||||
{
|
||||
return preg_replace('/src=(["\']?)files/i', 'src=$1' . \Context::getRequestUri() . 'files', $matches[0]);
|
||||
return $this->embed($original_filename, $cid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -354,66 +172,13 @@ class Mail
|
|||
// no-op
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the message before sending
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function procAssembleMessage()
|
||||
{
|
||||
// Add all attachments
|
||||
foreach($this->attachments as $original_filename => $filename)
|
||||
{
|
||||
$attachment = \Swift_Attachment::fromPath($original_filename);
|
||||
$attachment->setFilename($filename);
|
||||
$this->message->attach($attachment);
|
||||
}
|
||||
|
||||
// Add all CID attachments
|
||||
foreach($this->cidAttachments as $cid => $original_filename)
|
||||
{
|
||||
$embedded = \Swift_EmbeddedFile::fromPath($original_filename);
|
||||
$newcid = $this->message->embed($embedded);
|
||||
$this->content = str_replace(array("cid:$cid", $cid), $newcid, $this->content);
|
||||
}
|
||||
|
||||
// Set content type
|
||||
$content_type = $this->content_type === 'html' ? 'text/html' : 'text/plain';
|
||||
$this->message->setBody($this->content, $content_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send email
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->procAssembleMessage();
|
||||
if(!self::$transport)
|
||||
{
|
||||
self::$transport = \Swift_MailTransport::newInstance();
|
||||
}
|
||||
$mailer = \Swift_Mailer::newInstance(self::$transport);
|
||||
$result = $mailer->send($this->message, $this->errors);
|
||||
return (bool)$result;
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if DNS of param is real or fake
|
||||
*
|
||||
* @param string $email_address Email address to check
|
||||
* @return bool
|
||||
*/
|
||||
public function checkMailMX($email_address)
|
||||
public static function checkMailMX($email_address)
|
||||
{
|
||||
if(!self::isVaildMailAddress($email_address))
|
||||
{
|
||||
|
|
@ -434,13 +199,23 @@ class Mail
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this class supports Advanced Mailer features.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isAdvancedMailer()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if param is a valid email or not
|
||||
*
|
||||
* @param string $email_address Email address to check
|
||||
* @return string
|
||||
*/
|
||||
public function isVaildMailAddress($email_address)
|
||||
public static function isVaildMailAddress($email_address)
|
||||
{
|
||||
if(preg_match("/([a-z0-9\_\-\.]+)@([a-z0-9\_\-\.]+)/i", $email_address))
|
||||
{
|
||||
|
|
@ -458,7 +233,7 @@ class Mail
|
|||
* @param string $filename filename
|
||||
* @return string MIME type of ext
|
||||
*/
|
||||
function returnMIMEType($filename)
|
||||
public static function returnMIMEType($filename)
|
||||
{
|
||||
return Rhymix\Framework\MIME::getTypeByFilename($filename);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue