0

我正在使用以下代码从 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 上收到电子邮件时,我遇到了字符编码问题。“à”或“ç”等字符以奇怪的形式出现。

这是我在上面的代码中定义的编码问题吗?

谢谢!

4

2 回答 2

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
于 2012-08-31T11:11:03.053 回答
0

使用此代码使用 php 脚本发送邮件

<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

无需在脚本上设置其他信息。我不知道您为什么要设置多个标头和内容类型编码。

于 2012-08-31T11:11:46.390 回答