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.
Related
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.
I have this really weird issue that am troubled with and do not know how to fix it!
Sample code i used here Wroks and sends me the email correctly!
<?php
require_once "Mail.php";
$from = "Web Master <contact#sample.com>";
$to = "bob <sample#sample.com>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";
$host = "mail.emailsrvr.com";
$username = "sample#sample.com";
$password = "11111";
$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);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
But when i put this code into a email password reminder php code it stops working!
i have put the code at the very front or at the place where u click to send button
to trigger, nothing works and i have no idea why!
the funny part is i had it working if i slowly copy in parts of the code and it WORKS
for about the first one or two email and it stops working after...
the code that comes before the reminder.php before the email script is below
<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
if(get_magic_quotes_runtime())
{
set_magic_quotes_runtime(false);
}
include("include/dbcommon.php");
$cEmailField = "email";
$reminded=false;
$strSearchBy="username";
include('libs/Smarty.class.php');
$smarty = new Smarty();
$strUsername="";
$strEmail="";
$strMessage="";
$conn=db_connect();
// Before Process event
if(function_exists("BeforeProcessRemindPwd"))
BeforeProcessRemindPwd($conn);
Please ! if you have any idea on the solution to this?
mail("sample#sample.com","Test email","test body here");
check if this works?
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
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.
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/)?