2011-11-22 1 views
2

전자 메일을 보내야하는 C++ 응용 프로그램을 작성했습니다.postfix sendmail (콘솔에서 메일 보내기)에서 움라우트 (비 ASCII 문자) 처리

그것은

/usr/sbin/sendmail -f [sender] -t 

호출하고 센드 프로세스의 표준 입력 메일 헤더 및 본문을 작성하여이를 수행.

움라우트 또는 다른 비 ASCII 문자를 제외한 모든 것이 잘 작동합니다. 어떻게 제대로 작동하게 할 수 있습니까?

는 이미 메일 헤더로

Content-Type: plain-text; charset=ISO-8859-1 

을 설정하려고도

Content-Type: plain-text; charset=UTF-8 

는 아무것도 변경하지 않았다. 이 헤더가 무시 된 것 같습니다.

답변

4

이메일 메시지의 ASCII 이외의 문자는 일반적으로 quoted-printable 또는 base64으로 인코딩해야합니다. 그러면 Content-Transfer-EncodingContent-Type 헤더가 적절하게 설정되어 수신자가 비 ASCII 텍스트로 메시지를 다시 디코딩하는 방법을 알 수 있습니다.

#!/bin/bash 
message="Hellö ümläüts" 
encoded=$(base64 <<< "$message") 

/usr/sbin/sendmail -t <<< "From: [email protected] 
To: [email protected] 
Subject: Dear friend 
Content-Transfer-Encoding: base64 
Content-Type: text/plain; charset="utf-8" 

$encoded" 

당신은 데이터를 base64로 인코딩되기 전에 이진 문자열을 변환하는 데 사용 된 어떤 문자 인코딩을 지정해야합니다

다음은이 명령 행에서 수행 할 수있는 방법을 보여 떠들썩한 예입니다.

이 예제는 일반적인 플랫폼 기본값이므로 utf-8을 사용합니다. 따라서 대부분의 셸은 문자열을 바이너리로 변환하고 stdin의 base64으로 전달할 때 utf-8을 사용합니다.

+0

bse64 전송 인코딩을 사용하지 않고 따옴표 붙은 인쇄물로 메시지를 인코딩하려면 Content-Type을 어떻게 사용해야합니까? 이것은 심지어 의미가 있습니까? –

+0

@James이 경우'Content-Type'을 변경할 필요가 없지만'Content-Transfer-Encoding'을'quoted-printable'로 변경해야합니다. – Martin

0

이 질문에 답하면 수신자에게 전자 메일을 보내기위한 bash 함수를 작성했습니다. 이 함수는 utf-8로 인코딩 된 메일을 보내고 base64 인코딩을 수행하여 제목과 내용에 utf-8 문자를 사용합니다.

는 일반 텍스트 이메일을 보내려면

send_email "plain" "[email protected]" "subject" "contents" "[email protected]" "[email protected]" "[email protected]" ... 

은 HTML 이메일을 보내려면 :

여기
send_email "html" "[email protected]" "subject" "contents" "[email protected]" "[email protected]" "[email protected]" ... 

함수 코드입니다.

# Send a email to recipients. 
# 
# @param string $content_type Email content mime type: 'html' or 'plain'. 
# @param string $from_address Sender email. 
# @param string $subject Email subject. 
# @param string $contents Email contents. 
# @param array $recipients Email recipients. 
function send_email() { 
    [[ ${#} -lt 5 ]] && exit 1 

    local content_type="${1}" 
    local from_address="${2}" 
    local subject="${3}" 
    local contents="${4}" 

    # Remove all args but recipients. 
    shift 4 

    local encoded_contents="$(base64 <<< "${contents}")" 
    local encoded_subject="=?utf-8?B?$(base64 --wrap=0 <<< "${subject}")?=" 

    for recipient in ${@}; do 
    if [[ -n "${recipient}" ]]; then 
    sendmail -f "${from_address}" "${recipient}" \ 
     <<< "Subject: ${encoded_subject} 
MIME-Version: 1.0 
From: ${from_address} 
To: ${recipient} 
Content-Type: text/${content_type}; charset=\"utf-8\" 
Content-Transfer-Encoding: base64 
Content-Disposition: inline 

${encoded_contents}" 
    fi 
    done 

    return 0 
} # send_message() 

희망이 도움이 될 수 있습니다.