Skip to content

Instantly share code, notes, and snippets.

Created September 28, 2017 08:09
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 anonymous/a741c28e79a09fc8dd88b267527c75a7 to your computer and use it in GitHub Desktop.
Save anonymous/a741c28e79a09fc8dd88b267527c75a7 to your computer and use it in GitHub Desktop.
메일건(mailgun) 발송용 class (그누보드4 기준)
<?php
class MailgunMailer {
const CRLF = "\r\n";
const DEFAULT_CHARSET = "utf-8";
private $Charset = "utf-8";
private $api_key = "메일건 api key"; //mailgun domain api-key
private $api_call_url = "https://api.mailgun.net/v3/your.domain.com/messages";
private $From = "관리자 <admin@your.domain.com>";
private $To = "";
private $Cc = "";
private $Bcc = "";
private $Subject = "";
private $Content = "";
private $Type = 0;
private $File = null;
private $attachement = null;
public function __construct() {
global $g4;
if($g4['charset'] == "euc-kr") {
$this->Charset = "euc-kr";
}
}
public function setCharset($charset) {
$this->Charset = strtolower($charset);
}
public function setFrom($from) {
if($this->Charset == "euc-kr") {
$this->From = iconv("euc-kr", "utf-8", $from);
} else {
$this->From = $from;
}
}
public function setTo($to) {
$this->To = $to;
}
public function setSubject($subject) {
$this->Subject = $subject;
}
public function setContent($content) {
$this->Content = $content;
}
public function setType($type) {
$this->Type = $type;
}
public function attachFile($file) {
$this->attachement = $file;
}
public function send() {
if($this->Charset == "euc-kr") {
$this->To = iconv("euc-kr", "utf-8", $this->To);
$this->Subject = iconv("euc-kr", "utf-8", $this->Subject);
$this->Content = iconv("euc-kr", "utf-8", $this->Content);
}
$post = [
'from' => $this->From,
'to' => $this->To,
'subject' => $this->Subject,
];
if($this->Type) {
$post['html'] = $this->Content;
} else {
$post['text'] = $this->Content;
}
$ch = curl_init($this->api_call_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->api_key);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
return $response;
}
/**
*
* @param type 메일 본문 내용이 html인지 여부 0:text, 1:html
*/
function mailer($to, $subject, $content, $type=0, $file="", $cc="", $bcc="") {
global $g4;
$this->setCharset($g4['charset']);
$this->setTo($to);
$this->setSubject($subject);
$this->setContent($content);
$this->setType($type);
$this->send();
}
}
?>
@merong
Copy link

merong commented Sep 28, 2017

사용예제
$xmailer = new MailgunMailer();
$xmailer->setCharset($g4['charset']);
if($fname) {
$xmailer->setFrom("$fname <{$fmail}>");
} else {
$xmailer->setFrom($fmail);
}

$xmailer->setTo($to);
$xmailer->setSubject($subject);
$xmailer->setContent($content);
$xmailer->setType($type);
$response = $xmailer->send();

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