Skip to content

Instantly share code, notes, and snippets.

@metacoin
Last active January 4, 2016 05:19
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 metacoin/8574376 to your computer and use it in GitHub Desktop.
Save metacoin/8574376 to your computer and use it in GitHub Desktop.
MPOS authentication
/**
* smtp setting
*
* This will use smtp socket instead of the default php mail() function.
* Useful if you have another smtp application like dovecot on the server.
*
* Default:
* auth = 1
* server = 'servername'
* port = 25
* user = ''
* pass = ''
* localhost = 'servername'
* timeout = 30
* newline = "\r\n"
* idfrom = ''
**/
$config['smtp']['auth'] = 1;
$config['smtp']['server'] = 'pool.florincoin.info';
$config['smtp']['port'] = 25;
$config['smtp']['user'] = 'support@pool.florincoin.info';
$config['smtp']['pass'] = '';
$config['smtp']['localhost'] = 'pool.florincoin.info';
$config['smtp']['timeout'] = 30;
$config['smtp']['newline'] = "\r\n";
$config['smtp']['idfrom'] = 'pool.florincoin.info';
public function sendMail($template, $aData) {
date_default_timezone_set('UTC');
// Make sure we don't load a cached filed
$this->smarty->clearCache(BASEPATH . 'templates/mail/' . $template . '.tpl');
$this->smarty->clearCache(BASEPATH . 'templates/mail/subject.tpl');
$this->smarty->assign('WEBSITENAME', $this->setting->getValue('website_name'));
$this->smarty->assign('SUBJECT', $aData['subject']);
$this->smarty->assign('DATA', $aData);
if ($this->config['smtp']['auth'] === 1) {
if ($this->authSendEmail($this->setting->getValue('website_email'), $this->setting->getValue('website_name'), $aData['email'], $aData['email'],
$this->smarty->fetch(BASEPATH . 'templates/mail/subject.tpl'), $this->smarty->fetch(BASEPATH . 'templates/mail/' . $template . '.tpl'),
$this->config['smtp']['server'], $this->config['smtp']['port'], $this->config['smtp']['timeout'], $this->config['smtp']['user'],
$this->config['smtp']['pass'], $this->config['smtp']['localhost'], $this->config['smtp']['newline'], $this->config['smtp']['idfrom']))
return true;
}
else {
$headers = 'From: ' . $this->setting->getValue('website_name') . '<' . $this->setting->getValue('website_email') . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (strlen(@$aData['senderName']) > 0 && @strlen($aData['senderEmail']) > 0 )
$headers .= 'Reply-To: ' . $aData['senderName'] . ' <' . $aData['senderEmail'] . ">\n";
if (mail($aData['email'], $this->smarty->fetch(BASEPATH . 'templates/mail/subject.tpl'), $this->smarty->fetch(BASEPATH . 'templates/mail/' . $template . '.tpl'), $headers))
return true;
}
$this->setErrorMessage($this->sqlError('E0031'));
return false;
}
public function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message, $smtpServer, $port, $timeout, $username, $password, $localhost, $newLine, $idfrom) {
$success = false;
// connect to smtp on port specified
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect)) {return false;}
// auth login
fputs($smtpConnect, "AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
fputs($smtpConnect, "HELLO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
// send rfc-2822 headers and email
//$headers = "MIME-Version: 1.0" . $newLine;
//$headers .= "Content-Type: text/html;charset=iso-8859-1" . $newLine;
//$headers .= "Date: " . date(DateTime::RFC2822) . $newLine;
//$headers .= "Message-ID: <" . sha1(microtime(true)) . "@$idfrom>" . $newLine;
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
if (strstr($smtpResponse, "250", false)) $success = true;
fputs($smtpConnect, "QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
return $success;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment