Configuration is stored in data/email.json.
Example of Google Mail SMTP delivery:
{
"admin": ["Your Self", "you@domain.com"],
"smtp": {
"secure": "ssl",
"host": "smtp.gmail.com",
"port": 465,
"username": "my.account@gmail.com",
"password": "secret"
}
}
Example of sending mail using the convenience method "compose":
GBMail::compose('Hello', 'This is a mail', array('John Doe', 'john@doe.com'))->send();
static function did_spam_comment($comment) { if (!self::$data['notify_spam_comment']) { return; } $subject = '[' . gb::$site_title . '] Spam comment on "' . $comment->post->title . '"'; $body = self::comment_mkbody($comment); $to = self::recipient($comment); GBMail::compose($subject, $body, $to)->send(true); }
static function mkmailer() { # setup conf $siteurl = new GBURL(gb::$site_url); $admin = GBUser::findAdmin(); $default_name = $admin ? $admin->name : $siteurl->host; $default_address = $admin ? $admin->email : 'root@' . $siteurl->host; self::$conf = gb::data('email', array('admin' => array($default_name, $default_address), 'from' => array(gb::$site_title, 'noreply@' . $siteurl->host), 'smtp.example-gmail' => array('secure' => 'ssl', 'host' => 'smtp.gmail.com:465', 'username' => $default_address, 'password' => 'secret'))); # since PHPMailer is one ugly piece of software $orig_error_reporting = error_reporting(E_ALL ^ E_NOTICE); # setup phpmailer $e = new PHPMailer(); $e->From = ''; $e->FromName = ''; $e->PluginDir = gb::$dir . '/lib/PHPMailer/'; # SMTP if (($c = self::$conf['smtp']) !== null) { $e->IsSMTP(); # enable SMTP # authenitcation? $e->SMTPAuth = isset($c['password']) || isset($c['username']); # secure? if (isset($c['secure']) && $c['secure']) { static $allowed = array('ssl' => 1, 'tls' => 1, '' => 1); $c['secure'] = is_string($c['secure']) ? strtolower($c['secure']) : ($c['secure'] ? 'ssl' : ''); if (!isset($allowed[$c['secure']])) { gb::log(LOG_WARNING, 'malformed configuration: bad value for "secure": %s -- only "ssl" or "tls" is allowed', $c['secure']); } else { $e->SMTPSecure = $c['secure']; } } # support for multiple hosts if (isset($c['host'])) { $e->Host = $c['host']; } elseif (isset($c['hosts'])) { $e->Host = $c['hosts']; } if (is_array($e->Host)) { $hosts = $e->Host; $e->Host = array(); foreach ($hosts as $host) { if (is_array($host)) { if (!isset($host['name'])) { gb::log(LOG_WARNING, 'malformed configuration: missing "name" for host %s', var_export($host, 1)); } else { $v[] = $host['name'] . (isset($host['port']) ? ':' . $host['port'] : ''); } } else { $v[] = $host; } } $e->Host = implode(';', $e->Host); } # default port if (isset($c['port']) && ($port = intval($c['port'])) > 0) { $e->Port = $port; } # username if (isset($c['username'])) { $e->Username = $c['username']; } # password if (isset($c['password'])) { $e->Password = $c['password']; } # connection timeout if (isset($c['timeout']) && ($i = intval($c['timeout'])) > 0) { $e->Timeout = $i; } } # gitblog <heart> UTF-8 $e->CharSet = 'utf-8'; # Default from if ($from = self::$conf['from']) { list($e->From, $e->FromName) = self::normalizeRecipient($from); } # Default sender if (($sender = self::$conf['sender']) || ($sender = self::$conf['admin'])) { list($e->Sender, $discard) = self::normalizeRecipient($sender); } # default priority if ($priority = self::$conf['priority']) { $e->Priority = intval($priority); } # reset error reporting error_reporting($orig_error_reporting); return $e; }