Skip to content

Instantly share code, notes, and snippets.

@kpodemski
Created February 15, 2018 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpodemski/498e1714e3188049932d8a013391d822 to your computer and use it in GitHub Desktop.
Save kpodemski/498e1714e3188049932d8a013391d822 to your computer and use it in GitHub Desktop.
Mail class wrapper for PrestaShop to prevent super long function calls
<?php
class MailWrapper
{
/**
* Shop context
* @var Context
*/
protected $context;
/**
* Template name
* @var string
*/
protected $template;
/**
* Subject of the e-mail
* @var string
*/
protected $subject;
/**
* Variables available in template
* @var array
*/
protected $template_vars;
/**
* Recipient e-mail address
* @var string
*/
protected $to;
/**
* Recipient name
* @var string
*/
protected $to_name = null;
/**
* Sender e-mail
* @var string
*/
protected $from = null;
/**
* Sender name
* @var string
*/
protected $from_name = null;
/**
* File attachment path
* @var string
*/
protected $file_attachment = null;
/**
* Use SMTP?
* @var boolean
*/
protected $mode_smtp = null;
/**
* Template path
* @var string
*/
protected $template_path = _PS_MAIL_DIR_;
/**
* Die on error?
* @var boolean
*/
protected $die = false;
/**
* Recipient(s) (email address)
* @var mixed
*/
protected $bcc = null;
/**
* Reply to param
* @var string
*/
protected $reply_to = null;
public function __construct(Context $context)
{
if (!$context) {
$context = Context::getContext();
}
$this->context = $context;
}
/**
* @return Context
*/
public function getContext()
{
return $this->context;
}
/**
* @param Context $context
*
* @return self
*/
public function setContext(Context $context)
{
$this->context = $context;
return $this;
}
/**
* @return string
*/
public function getTemplate()
{
return $this->template;
}
/**
* @param string $template
*
* @return self
*/
public function setTemplate($template)
{
$this->template = $template;
return $this;
}
/**
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* @param string $subject
*
* @return self
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* @return array
*/
public function getTemplateVars()
{
return $this->template_vars;
}
/**
* @param array $template_vars
*
* @return self
*/
public function setTemplateVars(array $template_vars)
{
$this->template_vars = $template_vars;
return $this;
}
/**
* @return string
*/
public function getTo()
{
return $this->to;
}
/**
* @param string $to
*
* @return self
*/
public function setTo($to)
{
$this->to = $to;
return $this;
}
/**
* @return string
*/
public function getToName()
{
return $this->to_name;
}
/**
* @param string $to_name
*
* @return self
*/
public function setToName($to_name)
{
$this->to_name = $to_name;
return $this;
}
/**
* @return string
*/
public function getFrom()
{
return $this->from;
}
/**
* @param string $from
*
* @return self
*/
public function setFrom($from)
{
$this->from = $from;
return $this;
}
/**
* @return string
*/
public function getFromName()
{
return $this->from_name;
}
/**
* @param string $from_name
*
* @return self
*/
public function setFromName($from_name)
{
$this->from_name = $from_name;
return $this;
}
/**
* @return string
*/
public function getFileAttachment()
{
return $this->file_attachment;
}
/**
* @param string $file_attachment
*
* @return self
*/
public function setFileAttachment($file_attachment)
{
$this->file_attachment = $file_attachment;
return $this;
}
/**
* @return boolean
*/
public function isModeSmtp()
{
return $this->mode_smtp;
}
/**
* @param boolean $mode_smtp
*
* @return self
*/
public function setModeSmtp($mode_smtp)
{
$this->mode_smtp = $mode_smtp;
return $this;
}
/**
* @return string
*/
public function getTemplatePath()
{
return $this->template_path;
}
/**
* @param string $template_path
*
* @return self
*/
public function setTemplatePath($template_path)
{
$this->template_path = $template_path;
return $this;
}
/**
* @return boolean
*/
public function isDie()
{
return $this->die;
}
/**
* @param boolean $die
*
* @return self
*/
public function setDie($die)
{
$this->die = $die;
return $this;
}
/**
* @return mixed
*/
public function getBcc()
{
return $this->bcc;
}
/**
* @param mixed $bcc
*
* @return self
*/
public function setBcc($bcc)
{
$this->bcc = $bcc;
return $this;
}
/**
* @return string
*/
public function getReplyTo()
{
return $this->reply_to;
}
/**
* @param string $reply_to
*
* @return self
*/
public function setReplyTo($reply_to)
{
$this->reply_to = $reply_to;
return $this;
}
public function send()
{
return Mail::Send(
(int) $this->context->language->id,
$this->getTemplate(),
$this->getSubject(),
$this->getTemplateVars(),
$this->getTo(),
$this->getToName(),
$this->getFrom(),
$this->getFromName(),
$this->getFileAttachment(),
$this->isModeSmtp(),
$this->getTemplatePath(),
$this->isDie(),
(int) $this->context->shop->id,
$this->getBcc(),
$this->getReplyTo()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment