Diskuze: Nefunguje UTF-8 v headers pre mail()
V předchozím kvízu, Online test znalostí PHP, jsme si ověřili nabyté zkušenosti z kurzu.


Ja jsem se koukal na nejake stranky a tam psali ze linknout sice muzes ale nebude to kompabiltni se vsemi email sluzbami, takze spise rovnou do dokumentu (mozna jsem to precetl blbe, kdyztak se podivej sam) . Jeste bych udelal nejakou sablonu protoze html v php neni moc hezke. Co jsem koukal na dokumentaci php tak by charset mel byt v Content Type, ale nezkousel jsem to.
text/html;charset=utf-8
Chartset asi neni ta spravna hlavicka , hlavicka pro mail v UTF8 by mela byt tato
pro plain email
$headers = [
'Content-Type: text/plain; charset="UTF-8";',
];
pro HTML email
$headers = [
'Content-Type: text/html; charset="UTF-8";',
];
Nebo v celku
$headers = [
'Content-Type: text/plain; charset="UTF-8";',
'From: ' . $email_address,
'Reply-To: ' . $email_address,
'Return-Path: ' . $email_address,
];
Spousta mailu moc neumi pracovat, kdyz pouzivas rozdilna kodovani.
Spousta ma problem s utf, uznava jen win1250, 1252 a pod.
Spousta mailu ignoruje nastaveni v headers nebo to povazuje jako nastaveni pro
body.
A obecne se doporucuje pouzivat base64.
A vetsinou odesilas mail s prilohou, tam jse kodovani udava az u prilohy.
Muzes zkusit pouzit moji class.
$from = "=?utf-8?B?".base64_encode($from) ."?=";
$to = "=?utf-8?B?".base64_encode($to) ."?=";
$subject = "=?utf-8?B?".base64_encode($subject)."?=";
<?php
class classMail
{
//private $to, $subject, $message, $headers;
private $sep, $server;
function __construct($server=array())
{
$this->sep = PHP_EOL;
$this->server = $server;
}
private function fileToBase64($str)
{
return chunk_split(base64_encode($str), 76, $this->sep);
// return wordwrap(base64_encode($data['content']), 76, $this->sep, true);
}
private function createMessage($data)
{
if (!isset($data['content']) || $data['content']=='' || !isset($data['type']))
{
return;
}
$out = array();
switch ($data['type'])
{
// pro mail-body
case 'text': // array('type'=>'text', 'content'=>'a\nb\nc')
case 'text_file': // array('type'=>'text_file', 'content'=>file_get_contents('path')}
{
$out[] = 'Content-type: text/plain; charset=utf-8'; //'Content-type: text/html; charset=iso-8859-1',
if ($data['type']=='text')
{
$out[] = 'Content-Transfer-Encoding: quoted-printable';
$out[] = '';
$out[] = $data['content'];
break;
}
$out[] = 'Content-Transfer-Encoding: base64';
$out[] = '';
$out[] = $this->fileToBase64($data['content']);
break;
}
case 'html':
case 'html_file':
{
$out[] = 'Content-type: text/html; charset=utf-8';
if ($data['type']=='html')
{
$out[] = '';
$out[] = $data['content'];
break;
}
$out[] = 'Content-Transfer-Encoding: base64';
$out[] = '';
$out[] = $this->filetoBase64($data['content']);
}
// pro mail-att
case 'file':
$out[] = 'Content-type: text/html; charset=utf-8';
$out[] = 'Content-Type: application/octet-stream; name="'.$data['filename'].'"';
$out[] = 'Content-Transfer-Encoding: base64';
$out[] = 'Content-Disposition: attachment; filename="'.$data['filename'].'"';
$out[] = '';
$out[] = $this->fileToBase64($data['content']);
break;
default: break;
}
return $out;
}
//private function headersCreate()
// {
// }
//private function messagesCreate()
// {
// }
//rfc2557 ? https://tools.ietf.org/html/rfc2557 | https://www.ietf.org/rfc/rfc2557.txt / 1999
// https://tools.ietf.org/html/rfc7231#section-3.1.4.2
public function send($form, $server=array()) //$from, $to, $subject, $message, $domena
{
$keys = array(
'from',
'to',
'notify',
'subject',
'message',
'att'
);
foreach ($keys as $value)
{
$$value = isset($form[$value]) ? $form[$value] : '';
}
if (!isset($from) || $from==""
|| !isset($to) || $to==""
|| !isset($subject) || $subject==""
)
{
return;
}
//if ($domena=="localhost ") {return false;} // upraveno
$from_text = $from;
$to_text = $to;
$subject_text = $subject;
$from = "=?utf-8?B?".base64_encode($from) ."?=";
$to = "=?utf-8?B?".base64_encode($to) ."?=";
$subject = "=?utf-8?B?".base64_encode($subject)."?=";
$uid = substr(sha1(uniqid(time())),0,20);
$uid = 'NextPart uid=' . $uid;
// $part_sep = '--NextPart uid=' . $uid;
// $uid_sep = '--'.$uid;
// ??? RFC 821, RFC 2821, rfc1341
$headers = array();
$headers[] = "From: $from";
$headers[] = "Reply-To: $from";
$headers[] = "Return-To: $from";
if ($form['notify']==true)
{
$headers[] = "Disposition-Notification-To: $from";
$headers[] = "Return-Receipt-To: $from";
}
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'X-Mailer: PHP/' . phpversion();
$headers[] = 'Content-Type: multipart/mixed; boundary="' . $uid . '"';
// $headers[] = ''; // pro slu.cz musi byt zakomentovano
$headers[] = 'This is a multi-part message in MIME format.';
$headers = implode($this->sep, $headers);
$messages = array();
$messages[] = 'test zprava';
if (isset($message) && trim($message)!=='') // $form['message']
{
array_unshift($att, $this->createMessage(array('type'=>'text', 'content'=>$message)));
}
if (count($att)>0)
{
foreach($att as $item)
{
$messages[] = '--' . $uid;
$messages[] = implode($this->sep, $this->createMessage($item));
// $messages[] = '';
$messages[] = '';
}
}
/*
Nefunguje to, aspon Thunderbird ten obrazek nezobrazi
$messages[] = '--' . $uid;
$messages[] = 'Content-Type:Text/html; charset="US-ASCII"';
$messages[] = '';
$messages[] = '<html><head><title>Test
</title></head>
<body><img src="http://www.dsv.su.se/~yvonne-b/yvonne.jpg"/> yvonne.jpg
<img src="teckning.gif"/> teckning.gif
<IMG SRC="cid:foo4atfoo1atbar.net" ALT="IETF logo">
</body></html>';
$messages[] = '';
$messages[] = '';
$messages[] = '--' . $uid;
$messages[] = 'Content-Type: image/jpg';
$messages[] = 'Content-Transfer-Encoding: base64';
$messages[] = 'Content-ID: <foo4atfoo1atbar.net>';
$messages[] = 'Content-Location: cid:foo4atfoo1atbar.net';
$messages[] = '';
$messages[] = 'R0lGODlhPABSAMQAAP///+He3tXU2sjK1bzB0K+3zKOtx5ajw4qavX2QuXCGtGR9r1dyrExppz9gojJW
nSZMmBlClAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAEALAAA
AAA8AFIAAAX/YCCOZGmewbKgbOu+55IkK2zfsEzjfF8KDofAR8QtHAaDo1ZsmgaKBCSSjEASioFze3g4
IAxFJKJgeB+Hre/QeDwW7wCj4WCk6l5GWv0SKB5jD3NDDSoNAQJ0gBBxfC00EAgNUQ4iDQ1zIg5YDQhu
CY4oAw0RaYUKhwGXl5YKC4cHEEyhAQMEAQkODyJzDEIBdUeIggx2AYIKAglDjioDYcYIYDQFCkFZDAkN
kbwNMwfGagVjArGFBnoODQZyuisGdAcLBq8NBQRjjkAIiFh+A0G8bAsCQVEQKH6W4ZpFRIAvXwpGEHjl
YIzFixjrMLgVgMCXgr94CDhy6UsqEQIA/80hViBIAYqYIjxgFqBASTAOIuKgQaecgQQEtIhYoIDANkAY
HxScyKTAswMGBjDaiAPBgnsNAIopJULBlDcJDhQYWwABjUU6';
$messages[] = '';
$messages[] = '';
*/
$messages[] = '--' . $uid . '--';
$messages = implode($this->sep, $messages);
// set mail server, if not set in php.ini
$server = count($server)>0 ? $server : $this->server;
$key_all = array('SMTP', 'smtp_port', 'sendmail_from');
foreach($key_all as $key)
{
if (isset($server[$key]))
{
ini_set($key, $server[$key]);
}
}
// send mail
//echo '<hr><pre>'; var_dump(array($to_text, $subject, $messages, $headers)); echo '</pre>';
//die();
return mail($to_text, $subject, $messages, $headers);
}
}
// $this->createMessage($messages, $item);
// $this->createMessage($messages, array('type'=>'file', 'content'=>file_get_contents($item['path'])));
/// $message = array();
// foreach ($messages as $value)
/// {
// }
?>
Jestli chces neco pro studijni ucely delat, muzes zkusit si udelat
ticketovaci system, ktery dokaze precist mailovou schranku a vytvorit nove
tickety nebo pridat zpravu do ticketu podle id
OpenTicket nebo neco takoveho jsem kdysi prepisoval, aby umel diakritiku a vim,
ze jsem se s tim dost natrapil, prave kvuli utf a ze to rozdilne cetlo hlavicku
a telo. A neumelo prilohy a tak.
Zobrazeno 8 zpráv z 8.