테스트 사이트 - 개발 중인 베타 버전입니다

그누보드(영카트)용 메일건 mailgun 발송 소스 입니다.

· 8년 전 · 4309 · 6
옆동네서 그누보드에서 메일건(mialgun) 발송 오류에 대해 답변한 내용입니다.
요즘 많이 쓰는 솔루션이니 참고가 될까 하고 소스를 올립니다.
한달 1만건 까지 발송이 무료입니다.(신용카드는 연결해두어야 합니다.)


<?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();
}


}
?>

사용예제
$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();

그누보드의 경우 mailer.lib.php 에서 위의 코드로 대체하시면 됩니다.

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 6개

8년 전
좋은 정보 간사합니다.^^
8년 전
오 mailgun 이군요! 그나저나 오랜만이예요! 명랑폐인님! 건강하시죠!?
8년 전
아네. 안녕하세요.
^^ 애 셋 키우느라 바쁘네요... 일도 하고, 애들하고 놀아야 하고... 뭐 다들 그렇겠지만..
회사 이직 준비하고 있어서, 간간히 들어올것 같네요.
감사합니다.^^
감사합니다
7년 전
소스를 좀더 정리하여, 그누보드 플러그인 형태로 작업하였습니다.
https://sir.kr/g5_plugin/4270

게시글 목록

번호 제목
2400
2394
2363
2362
2351
2350
2348
2347
2336
2326
2324
2323
2318
2306
2305
2299
2295
2293
2283
2279
2266
2254
2248
2232
2231
2204
2184
2183
2181
2179