2014-04-11 1 views
4

저는 base64로 인코딩 된 다중 부분 이메일을 얻고 swiftmail을 통해 보내려고합니다. 다음 코드는 내가 지금까지 가지고 있습니다 :SWIFTMAIL로 base64 이메일 보내기

$message = Swift_Message::newInstance("Email Template Test") 
    ->setBoundary($boundary) 
    ->setFrom(array('[email protected]' => 'Mailer Service')) 
    ->setTo(array("[email protected]","[email protected]")) 
    ->setBody($plaintext) 
    ->addPart($htmlmail,"text/html"); 

$headers = $message->getHeaders(); 
$headers->addTextHeader('Content-Transfer-Encoding','base64'); 

$contenttype = $message->getHeaders()->get('Content-Type'); 
$contenttype->setValue('multipart/alternative'); 

를 지금까지 내가 (내가 너무 명확 찾지 않는) 문서에서 볼 수 있듯이, Content-Transfer-Encoding 헤더 텍스트 헤더, 그래서 내가 할 수 있어야 위와 같이 설정하십시오. 이 전에 모든 현재 헤더의 출력을 실행했고 Content-Transfer-Encoding이 거기에 나열되어 있지 않으므로 설정해야했습니다. 따라서 위의 코드에서 왜 설정하려고 시도했는지.

출력이 정상적으로 작동하지만 전자 메일을 수신하지만 원본을 볼 때 인코딩되지 않았습니다. 위의 코드를 사용해 보았지만 $plaintextbase64_encode($plaintext)으로 변경했지만 인코딩 된 메시지가 수신되었습니다. 작동 방법>

답변

2

version 5.4에서 인코더를 설정할 수 있습니다. 그렇지 않으면 Swift_Message은 원시 인코더를 사용하여 메시지를 인코딩합니다.

$message = \Swift_Message::newInstance("Email Template Test"); 
$message->setEncoder(\Swift_Encoding::getBase64Encoding()); 
//... 

은 또한 부호화 및 addPartbug (as of version 4 and 5)있다. MimePart은 원본 메시지에서 인코딩을 상속받지 않습니다. 이렇게하려면 MimePart을 수동으로 만들어 원본 메시지에 첨부해야합니다.

$part = \Swift_MimePart::newInstance(); 
$part->setEncoder($message->getEncoder()); 
$part->setBody($htmlmail, 'text/html'); 
$message->attach($part); 

이 자동으로뿐만 아니라 Content-Type: multipart/alternative; boundary=****, 경계 charsetContent-Transfer-Encoding: base64 헤더 정보를 추가합니다.

결과 :

var_dump($message->toString()); 

string 'Message-ID: <[email protected]> 
Date: Thu, 14 Jan 2016 20:45:30 +0000 
Subject: Email Template Test 
From: Mailer Service <[email protected]> 
To: [email protected], [email protected] 
MIME-Version: 1.0 
Content-Type: multipart/alternative; 
boundary="_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_" 


--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_ 
Content-Type: text/plain; charset=utf-8 
Content-Transfer-Encoding: base64 

VGhpcyBpcyBhbiBodG1sIG1lc3NhZ2U= 

--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_ 
Content-Type: text/html; charset=utf-8 
Content-Transfer-Encoding: base64 

VGhpcyBpcyBhIHRleHQgbWVzc2FnZQ== 

--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_-- 
' (length=751)