개발노트
목록
[PHP] mail 함수를 이용하여 이메일 발송하기
메일이메일함수sendmail
PHP 2024.03.26 212 회 읽음
PHP 24.03.26 212

PHP의 mail 함수를 이용하여 이메일을 발송하는 방법에 대해서 안내 드리려고 합니다.

기본적인 명령어를 이용하기에 메일이 발송되더라도 스팸 처리가 될 수 있으니 받은 메일 확인이 안되면 스팸 메일 함에서 확인을 해보세요.

기본 화이트도메인 설정을 위한 작업을 해도 스팸으로 처리되는 경우에는 서버에서 도메인이나 발신인 등 설정이 정상적으로 되어 있어야 합니다.



1. mail 함수 

mail 함수는 php 대부분 버전에서 사용 가능하며 to, subject, message, additional_headers, additinal_params 파라미터를 통해 발송 할 수 있습니다.



2. 이메일 발송 폼 만들기

해당 소스는 테스트를 위해 제공하므로 필요에 따라 수정하여 사용하는 것을 권장합니다.

아래 소스를 복사하면 발송에 필요한 필수 정보를 입력 받는 폼으로 기존 소스에 연동하는 경우에는 발송 소스만 사용해도 됩니다.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Sendmail</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<style>
html, body {
    overflow-x: hidden; /* Prevent scroll on narrow devices */
}
@font-face {
    font-family: 'NEXON Lv1 Gothic OTF';
    src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_20-04@2.1/NEXON Lv1 Gothic OTF.woff') format('woff');
    font-weight: normal;
    font-style: normal;
}
body,span,p,div,ul,ol,dl,dt,dd,em,input,textarea,fieldset,small,tr,td,th,source {
    font-family : 'NEXON Lv1 Gothic OTF', sans-serif;
    font-weight : normal;
}
h1,h2,h3,h4,h5,h6 {
    font-family : 'NEXON Lv1 Gothic OTF', sans-serif;
    font-weight : normal;
}
</style>
<body>
<form name="frm_form" method="post" action="?action=send" target="_blank">
<div class="container mt-3">
    <h2>이메일 발송하기</h2>
    <p>PHP를 이용하여 메일 발송하는 폼 입니다.</p>
    <div class="form-floating mb-3 mt-3">
        <input type="text" class="form-control" name="title" id="title" required value="테스트 이메일 발송합니다.">
        <label for="title">이메일 제목</label>
    </div>
    <div class="form-floating mt-3 mb-3">
        <input type="email" class="form-control" name="email" id="email" required>
        <label for="email">받는사람 이메일</label>
    </div>
    <div class="form-floating mt-3 mb-3">
        <input type="text" class="form-control" name="fromname" id="fromname" required value="지티펀">
        <label for="fromname">보내는 사람이름</label>
    </div>
    <div class="form-floating mt-3 mb-3">
        <input type="email" class="form-control" name="fromemail" id="fromemail" required value="gtfun@gtfun.net">
        <label for="fromemail">보내는 사람이메일</label>
    </div>
    <button type="submit" class="btn btn-primary">발송하기</button>
</div>
</form>
</body>
</html>



3. PHP Smail 함수를 이용하여 발송하기

입력 받는 정보로 발송하는 소스이므로 사용자 필요에 따라 변수를 변경하여 사용하세요.

if( $_GET['action']=='send' ){
    $nameFrom   = $_POST['fromname'];
    $mailFrom   = $_POST['fromemail']; //보내는 사람 이메일
    $nameTo     = '담당자';
    $mailTo     = $_POST['email'];
    $cc         = "";
    $bcc        = "";
    $subject    = $_POST['title'];
    $content    = "
테스트용 이메일 입니다.
해당 이메일은 발신전용으로 회신되지 않습니다.
    ";
    $charset    = "UTF-8";
    $nameFrom   = "=?$charset?B?".base64_encode($nameFrom)."?=";
    $nameTo     = "=?$charset?B?".base64_encode($nameTo)."?=";
    $subject    = "=?$charset?B?".base64_encode($subject)."?=";
    $header     = "Content-Type: text/html; charset=utf-8rn";
    $header    .= "MIME-Version: 1.0rn";
    $header    .= "Return-Path: <". $mailFrom .">rn";
    $header    .= "From: ". $nameFrom ." <". $mailFrom .">rn";
    $header    .= "Reply-To: <". $mailFrom .">rn";
    if ($cc)  $header .= "Cc: ". $cc ."rn";
    if ($bcc) $header .= "Bcc: ". $bcc ."rn";
    //받는사람 email, subject, content, header, 보내는사람 email
    if( mail($mailTo, $subject, $content, $header, $mailFrom)==true ){
        echo'<div class="container mb-5 text-success">메일이 발송되었습니다.</div>';
    }
    else {
        echo'<div class="container mb-5 text-danger">메일 발송이 실패하였습니다.</div>';
    }
    exit;
}


메일 발송이 불안정할 때에는 gmail이나 naver 계정에서 imap 을 이용하여 발송하는 방법도 많이 사용합니다.

목록