Zend Mail throws an exception (because mail() returns false) when the to name is set to something with both a foreign character (like "å") and a comma (","). Re-produce with code below.
$mail = new Zend_Mail('utf-8');
$mail
->setFrom('info#myhost', 'My company')
->setSubject('hi')
->addTo('MYEMAIL#SOMEHOST.COM', 'aå,a')
->setBodyHtml('<p>asd</p>')
->send();
If I change the addTo call to something of the below, no error occurs.
->addTo('znarkus#gmail.com', 'aåa')
->addTo('znarkus#gmail.com', 'a,a')
->addTo('znarkus#gmail.com', 'aa')
The weird thing is, even though it throws an exception ("Unable to send mail"), the mail is delivered. I'm running the latest Zend Mail (1.9.5?). Please halp!
It's just a bug in Zend_Framework:
http://framework.zend.com/issues/browse/ZF-10792
a comma is allowed in the name part of the e-mail:
"Smith, Frank"
this is okay
The problem is that mail() function for $to accepts
User <user#example.com>, Another User <anotheruser#example.com>
and I guess that PHP internally splits the string on commas to separate multiple recipients but you are providing only one email address.
If you think this is a Zend_Mail, or PHP bug you should post this to the appropriate issue tracker.
The comma is a reserved literal in the "to" part of a mail header (and you should never use it though), separating different targets. Even if your "first" mail gets sent, imho it creates a header like this:
aå, a <znarkus#gmail.com>
With this header i assume your mta tries to send two mails: one to aå, which fails (badly), and a second one to znarkus#gmail.com, which should make its way.
You could try to look into the mail headers to confirm this theory.
Related
I have read the following comment in the official documentation of php about mail() function:
Although it is not documented, for the parameters $to and $subject the mail() function changes at least \r and \n to space. So these parameters are safe against injection of additional headers. But you might want to check $to for commas as these separate multiple addresses and you might not want to send to more than one recipient.
Is it correct? Also, I have always considered the $message parameter safe against injection, is it also correct?
I know how to protect myself against injection, I just want to know if I can avoid to filter those parameters.
Yes, that's true, but it's also incomplete. In the engine source code, the function php_mail_build_headers ensures headers comply with RFC 2822 § 3.6 requirements for maximum number of values. Particularly, the following headers are checked for single value:
orig-date
from
sender
reply-to
to
bcc
message-id
in-reply-to
subject
Yes, the message parameter is safe from header injection by definition: the message part is inserted after the separating new line between headers and body, so any header-like text inserted as part of the message will appear as literal text within the message body.
For your comment that you don't want to apply those filters.
I think you can get it done automatically by using Zend_Mail as I commented.
$ composer require zendframework/zend-mail
I'm pasting this from their documentation:
use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail as SendmailTransport;
$message = new Message();
$message->addTo('matthew#example.org');
$message->addFrom('ralph#example.org');
$message->setSubject('Greetings and Salutations!');
$message->setBody("Sorry, I'm going to be late today!");
$transport = new SendmailTransport();
$transport->send($message);
I have seen this type of question asked multiple times and I've tried all the answers but had no luck so far;
The problem is when I try to send an email (I am using Zend Mail) with special characters in recipient name, it gets converted into html for e.g. A user is named Mateo Julián which gets replaced as Mateo Julián
I have tried html_entity_decode but had no success, added UTF-8 header and meta but they didn't work either. And one point to be noted is that I am facing this issue only while sending emails. Added UTF-8 also to mail object like this.
$mail = new Zend_Mail('UTF-8');
and it is called as
$mail->setBodyHtml($recipient_info['message'])
->setFrom($recipient_info["sender"]['email'],$recipient_info["sender"]['name'])
->addTo($recipient_info["receiver"]['email'],$blabla)// $blabla is the problem area where I send the name of recipient.
->setSubject($recipient_info['subject']);
I have a contact form, with mail() function.
For a long time it worked correctly. Suddenly now it doesn't work.
I have checked, but all is correct. But if I delete the sender parameter, it works, with sender parameters it doesn't work:
mail($destinatario_1,$oggetto_1,$messaggio_1,'');
WORKS
Instead:
mail($destinatario_1,$oggetto_1,$messaggio_1,'From: "My Name" <thisisanexample#gmail.com> \r\n');
Thanks
Try this instead:
mail($destinatario_1, $oggetto_1, $messaggio_1, "From: My Name <thisisanexample#gmail.com>");
Note that I leave our the \r\n which should just be \n on most servers and must be in double quotes not single quotes, and "My Name" shouldn't be in quotes.
Mail servers can be configured to not accept mail when certain information is set. The PHP mail() function more or less directly talks to the local mail server and passes the mail to it. Whether the mail server accepts it or not is not really part of the communication and cannot be detected by a PHP script.
Consult your web hosting company to find out if there are any restrictions.
How can i put commas in the 'From:' field of the mail headers??
For example with "From:Javier, My Site" when i read the email sended with any mail client like outlook, in the From only appears Javier#myinternalserverurl.com.
It cuts the from field by the comma... All the things that i found about 'From' field in php mails says that if you put commas automatically is treated like a list of mails.. any suggestion?
Thanks in adevice!
"From: \"Javier, My Site\" <email#domain.com>" should work
I'm using PHP _EOL when building the message body of my email but the line feeds are not getting through and the entire message body ends up one long line in the resultant email. This happens regardless of Multi-part or html only messages. Sending as text only it works fine, but of course I want to send Multi-part messages.... Any ideas?
Uhm. If there are no line breaks in your HTML email, it's probably because neither a \n nor a \r\n is a newline in HTML; a <br /> tag is.
I've never even used PHP_EOL before, but I wonder if it is set to the type of your server, not of the recipient. I don't see how a constant could be correct for all recipients, that doesn't make sense.
Usually '\n' is all that is needed... in some cases you may need '\r\n' depending on the protocol involved. What are you using to send the email? What are you using to view the email?