메일 사진 첨부하는 방법
안녕하세요. 저는 회원메일발송 기능을 이용할 때, 메일이 전송은 되는데 텍스트만 전송이 되고 첨부된 사진은 전송이 되지 않을 때 이렇게 코드를 바꿔서 해결했습니다.
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5066 | 기타 |
DogFoot개발
|
4년 전 | 1817 | |
| 5065 | PHP |
DogFoot개발
|
4년 전 | 1522 | |
| 5064 | PHP |
happyl
|
4년 전 | 1914 | |
| 5063 | node.js |
DogFoot개발
|
4년 전 | 1671 | |
| 5062 | node.js |
DogFoot개발
|
4년 전 | 1687 | |
| 5061 | node.js |
DogFoot개발
|
4년 전 | 1410 | |
| 5060 | node.js |
DogFoot개발
|
4년 전 | 1246 | |
| 5059 | node.js |
DogFoot개발
|
4년 전 | 1245 | |
| 5058 | 기타 |
DogFoot개발
|
4년 전 | 2692 | |
| 5057 | 웹서버 |
DogFoot개발
|
4년 전 | 2715 | |
| 5056 | MySQL | 4년 전 | 1499 | ||
| 5055 | 기타 | 4년 전 | 1427 | ||
| 5054 | OS | 4년 전 | 2063 | ||
| 5053 | 웹서버 | 4년 전 | 3617 | ||
| 5052 | OS | 4년 전 | 2413 | ||
| 5051 | PHP | 4년 전 | 2183 | ||
| 5050 | 웹서버 | 4년 전 | 1712 | ||
| 5049 | MySQL | 4년 전 | 1887 | ||
| 5048 | OS | 4년 전 | 2690 | ||
| 5047 | PHP | 4년 전 | 4364 | ||
| 5046 | MySQL | 4년 전 | 1805 | ||
| 5045 | MySQL | 4년 전 | 1928 | ||
| 5044 | MySQL | 4년 전 | 4122 | ||
| 5043 | MySQL | 4년 전 | 1879 | ||
| 5042 | 기타 |
|
4년 전 | 2629 | |
| 5041 | MySQL | 4년 전 | 1986 | ||
| 5040 | MySQL | 4년 전 | 1712 | ||
| 5039 | MySQL | 4년 전 | 1485 | ||
| 5038 | MySQL | 4년 전 | 1327 | ||
| 5037 | OS | 4년 전 | 4424 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기