Skip to content

Instantly share code, notes, and snippets.

@cgsmith
Created January 31, 2017 19:55
Show Gist options
  • Save cgsmith/69ff497bf8b9aa7318c676264c1d938e to your computer and use it in GitHub Desktop.
Save cgsmith/69ff497bf8b9aa7318c676264c1d938e to your computer and use it in GitHub Desktop.
OpenCart mail.php fix and integration with Postmark App
<?php
require_once 'vendor/autoload.php';
use Postmark\PostmarkClient;
use Postmark\Models\PostmarkException;
class Mail {
protected $to;
protected $from;
protected $sender;
protected $reply_to;
protected $subject;
protected $text;
protected $html;
protected $attachments = array();
public $protocol = 'mail';
public $smtp_hostname;
public $smtp_username;
public $smtp_password;
public $smtp_port = 25;
public $smtp_timeout = 5;
public $verp = false;
public $parameter = '';
public function __construct($config = array()) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
public function setTo($to) {
$this->to = $to;
}
public function setFrom($from) {
$this->from = $from;
}
public function setSender($sender) {
$this->sender = $sender;
}
public function setReplyTo($reply_to) {
$this->reply_to = $reply_to;
}
public function setSubject($subject) {
$this->subject = $subject;
}
public function setText($text) {
$this->text = $text;
}
public function setHtml($html) {
$this->html = $html;
}
public function addAttachment($filename) {
$this->attachments[] = $filename;
}
public function send() {
if (!$this->to) {
throw new \Exception('Error: E-Mail to required!');
}
if (!$this->from) {
throw new \Exception('Error: E-Mail from required!');
}
if (!$this->sender) {
throw new \Exception('Error: E-Mail sender required!');
}
if (!$this->subject) {
throw new \Exception('Error: E-Mail subject required!');
}
if ((!$this->text) && (!$this->html)) {
throw new \Exception('Error: E-Mail message required!');
}
if (is_array($this->to)) {
$to = $this->to;
} else {
$to[] = $this->to;
}
/**
* Integration with PostMark below
*
* I chose to use the API due to several restrictions on different web hosts. Just put the API key in the SMTP Username setting
* and everything will work. Note: you will need to configure Postmark App with your authorized senders or domain.
*/
try{
$client = new PostmarkClient($this->smtp_username);
$html = is_null($this->html);
$key = ($html) ? 'TextBody' : 'HtmlBody';
$message = [
'To' => $this->to,
'From' => $this->from,
'TrackOpens' => true,
'Subject' => $this->subject,
$key => ($key == 'TextBody') ? $this->text : $this->html,
];
$sendResult = $client->sendEmailBatch([$message]);
}catch(PostmarkException $ex){
echo $ex->httpStatusCode;
echo $ex->message;
echo $ex->postmarkApiErrorCode;
}catch(Exception $generalEx){
}
// End of the Integration
}
}
@cgsmith
Copy link
Author

cgsmith commented Jan 31, 2017

Requirement: https://github.com/wildbit/postmark-php

Install through composer right inside the system folder. Or install locally and FTP all the files up - I know it is dirty but it gets the job done with OpenCart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment