메일 사진 첨부하는 방법
안녕하세요. 저는 회원메일발송 기능을 이용할 때, 메일이 전송은 되는데 텍스트만 전송이 되고 첨부된 사진은 전송이 되지 않을 때 이렇게 코드를 바꿔서 해결했습니다.
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5186 | PHP |
swallow
|
2년 전 | 939 | |
| 5185 | PHP |
swallow
|
2년 전 | 960 | |
| 5184 | PHP |
swallow
|
2년 전 | 534 | |
| 5183 | PHP |
swallow
|
2년 전 | 834 | |
| 5182 | PHP |
swallow
|
2년 전 | 768 | |
| 5181 | 기타 |
swallow
|
2년 전 | 1016 | |
| 5180 | 2년 전 | 568 | |||
| 5179 | 기타 |
swallow
|
2년 전 | 1426 | |
| 5178 | PHP |
swallow
|
2년 전 | 791 | |
| 5177 | JavaScript |
swallow
|
2년 전 | 1483 | |
| 5176 | PHP |
swallow
|
2년 전 | 1181 | |
| 5175 | 2년 전 | 562 | |||
| 5174 | 기타 | 2년 전 | 809 | ||
| 5173 | PHP |
|
2년 전 | 962 | |
| 5172 | PHP |
|
2년 전 | 1251 | |
| 5171 | 기타 |
그누GPT
|
2년 전 | 1552 | |
| 5170 | PHP |
|
2년 전 | 1043 | |
| 5169 | PHP |
|
2년 전 | 1222 | |
| 5168 | PHP |
|
2년 전 | 1191 | |
| 5167 | JavaScript |
|
2년 전 | 993 | |
| 5166 | MySQL | 2년 전 | 1182 | ||
| 5165 | MySQL | 2년 전 | 1247 | ||
| 5164 | PHP | 2년 전 | 1496 | ||
| 5163 | OS |
|
2년 전 | 838 | |
| 5162 | 웹서버 |
|
2년 전 | 887 | |
| 5161 | 웹서버 |
|
2년 전 | 969 | |
| 5160 | PHP |
|
2년 전 | 840 | |
| 5159 | PHP |
|
2년 전 | 898 | |
| 5158 | PHP |
|
2년 전 | 907 | |
| 5157 | PHP |
|
2년 전 | 938 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기