dt56449492 2012-08-31 10:47
浏览 27
已采纳

Php电子邮件字符编纂

i'm using the following code to send e-mails from php.

    $to = 'myemail@domain.com'; 
$subject = 'Test email with attachment'; 
$random_hash = md5(date('r', time())); 
$headers = "From: webmaster@example.com
Reply-To: webmaster@example.com"; 
$headers .= "
Content-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"; 

However, when i receive the email on the webmail i have problems with the character codification. Characters like "à" or "ç" appears in strange forms.

Is this a problem of the codification i have defined in the code above?

Thanks!

  • 写回答

2条回答 默认 最新

  • duanmiaosi0150 2012-08-31 11:11
    关注

    Change headers to UTF8

    in the text/plain part

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

    and in the text/html part

    --PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="UTF-8" 
    Content-Transfer-Encoding: 7bit
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?