본문으로 바로가기

PHPMailer, XPertMailer(XPM4) 메일 보내기

category [ Web 관련 ]/php 2019. 12. 26. 16:11

 

PHPMailer.zip
0.20MB

 

 

XPM4-v.0.5.zip
0.13MB

 

 


// 1:XPertMailer, 2:PHPMailer
$mail_kind = 2;

//보낼 메일 주소
$to_mail = isset($_REQUEST['mail']) ? $_REQUEST['mail'] : '';
$subject = "테스트 (".date("Y-m-d H:i:s"). ")";
$content = "메일 내용 입니다.";
$from_mail = "보내는 메일 주소";

if($mail_kind == 1){

    /*
    [XPertMailer] http://xpertmailer.sourceforge.net/

    Array ( [101] => ) 1
    error code 101 is returning by SMTP Class when you can NOT connect to smtp mail server
    */

    // manage errors
    error_reporting(E_ALL); // php errors
    define('DISPLAY_XPM4_ERRORS', true);


    // path to 'MAIL.php' file from XPM4 package
    require_once './경로/XPM4/MAIL.php';

    // initialize MAIL class
    $m = new MAIL;

    // set from address
    $m->From($from_mail);

    // add to address
    $m->AddTo($to_mail);

    // set subject
    $m->Subject("=?UTF-8?B?".base64_encode("$subject")."?="."\r\n");

    // set HTML message
    $m->Html = array(
        'content'	=> $content, // required
        'charset'	=> 'utf-8', // optional
        'encoding' => 'base64' // optional
    );

    // connect to MTA server with no user and pass
    //@$c = $m->Connect('smtp.gmail.com', 465, '계정', '패스워드', 'tls', 10, 'localhost', null, 'plain') or die(print_r($m->Result));
    @$c = $m->Connect('자체 메일서버 또는 구글등 SMTP서버 주소', 25) or die(print_r($m->Result));

    // send mail relay using the '$c' resource connection
    @$m->Send($c) ? 'Mail sent !' : 'Error !';

    // disconnect from server
    @$m->Disconnect();

    // optional for debugging ----------------
    echo '<pre>';

    //print History
    print_r($m->History);

    //calculate time
    list($tm1, $ar1) = @each($m->History[0]);
    list($tm2, $ar2) = @each($m->History[count(@$m->History)-1]);
    echo 'The process took: '.(floatval($tm2)-floatval($tm1)).' seconds.';
    echo '<pre>';

}else if ($mail_kind == 2){

    require_once("./경로/PHPMailer/class.phpmailer.php");

    $mail = new PHPMailer(true);
    $mail->IsSMTP();
    $mail->SMTPDebug  = 1; //디버깅

    try {
        $mail->CharSet = "utf-8"; // 언어셋 설정
        $mail->Encoding = "base64"; // 인코딩 방법 정의
        $mail->Host = "자체 메일서버 또는 구글등 SMTP서버 주소"; // SMTP 서버 주소 입력
        //$mail->SMTPAuth = true; // SMTP 인증 사용
        //$mail->Username = "******"; // SMTP 계정(메일 주소)
        //$mail->Password = "******"; // SMTP 패스워드
        //$mail->SMTPSecure = "ssl"; // SSL 암호화 사용

        $mail->Port = 25; // TCP port
        //$mail->Port = 465; // TCP port
        $mail->setFrom($from_mail, "그룹웨어"); // 보내는 사람 메일 주소, 보내는 사람 이름
        $mail->addAddress($to_mail, "이름"); // 받는 사람 메일 주소, 받는 사람 이름
        $mail->isHTML(true); // 이메일 포맷을 HTML로 하겠다고 선언
        $mail->Subject = $subject; // 제목
        $mail->Body = $content; // 내용
        $mail->Send();
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
    }

}