php - Sending email with mail() -
i'm making kind of email sender class , have problems:
- some email services russian mail.ru don't understand encoding of message , show utf-8 mail in windows-1251 charset. gmail ok that.
- all email services show text/html plain text.
- multipart messages broken well.
also should note, i'm sending local server build on openserver using gmail smtp server.
opinion problem in kind of mybullshitcode or in smth covers content-type header.
theres code:
class _email { protected $type; protected $to; protected $subject; protected $plain; protected $html; protected $charset; protected $headers = array( 'mime-version' => '1.0', // 'content-type' => '', // 'from' => '', // 'cc' => '', // 'bcc' => '', // 'reply-to' => '', // 'subject' => '', // 'return-path' => '' ); public function __construct($to, $subj, $datacharset, $sendcharset) { $this->to = self::hencode($to, $datacharset, $sendcharset); $this->subject = self::hencode($subj, $datacharset, $sendcharset); $this->headers['subject'] = $this->subject; $this->datacharset = $datacharset; $this->sendcharset = $sendcharset; } public function setheader($el, $val) { $this->headers[$el] = $val; } public function setplain($message) { $this->plain = iconv($this->datacharset, $this->sendcharset, $message); } public function sethtml($message) { $this->html = iconv($this->datacharset, $this->sendcharset, $message); } /* static */ public static function send($to, $mail) { if ($mail->plain !== null && $mail->html !== null) { $r = sha1(uniqid()); $mail->headers['content-type'] = "multipart/alternative; boundary=$r"; $message = "--$r content-type: text/plain; charset=".$mail->sendcharset." content-transfer-encoding: 7bit ".$mail->plain." --$r content-type: text/html; charset=".$mail->sendcharset." content-transfer-encoding: 7bit ".$mail->html." --$r--"; } else if ($mail->html !== null) { $mail->headers['content-type'] = 'text/html; charset='.$mail->sendcharset; $message = $mail->html; } else if ($mail->plain !== null) { $mail->headers['content-type'] = 'text/plain; charset='.$mail->sendcharset; $message = $mail->plain; } else { return false; } $headers = ''; $mail->to = $mail->to." <$to>"; $mail->headers['x-mailer'] = 'php/'.phpversion().' - blabla '.core::build_info(); foreach ($mail->headers $key=>$val) { $headers .= $key.': '.$val.'\r\n'; } return mail($to, $mail->subject, $message, $headers); } private static function hencode($str, $data_charset, $send_charset=false) { if ($send_charset && $data_charset != $send_charset) { $str = iconv($data_charset, $send_charset, $str); } else { $send_charset = $data_charset; } return '=?'.$send_charset.'?b?'.base64_encode($str).'?='; } }
i tried make subj
, to
headers in base64_encode - seems hopeless.
all kind of appreciated!
upd: quoted-printable variant
public function setplain($message) { $this->plain = quoted_printable_encode(iconv($this->datacharset, $this->sendcharset, $message)); } public function sethtml($message) { $this->html = quoted_printable_encode(iconv($this->datacharset, $this->sendcharset, $message)); } $message = "--$r content-type: text/plain; charset=".$mail->sendcharset." content-transfer-encoding: quoted-printable ".$mail->plain." --$r content-type: text/html; charset=".$mail->sendcharset." content-transfer-encoding: quoted-printable ".$mail->html." --$r--";
upd2: using phpmailer... gmail email subject cyrillic chars in end.
here subject. Ðто заголовок!
the body acceptable:
this html message body in bold!. Такое сообщение!
code:
$mail = new _email; $mail->from = 'admin@ss133d.ru'; $mail->fromname = 'ss133d administration'; $mail->addaddress($email, $login); $mail->addreplyto('admin@ss133d.ru', 'ss133d administration'); $mail->ishtml(true); $mail->subject = 'here subject. Это заголовок!'; $mail->body = 'this html message body <b>in bold!</b>. Такое <b>сообщение</b>!'; $mail->altbody = 'this body in plain text non-html mail clients. А это без html!';
so maybe need use iconv? theres subj conversion utf-8 windows-1252...
have thought of using phpmailer here
it requires use email , email password in config values. secure , information wont released thats option use.
Comments
Post a Comment