我使用下面的代碼從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郵件我有性格編纂問題。像「à」或「ç」這樣的字符以奇怪的形式出現。
這是我在上面的代碼中定義的編碼問題嗎?
謝謝!
你爲什麼要發送diffrent2類型的內容類型和標題? –
好吧,我發現這段代碼om互聯網和改編的一些部分,我沒有改變那部分,我應該? – user1511579
你使用的最糟糕的代碼不需要做所有這些,這使得它很簡單。看我的答案。 –