我正在使用以下代码从 php 发送电子邮件。
$to = 'myemail@domain.com';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$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 上收到电子邮件时,我遇到了字符编码问题。“à”或“ç”等字符以奇怪的形式出现。
这是我在上面的代码中定义的编码问题吗?
谢谢!