메일 사진 첨부하는 방법
안녕하세요. 저는 회원메일발송 기능을 이용할 때, 메일이 전송은 되는데 텍스트만 전송이 되고 첨부된 사진은 전송이 되지 않을 때 이렇게 코드를 바꿔서 해결했습니다.
SMTP는 설정이 되어있다고 가정하고요.
lib/mailer.lib.php 파일 소스코드입니다.
[code]
<?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;
}
?>
[/code]
도움이 되길 바랍니다. 항상 대가 없이 도와주시는 선배님들 감사합니다.
댓글 2개
좋은 정보 감사합니다.
꼭 필요한 정보였는데 감사합니다.
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5096 | PHP | 3년 전 | 1498 | ||
| 5095 | JavaScript | 3년 전 | 2127 | ||
| 5094 | JavaScript | 3년 전 | 2045 | ||
| 5093 | 기타 | 3년 전 | 1415 | ||
| 5092 | JavaScript | 3년 전 | 1962 | ||
| 5091 | 기타 |
|
3년 전 | 1320 | |
| 5090 | 기타 |
|
3년 전 | 2257 | |
| 5089 | 웹서버 |
|
3년 전 | 3330 | |
| 5088 | JavaScript | 3년 전 | 2060 | ||
| 5087 | 기타 | 3년 전 | 1659 | ||
| 5086 | 웹서버 |
|
3년 전 | 1702 | |
| 5085 | node.js | 3년 전 | 1646 | ||
| 5084 | JavaScript |
DogFoot개발
|
3년 전 | 1454 | |
| 5083 | 웹서버 |
DogFoot개발
|
3년 전 | 2989 | |
| 5082 | JavaScript |
DogFoot개발
|
3년 전 | 4026 | |
| 5081 | JavaScript |
DogFoot개발
|
3년 전 | 2100 | |
| 5080 | JavaScript |
DogFoot개발
|
3년 전 | 2439 | |
| 5079 | 기타 |
DogFoot개발
|
3년 전 | 2492 | |
| 5078 | OS | 3년 전 | 1650 | ||
| 5077 | 웹서버 |
DogFoot개발
|
3년 전 | 1723 | |
| 5076 | 기타 |
DogFoot개발
|
3년 전 | 1535 | |
| 5075 | 기타 |
DogFoot개발
|
3년 전 | 1676 | |
| 5074 | 기타 | 3년 전 | 2284 | ||
| 5073 | 기타 | 3년 전 | 2554 | ||
| 5072 | 웹서버 |
|
4년 전 | 2234 | |
| 5071 | 웹서버 | 4년 전 | 3236 | ||
| 5070 | PHP | 4년 전 | 3197 | ||
| 5069 | 웹서버 | 4년 전 | 2433 | ||
| 5068 | 기타 |
|
4년 전 | 3014 | |
| 5067 | 기타 |
DogFoot개발
|
4년 전 | 2684 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기