그누보드(영카트)용 메일건 mailgun 발송 소스 입니다. > 영카트5 팁자료실

영카트5 팁자료실

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

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

본문

옆동네서 그누보드에서 메일건(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 = "관리자 <*** 개인정보보호를 위한 이메일주소 노출방지 ***>";

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 에서 위의 코드로 대체하시면 됩니다.
추천
5

댓글 6개

아네. 안녕하세요.
^^ 애 셋 키우느라 바쁘네요... 일도 하고, 애들하고 놀아야 하고... 뭐 다들 그렇겠지만..
회사 이직 준비하고 있어서, 간간히 들어올것 같네요.
전체 3

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT