Encoding problem in From email and name when sending email in PHP? - php

What could be causing this encoding problem? Am I encoding the header information correctly or could this be caused somewhere else?
I found a similar problem here: Email from PHP has broken Subject header encoding but from what I'm reading I think I'm doing this right.
Basically sometimes our clients send emails written in french. It works but every once in a while we get some encoding issues and I'm wondering if I did the encoding wrong (I can't figure out).
$from = '=?UTF-8?B?'.base64_encode($email['from_name']).'?= <'.$email['from_email'].'>';
$to = '=?UTF-8?B?'.base64_encode($email['to_name']).'?= <'.$email['to_email'].'>';
$get_param = array(
'Content-Type' => 'text/plain;charset=utf-8',
'Content-Transfer-Encoding' => '8bit');
The code above is how I encode my header information and below is the sending:
$msg = $message_mime->get($get_param);
$headers = array (
'From' => $from,
'To' => $to,
'Reply-To' => $from,
'Subject' => utf8_decode($subject));
$headers = $message_mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $smtp_settings['host'],
'auth' => true,
'username' => $smtp_settings['username'],
'password' => $smtp_settings['password']));
$mail = $smtp->send($to, $headers, $msg);
The problem is that the email they received was from:
Clientnamé de l'Ontario[=?UTF-8B?QXNzZW1ib(...)=?=]
Thanks for any help :)
Edit: The client's name basically uses the same characters as above. What I mean is that the email was supposed to be from "Clientnamé de l'Ontario"

Did you try replacing base64_encode with utf8_encode?
I'd try replacing:
$from = '=?UTF-8?B?'.base64_encode($email['from_name']).'?= <'.$email['from_email'].'>';
$to = '=?UTF-8?B?'.base64_encode($email['to_name']).'?= <'.$email['to_email'].'>';
with:
$email['from_name'] = utf8_encode($email['from_name']);
$email['to_name'] = utf8_encode($email['to_name']);
$from = $email['from_name'].' <' . $email['from_email'] . '>';
$to = $email['to_name']) . ' <' . $email['to_email'] . '>';
Might be worth a try...
Also detect_encoding could help (http://php.net/mb_detect_encoding/)?

Related

Php script sending mail twice with different timestamp although called just once

I've read many posts with a topic like my title express. But there's no help for me and I'm totally stuck. I'm using this chunk of code:
function acceptCallbackData() {
$body = "test";
$subject = "Message sent by php";
$to = "a-person#parascus.de";
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From: another-person#parascus.de' . "\r\n";
$from = "another-person#parascus.de";
$host = "xxxx";
$username = "xxxx";
$password = "xxxx";
$headers = array ('From' => $from, 'Subject' => $subject, 'Content-type' => 'text/html; charset=utf-8');
require_once "Mail.php";
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
$_SESSION["ERRORS"] = "";
exit();
}
I think the address of the "To" exists only once (not in the call and also the headers) and the exit() makes sure (and firebug tells me as well) that this script/function is executed only once.
But I still get the mail to times and there can be a different timestamp as well. Any suggestions how I can fix this problem?
Best regards
parascus
First thing when debugging PHP mail (when sending from a front-end javascript/jQuery/etc. client) is to remove the client from the equation.
Can you call the php function on its own and reproduce the duplicate mails? If not, then your problem lies with your front-end. If so, then you need to find where in your php code the function is being reproduced.
In this case, we determined that it was indeed client-side.

PHP Html Email using Pear HTML Table and Pear Email

I am facing quite strange issue, I've php script which sends out an email to the list of users containing HTML table(with the data) and CSV attachment of the same data. The problem is when that script to send out more then 10 emails, in some of the emails we get
--=_f3be233a9b22e88524e1539166c49be0 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8
in email body and HTML table lost the formatting as well, however CSV data is absolutely fine in that email.
Basically the purpose of the script is to pick the data according to user subscribe information(database-table) and send it to the subscribe user early morning. Every night the database tables are updated with the new data, which is picked up by that PHP script and send in an email. Further that PHP script trigger by Windows Schedule task.
Below is the code of the application.
if($ix>0)
{
while(odbc_fetch_row($subRes2))
{
$s_email=odbc_result($subRes2, 1);
//echo $s_email;
$from = "Sender <alerts#xxx.com>";
$to = "$s_email";
$subject = "Information_DB:" . $arrayE . " Restored on: " . "-";
$host = "xxxinternal.xxxx.com";
$port = "25";
//disabled the username and password because currently sending unauthenticated email.
// $username = ""; //<> give errors
//$password = "";
//add the email headers 29082013 1008 - SMS
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => false));
//'username' => $username,
//'password' => $password));
$subject = "Results from query";
$crlf = "\n";
$mime_params = array(
'text_encoding' => '7bit',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8',
'head_charset' => 'UTF-8'
);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
//$mime->setTXTBody($body);
$mime->setHTMLBody($body);
$file='./wamp/www/' . $csv_filename;
$mime->addAttachment($csv_filename,'application/octet-stream');
$body = $mime->get($mime_params);
$headers = $mime->headers($headers);
// Sending the email
// $mail =& Mail::factory('mail');
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
// echo "<script>window.close()</script>";
}
else {
echo("<p>Message successfully sent!</p>");
}
}}}
else
{
$from = "Sender <alert#xxx.com>";
$to = "$s_email";
$subject = "Information_DB:" . $arrayE . " Restored on: " . "-";
//$host = "ssl://smtp.gmail.com";
$host = "xxxinternal.xxxx.com";
$port = "25";
//disabled the username and password because currently sending unauthenticated email.
// $username = ""; //<> give errors
//$password = "";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8');
?>
Unfortunately when we tested I could't seen this issue. But when its went UAT lots of user get this issue and it seems to be consistent with the database. For e.g. if user A is subscribe with Spain and Russian data, then consistently they experience the issue with Russian Data email (contains the mentioned headers in the email) however Spain data email is fine. In the live its probably sending around 100's of email daily.
I will appreciate if any of you please point me out what is going wrong as I am setting up the UTF-8 character set in an email mime setup but still getting this issue.
Regards
Shoaib
Most probably you are sending emails too fast that may occur in the result of spam/unwantd email use sleep(4) to create the 4 sec delay in next `mail.
The problem was with mime type, which require to code again. Basically the message I was sending was not encoded with UTF-8 which I did. This time I user Mail_mimePart instead of Mail_mime and it works treat.
That example was very very helpful
---ahttp://tiger-fish.com/comment/reply/133---
and some of the parameter explanation for Mail_mimePart can be found on below link
ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.mail-mimepart.php---
How to attach the attachment using Mail_mimePart.
ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.addsubpart.php---
Hope it helps.

pear mail function bcc won't send

I copied a code for PEAR mail from a website, and input my data. It works. It sends mail, however, I want to use bcc to send to a lot of people and keep their addresses anonymous, and it will send to the $to recipients, but not the $bcc.
The code:
<?php
$message = "yay email!";
require_once("Mail.php");
$from = 'myaddress#mysite.com ';
$to = "anadress#gmail.com";
$bcc = "thepeopleimemailing#yaddayadda.com";
$subject = " test";
$body = $message;
$host = "smtp.mysite.com";
$username = "myusername";
$password = "mypassword";
$headers = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject
);
$recipients = $to;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => '25'
)
);
$mail = $smtp->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
}
else {
echo("Message successfully sent!");
}
?>
P.s. I read on anther forum that I shouldn't put the headers in an array? I'm having trouble grasping the concept of the headers. What do they do and how should I organize them? I just want a to, from, subject, and bcc.
Thanks!
To elaborate on Chaky31's answer to send a Bcc use the following, note that we do NOT specify any Bcc information in the header:
//All other variables should be self explanatory!
//The main recipient
$to = "test#test.com";
//Bcc recipients
$bcc = "bcc#test.com";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
//We append the bcc addresses as comma seperated values to the send method
$mail = $smtp->send($to . "," . $bcc, $headers, $body);
For those who are looking to the solution to adding cc and bcc in PEAR php mail. Here is the simple solution and the abbreviated explanation why.
ANSWER: Everyone who want to receive the mail must be added to the $recipients field. If they are not in this field, they will not get the mail. Everything you want to be visible, add to the header. Therefore, since bcc is BLIND carbon copy, do NOT add it to the header.
WHY?: The recipient field dictates where the mail goes, the headers dictate what is displayed. If you do not add cc to the header, then you can make them blind too. Whichever tickles your fancy. Any questions, check the link ripa added above! Great explanation!
use $headers['Cc'] = 'cc#example.com, bb#example.com, dd#ex.com';
see the link below for pear mail
Sending multiple CC's and BCCs with PHP PEAR MAIL
or can get help from
http://phpmailer.worxware.com/index.php?pg=exampledb -- it is not pear mail. but it works very fine. I have used this and it is very easy to integrate.

Syntax for sending a email header attachment [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send emails with PHP using the PEAR Mail package with attachment
I want to send an email with PHP using a generated attachment (not a file on the server), I was curious about the syntax.
<?php
#require_once "Mail.php";
$from = "foo#me.com>";
$to = "blah#me.com>";
$subject = "Hi!";
$body = "Attached is the file\n\n";
$attachment = header( 'Content-Type: text/csv' );
$attachment = $attachment.header( 'Content-Disposition: attachment;filename=example.csv');
$attachment = 'transactionid,valid,fname,lname,email,studentid,status
"654","1","First Name","Last Name","Email#me.com","1000000"';
$body = $body.$attachment;
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = #Mail::factory('smtp', array('host' => $host,
'port' => $port,
'auth' => true,
'username' => $mail_username,
'password' => $mail_password));
$mail = #$smtp->send($to, $headers, $body);
if (#PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
EDIT: Please note, i know there are many examples of attaching a file from the server in an email, but I am asking about attaching a generated csv file (that is NOT on the server) to an email.
Start with How to send emails with PHP using the PEAR Mail package with attachment
Then replace the following:
if ($mime->addAttachment($file,'application/pdf')){
with
$file = 'transactionid,valid,fname,lname,email,studentid,status
"654","1","First Name","Last Name","Email#me.com","1000000"';
if ($mime->addAttachment($file,'text/csv', 'example.csv', false)){
see documentation at http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

How to format email using SMTP

I'm trying to send formatted email with php smtp authentication, but for some reason(s) \n, \r\n and PHP_EOL can not format the message, hence everything appears on a line.
Below is what I've tried
$order .= "Title : {$title}" . PHP_EOL
."Author : {$author}" . PHP_EOL
."ISBN : {$isbn}" . PHP_EOL
."Publisher : {$publisher}" . PHP_EOL
."Supplier : {$supplier}\n
Quantity : {$qty}\r\n
The variable $order contains the body of the email.
Here is part of my sendMail() function
$from = "{$sender_name} <{$sender}>";
$to = $reciever." <$email>";
$subject = $subj;
$body = $msg;
$host = "mail.thekingsbooks.com";
$username = "noreply#thekingsbooks.com";
$password = "password";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory(
'smtp',
array(
'host' => $host,
'auth' => true,
username => $username,
password => $password
)
);
$mail = $smtp->send($to, $headers, $body);
The sent mail looks like this
Ordered items\r\nTitle : A Retreat With Pope John Paul II: Be Not Afraid (Retreat With-- Series)\r\nAuthor : \r\nISBN : 0867164204\r\nPublisher : \r\nSupplier : \r\n\r\n Quantity : 6\r\n\r\n
Any suggestion on how to format the email body? Thanks
If you are viewing this in Outlook, Outlook may be stripping the whitespace. In Outlook, click on the option that shows the whitespace restored.
It could be that it's being sent as HTML. Trying adding "Content-type: text/plain; charset=iso-8859-1" to your headers.

Categories