Favorites

PHP 메일 사진 첨부하는 방법

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

안녕하세요. 저는 회원메일발송 기능을 이용할 때, 메일이 전송은 되는데 텍스트만 전송이 되고 첨부된 사진은 전송이 되지 않을 때 이렇게 코드를 바꿔서 해결했습니다.

SMTP는 설정이 되어있다고 가정하고요.
lib/mailer.lib.php 파일 소스코드입니다.
 

<?php
if (!defined('_GNUBOARD_')) exit;
 
include_once(G5_PHPMAILER_PATH.'/PHPMailerAutoload.php');
 
// 메일 보내기 (파일 여러개 첨부 가능)
// type : text=0, html=1, text+html=2
function mailer($fname, $fmail, $to, $subject, $content, $type=2, $file="", $cc="", $bcc="")
{
    global $config;
    global $g5;
 
    // 메일발송 사용을 하지 않는다면
    if (!$config['cf_email_use']) return;
 
    if ($type != 1)
        $content = nl2br($content);
 
    $result = run_replace('mailer', $fname, $fmail, $to, $subject, $content, $type, $file, $cc, $bcc);
   
    if (is_array($result) && isset($result['return'])) {
        return $result['return'];
    }
 
    $mail_send_result = false;
 
    try {
        $mail = new PHPMailer(); // defaults to using php "mail()"
        if (defined('G5_SMTP') && G5_SMTP) {
            $mail->IsSMTP(); // telling the class to use SMTP
            $mail->Host = G5_SMTP; // SMTP server
            if (defined('G5_SMTP_PORT') && G5_SMTP_PORT)
                $mail->Port = G5_SMTP_PORT;
            /* 추가 */
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = G5_SMTP_SECURE;
            $mail->Username = G5_SMTP_USER;
            $mail->Password = G5_SMTP_PW;
            /* 추가 끝 */
        }
        $mail->CharSet = 'UTF-8';
        $mail->From = $fmail;
        $mail->FromName = $fname;
        $mail->Subject = $subject;
        $mail->AltBody = ""; // optional, comment out and test
        $mail->addAddress($to);
        if ($cc)
            $mail->addCC($cc);
        if ($bcc)
            $mail->addBCC($bcc);
 
        // 이미지 CID 자동 처리 (샘플 메일에 사용된 이미지들만)
        preg_match_all('/<img[^>]+src="([^">]+)"/i', $content, $matches); // HTML에서 img 태그의 src 속성 추출
        $image_sources = array_unique($matches[1]); // 중복된 경로를 제거
 
        foreach ($image_sources as $index => $src) {
            // src 경로에서 이미지 파일명만 추출
            $image_name = basename($src);
            $image_path = G5_PATH.'data/editor/파일경로/'.$image_name;
 
            if (file_exists($image_path)) {
                $cid = 'image_cid_' . $index;
                $mail->addEmbeddedImage($image_path, $cid, $image_name);
 
                // 이미지 경로를 CID로 대체
                $content = str_replace($src, 'cid:'.$cid, $content);
 
                // 동시에 이미지를 첨부파일로 추가
                $mail->addAttachment($image_path, $image_name);
            }
        }
 
        $mail->msgHTML($content);
 
        $mail = run_replace('mail_options', $mail, $fname, $fmail, $to, $subject, $content, $type, $file, $cc, $bcc);
 
        $mail_send_result = $mail->send();
 
    } catch (Exception $e) {
        // 오류 처리
    }
 
    run_event('mail_send_result', $mail_send_result, $mail, $to, $cc, $bcc);
 
    return $mail_send_result;
}
 
// 파일을 첨부함
function attach_file($filename, $tmp_name)
{
    // 서버에 업로드 되는 파일은 확장자를 주지 않는다. (보안 취약점)
    $dest_file = G5_DATA_PATH.'/tmp/'.str_replace('/', '_', $tmp_name);
    move_uploaded_file($tmp_name, $dest_file);
    $tmpfile = array("name" => $filename, "path" => $dest_file);
    return $tmpfile;
}
?>
Copy



도움이 되길 바랍니다.
 

관련자료

댓글 0
등록된 댓글이 없습니다.
전체 141 / 2 페이지
번호
제목
연결

Favorites


최근글


새댓글


  • 댓글이 없습니다.
알림 0