2012-08-31 71 views
0

我使用下面的代碼從PHP發送電子郵件。Php電子郵件字符編碼

$to = '[email protected]'; 
$subject = 'Test email with attachment'; 
$random_hash = md5(date('r', time())); 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ $attachment = chunk_split(base64_encode(file_get_contents($_FILES['curriculum_vitae']['tmp_name']))); } 
ob_start(); 
?> 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

<?php if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ ?>--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['name']; ?>" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 
<?php } ?> 
<?php 
$message = ob_get_clean(); 
$mail_sent = @mail($to, $subject, $message, $headers); 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

然而,當我收到的webmail郵件我有性格編纂問題。像「à」或「ç」這樣的字符以奇怪的形式出現。

這是我在上面的代碼中定義的編碼問題嗎?

謝謝!

+0

你爲什麼要發送diffrent2類型的內容類型和標題? –

+0

好吧,我發現這段代碼om互聯網和改編的一些部分,我沒有改變那部分,我應該? – user1511579

+0

你使用的最糟糕的代碼不需要做所有這些,這使得它很簡單。看我的答案。 –

回答

2

更改標題爲UTF8

text/plain部分

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="UTF-8" 
Content-Transfer-Encoding: 7bit 

,並在text/html部分

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="UTF-8" 
Content-Transfer-Encoding: 7bit 
+0

完美Mihai,謝謝! – user1511579

0

使用該使用該代碼使用PHP腳本來發送郵件

<?php 
$to = "[email protected]"; 
$subject = "Test mail"; 
$message = "Hello! This is a simple email message."; 
$from = "[email protected]"; 
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers); 
echo "Mail Sent."; 
?> 

不需要在腳本上設置附加信息。我不知道你爲什麼要設置sevral標題和內容類型編碼。

+0

已經解決了,感謝評論! – user1511579