회원메일발송 사진 첨부 관련 질문드립니다.
안녕하세요 챗 gpt한테 물어봐서 첨부한 사진 파일을 회원메일발송기능으로 보내는 것 까지는 성공했습니다. 근데 이렇게 되면 항상 CID랑 경로를 제가 직접 입력해야만 보내줄 수 있어서요.
어떻게하면 함수같은 걸 써서 자동으로 CID화랑 경로 추출을 할까요? gpt한테 물어보고
코드는 그럴듯한데 되는 코드가 없더라고요 ㅠㅠ.
-----------------------------------------------------------------------------------------------------------
1. allow_url_fopen 문제 해결
서버 설정에서 allow_url_fopen이 0으로 설정되어 있어서 외부 URL을 통해 파일을 가져올 수 없습니다. 이를 해결하기 위해서는 두 가지 접근 방법이 있습니다.
서버 설정 변경
서버의 PHP 설정을 변경하여 allow_url_fopen을 1로 설정해야 합니다. 이를 위해서는 웹 호스팅 제공업체에 요청하거나, 서버에 대한 접근 권한이 있다면 직접 php.ini 파일을 수정합니다. php.ini에서 다음과 같이 설정을 변경합니다:
-----------------------------------------------------------------------------------------------------------
이건 gpt 답변인데 제가 건들 수 있는게 맞나 싶고요.
그누보드 5.6.4 쓰고 닷홈은 무료버전 쓰고 있습니다.
아래 소스코드는 ib/mailer.lib.php 파일입니다. config.php에서 smtp설정한 것 말고는 다른 파일은 건든 것이 없습니다.
감사합니다.
답변 1개
$mail->addEmbeddedImage(G5_PATH.'/data/editor/2408/c3cb22671109dd1265fb3938b6871942_1724307239_6789.jpg', 'image_cid', 'c3cb22671109dd1265fb3938b6871942_1724307239_6789.jpg');
이부분을 수정을 해야한데 G5_DATA_URL 로 변경해보세요
답변에 대한 댓글 4개
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/2408/'.$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;
}
?>
이렇게 만들어서 해결했습니다 선배님. 감사합니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인