메일 사진 첨부하는 방법
안녕하세요. 저는 회원메일발송 기능을 이용할 때, 메일이 전송은 되는데 텍스트만 전송이 되고 첨부된 사진은 전송이 되지 않을 때 이렇게 코드를 바꿔서 해결했습니다.
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5126 | PHP |
|
3년 전 | 1797 | |
| 5125 | PHP | 3년 전 | 1598 | ||
| 5124 | PHP | 3년 전 | 2115 | ||
| 5123 | OS | 3년 전 | 1396 | ||
| 5122 | OS | 3년 전 | 1313 | ||
| 5121 | OS | 3년 전 | 1419 | ||
| 5120 | OS | 3년 전 | 1296 | ||
| 5119 | PHP |
|
3년 전 | 1143 | |
| 5118 | PHP | 3년 전 | 1353 | ||
| 5117 | Mobile |
|
3년 전 | 1397 | |
| 5116 | PHP | 3년 전 | 2291 | ||
| 5115 | MySQL |
welcome
|
3년 전 | 2903 | |
| 5114 | OS | 3년 전 | 2002 | ||
| 5113 | JavaScript | 3년 전 | 1539 | ||
| 5112 | PHP | 3년 전 | 1558 | ||
| 5111 | 기타 |
|
3년 전 | 1298 | |
| 5110 | PHP | 3년 전 | 4545 | ||
| 5109 | PHP | 3년 전 | 1578 | ||
| 5108 | 기타 |
|
3년 전 | 12175 | |
| 5107 | 기타 |
|
3년 전 | 2408 | |
| 5106 | 기타 |
|
3년 전 | 6321 | |
| 5105 | 기타 |
|
3년 전 | 3694 | |
| 5104 | 기타 |
|
3년 전 | 1501 | |
| 5103 | JavaScript | 3년 전 | 1691 | ||
| 5102 | 기타 |
|
3년 전 | 1269 | |
| 5101 | 기타 |
|
3년 전 | 1465 | |
| 5100 | 기타 |
|
3년 전 | 1305 | |
| 5099 | 웹서버 | 3년 전 | 2171 | ||
| 5098 | PHP | 3년 전 | 1326 | ||
| 5097 | PHP |
|
3년 전 | 1986 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기