php mail() 함수로 첨부파일 보낼때 파일이름이 한글일 때? 채택완료
돌소프트
2년 전
조회 3,990
안녕하세요?
아무리 해도 잘 안되네요.
php mail() 함수로
그림파일이나, txt파일 .zip 파일을 첨부파일로 보낼 때
영문자 파일이름일때는 이상없이 잘 가는데
파일이름이 한글일 때는 "0 바이트"로 전송되거나, 가지 않는 경우 여러 공개 소스를 사용해 보았는데, 한글파일이름 제대로 보내는 소스를 찾지 못하였습니다.
대부분 , 안 가거나 0바이트 코드 뿐이라서요.. 고수님들의 답변을 부탁드립니다.
댓글을 작성하려면 로그인이 필요합니다.
답변 2개
채택된 답변
+20 포인트
2년 전
mail() 함수를 사용하여 한글 파일 이름을 가진 첨부 파일을 전송하는 경우, 몇 가지 사항을 확인해야 합니다. 1. 제대로 된 문자 인코딩 설정: mail() 함수를 사용하기 전에 문자 인코딩을 올바르게 설정해야 합니다. 예를 들어, UTF-8 인코딩을 사용하려면 다음과 같이 헤더를 추가합니다:
$headers = "Content-Type: text/plain; charset=UTF-8\r\n";
$encoded_file_name = base64_encode($file_name) ;
$boundary = md5(uniqid(rand(), true));
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\r\n";
$message = "--{$boundary}\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "메일 본문 내용\r\n\r\n";
$message .= "--{$boundary}\r\n";
$message .= "Content-Type: application/octet-stream; name=\"{$encoded_file_name}\"\r\n";
$message .= "Content-Disposition: attachment; filename=\"{$encoded_file_name}\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode(file_get_contents($file_path))) . "\r\n";
$message .= "--{$boundary}--\r\n";
mail($to, $subject, $message, $headers);
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8'; // 문자 인코딩 설정
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = '첨부 파일 테스트';
$mail->Body = '첨부 파일 테스트입니다.';
// 파일 첨부
$file_path = '/path/to/file.jpg';
$file_name = '한글파일명.jpg';
$mail->addAttachment($file_path, $file_name);
if (!$mail->send()) {
echo '메일 전송 실패: ' . $mail->ErrorInfo;
} else {
echo '메일 전송 성공!';
}
로그인 후 평가할 수 있습니다
답변에 대한 댓글 1개
�
돌소프트
2년 전
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
이런 것 같습니다.