gpt4 book ai didi

带有重音的 PHP 邮件

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:26 32 4
gpt4 key购买 nike

我在发送主题带有口音的电子邮件时遇到问题。我到处搜索,但所有解决方案均无效。

我有一个字符串,主题,可能用 iso-8859-1 编码,通过命令行脚本发送

$res = shell_exec(sprintf("php mail.php %s %s %s %s",
escapeshellarg($email->sender),
escapeshellarg($receiver->email),
escapeshellarg($email->subject),
escapeshellarg($email->message)
));

还有邮件的标题:

$headers  = "MIME-Version: 1.0" . $endl;
$headers .= 'Content-type: text/html; charset="iso-8859-1"' . $endl;
$headers .= 'Content-Transfer-Encoding: 8bit';

结果,我收到了一封包含 ?????在主题中

注意:在 Windows 中测试,localhost

示例代码

testmail.php

<?php
$sender = "me@gmail.com";
$receiver = "you@mgmail.com";
$subject = "Accentued éàèçô letters";
$msg = "Accentued éàèçô letters";

shell_exec(sprintf('LANG="fr_FR.iso88591"; php mail.php %s %s %s %s > /dev/null 2>/dev/null &',
escapeshellarg($sender),
escapeshellarg($receiver),
escapeshellarg($subject),
escapeshellarg($msg)
));
?>

mail.php

<?php

if ($argc!=5) exit;

$sender = $argv[1];
$receiver = $argv[2];
$subject = $argv[3];
$msg = $argv[4];

$endl = "\r\n";

//HTML header
$headers = "From: $sender" . $endl;
$headers .= "MIME-Version: 1.0" . $endl;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $endl;
$headers .= 'Content-Transfer-Encoding: 8bit';


$msg = "
<html>
<head>
<title></title>
</head>
<body>
$msg
</body>
</html>
";

$receiver = explode(',', $receiver);

foreach ($receiver as $rec)
{
mail($rec, $subject, $msg, $headers);
sleep(5);
}
?>

执行:php testmail.php在 Linux 上测试。

解决方案这个问题的正确解决方案是使用base64_encode

'=?UTF-8?B?' . base64_encode($subject) . '?=';

最佳答案

好的,在对您的问题更新后的代码进行多次测试后,这里是基于您当前代码的有效解决方案:

MAIL.PHP (以UTF8编码保存的文件)

<?php

if ($argc!=5) exit;

$sender = $argv[1];
$receiver = $argv[2];
$subject = $argv[3];
$msg = $argv[4];

$endl = "\r\n";

// HTML header
$headers = "From: $sender" . $endl;
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers.= 'Content-Transfer-Encoding: 8bit';

// Message
$msg = "
<html>
<head>
<title></title>
</head>
<body>
$msg
</body>
</html>";

$receiver = explode(',', $receiver);

foreach ($receiver as $rec) {
mail($rec, $subject, $msg, $headers);
sleep(5);
}

?>

TESTMAIL.PHP (以UTF8编码保存的文件)

<?php

$sender = "inbox@domain.com";
$receiver = "outbox@domain.com";
$subject = "Accentued éàèçô letters";
$msg = "Accentued éàèçô letters";

setlocale(LC_CTYPE, "en_US.UTF-8");
shell_exec(sprintf('"LANG=en_US.UTF-8"; php mail.php %s %s %s %s > /dev/null 2>/dev/null &',
escapeshellarg($sender),
escapeshellarg($receiver),
escapeshellarg($subject),
escapeshellarg($msg)
));
?>

我已经对此进行了测试并正确接收了所有邮件。

关于带有重音的 PHP 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10582004/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com