=?UTF-8?B??= in Emails sent via php mail problem - php

I have a website, and in the "Contact" section I have a form which users may fill in to contact me.
The form is a simple form which action is a php page.
The php code:
$to = "email#domain.com";
$name=$_POST['name']; // sender name
$email=$_POST['email']; // sender email
$tel= $_POST['tel']; // sender tel
$subject=$_POST['subject']; // subject CHOSEN FROM DROPLIST, ALL TESTED
$text=$_POST['text']; // Message from sender
$text.="\n\nTel:".$tel; // Added to message to show me the telephone nr to the sender at bottom of message
$headers="MIME-Version: 1.0"."\n";
$headers.="Content-type: text/plain; charset=UTF-8"."\n";
$headers.="From: $name <$email>"."\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $text, $headers, '-fno-reply#domain.com');
Could somebody please tell me why this works most of the time, but sometimes I receive email whith no text and the subject line showing
=?UTF-8?B??=
I use outlook express, and I have read this System.Net.Mail and =?utf-8?B?XXXXX.... Headers
but it didn't help.
The problem is not in Outlook, because when I log in to the actual mailprogram where I fetch the POP3 emails from, the email looks the same.
When I right click in Outlook and chose "message source" then there is no "From" information.
Ex, a good message should look like this:
Subject: =?UTF-8?B?w5Z2cmlndA==?=
MIME-Version: 1.0
Content-type: text/plain; charset=UTF-8
From: John Doe
However, the ones with problem looks like this:
Subject: =?UTF-8?B??=
MIME-Version: 1.0
Content-type: text/plain; charset=UTF-8
From:
As if the information has been lost somewhere.
You should know also that I have a VPS, which I manage myself.
I use postfix as an emailserver, if thats got anything to do with it.
But then again, why does it work sometimes?
Also another thing that I have noticed is that sometimes special characters are not shown correctly (by both Outlook and the webmail).
For instance, the name "Björkman" in swedish is shown like Björkman, but again, only sometimes.
I hope anybody knows something about this problem, because it is very hard to track down for me atleast.
If you need more input let me know.
Thanks

When looking at PHP mail(), I see another line breaking: shouldn't you use \r\n as line endings?
And that Björkman example looks very much like your mail is not recognized as UTF-8 encoded.

Related

Email header with custom CC in format: "Name <email>" in php

Question:
How do I get an email header to have a CC with the format: "name <email>"? (I have not found much to help with this when its not the TO or FROM address)
Summary of Issue:
An email CC field in the format: "name lastname " produces a header like: "name#emaildomain; lastname#emaildomain; email#emaildomain;" rather then the expected format.
Generating the header
The cc field is populated on a php form with fields for Subject, To, CC, and Body text POSTS to an email handling class. Inside the handler class, the From, BCC, and CC field are written into a header string using an addHeader() method.
function addHeader($type,$value)
{
$this->header .= $type.":".$value.$this->eol;//eol = PHP_EOL
}
When the mail is actually sent it uses a send() method (that I sadly dont feel comfortable detailing too extensively) but the essential point is that it creates the full header string that looks much like this:
From: Company Name CC:Long Last Name, First Name <longlastname#company.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xb964d9146ea853337b28e6b471a8ca00x"
Which is added to a php mail():
$result = mail($this->to, $this->Subject , $this->Body, $this->header);
What I have tried:
I have tried MIME encoding the header but it doesn't seem to help.
From: =?UTF-8?Q?Company=20Name?= Cc: =?UTF-8?Q?Long=20Last=20Name,=20First=20Name?= <longlastname#company.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x4cfee11b9185861f4ba890079305ecb0x"
I also tried replacing the white space characters with alternatives like &nbsp or (space) but it did not solve the issue either.

imap_fetchbody can't handle mails sent with phpmailer?

I am sending some emails with phpmailer with the regular example on their website.
the email is sent, and it arrives perfectly to the sent address.
after the mail is sent, I am saving the email to my sent items folder with imap_append, using the info of this answer Sent mails with phpmailer don't go to "Sent" IMAP folder
and in third place, I am creating a custom made webmail which reads my imap folders.
the problem is that I can't get the html body of the email sent correctly.
When I open the mail on sent items on thunderbird, or my phone, the body is shown perfectly.
but when I try to get the html body with php_imap functions I can't make it!!.
I created an isolated file to fetch the body of the msg like this:
$body = imap_fetchbody($conn,3392,1,FT_UID);
that returns this:
--b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ REMOVED CONTENT --------------------- klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ
REMOVED CONTENT
---------------------
klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b--
If I try
$body = imap_fetchbody($conn,3392,1.2,FT_UID);
don't get anything
If I try
$body = imap_fetchbody($conn,3392,2,FT_UID);
don't get anything, it seems that PHPMailer creating the emailbody in a way that imap_fetchbody can't handle?
thank you

How to remove 'via' email address while sending mails using php [duplicate]

When I send a mail with PHP the destinatary gets a header like this one:
noreply#justwalk.it **via** de p3nlhg147.shr.prod.phx3.secureserver.net
I want to remove the "via" part. Most automated mails from websites don't have the "via" so it's certainly possible to remove it.
How do they do it?
Yes, you can get rid the "via" part. Here's the details:
1) SPF and DKIM
Firstly, you would need to set an SPF record for the domain you are sending emails from and enable DKIM as well. These are primarily for identifying your messages against spam.
2) "From: anything#yourdomain.com"
Secondly, make sure you are setting the “From: ” header to be an email address on the domain you are sending messages from. Don’t pretend to be someone else. Use “From: someone#abc.com” if you are sending the messages from abc.com, rather than anything else, such as blah#def.com, or yours#gmail.com, or whatever. If you want the recipient to reply to your Gmail email instead of your domain email, use the “Reply-To: ” header. “From: ” must always be the domain email that you are sending the email from.
3) "Return-Path: return#yourdomain.com"
Thirdly and most importantly, set the “Return-Path: ” header to be the same domain as that of the “From: ” header. Use the 5th parameter of the mail() function for this:
mail('recipient#example.com', 'Subject', "Message Body", $headers, '-freturn#yourdomain.com')
So the Return-Path of this message would be “return#yourdomain.com” (the email address immediately following the -f switch). The $headers parameter should contain all the necessary message headers. Make sure “From: ” is something#yourdomain.com.
After these steps and measures, Gmail should now completely trust your messages from yourdomain.com. The ‘via‘ field of your messages should be gone and the ‘mailed-by‘ field as well as the ‘signed-by‘ field should be correctly showing up as yourdomain.com.
Hope it helps!
I also fetched the same problem. But I have overcome the problem by using the following code:
mail('maaaa#abcd.com', 'the subject', 'the message', null,'-faaa#abc.com');
Make sure that last parameter is -f with the email address.
You can add the
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";<br />
mail('maaaa#abc.com', 'the subject', 'the message body in html format', $headers,'-faaaa#abc.com');
for the html message body in email.
This is probably added by your MTA and you didn't say which MTA you are using.
I'd recommend sending the mails not by PHP's mail() function but via SMTP, possibly even with SMTP-Auth, using something like PHPMailer.
#Mujibur is also right, But I used. But Didn't missed Headers too.
mail($to, $subject, $message, $headers, '-f'.$from_email_address);
And its successful to me, Let's check it from your side.
See what Google says about this here: http://support.google.com/mail/bin/answer.py?hl=en&ctx=mail&answer=1311182
All the best!

email header injection - example not working

first of all this question is for personal knowledge, and not for any kind of attack :) hope you'll believe me and give me some hints.
I'm trying to reproduce an example of mail header injection I found (link-> http://www.phpsecure.info/v2/article/MailHeadersInject.en.php). Basically it uses a form to get 3 parameters (subject, message and sender mail), then these parameters are sent with POST method and used in the php mail() function to an admin's mail.
Everything works fine, each mail is sent without problem but when I try to inject some other parameters as Cc, Bcc etc the trick doesn't work: neither \r & \n nor %0A & %0D are interpreted as CL and RF. For example, if I put my#mail.com%0ACc:foo#bar.com in the "From" field, in "my#mail.com" inbox I'll find the mail, with the same "From" field as it was sent (my#mail.com%0ACc:foo#bar.com). Does php or does input tag encode (or unencode) properly the input? How can I make it work?
Hope you can understand my bad english, thanks in advance, best regards.
ps: the article I linked is dated 2005, recently I've found that a similar bug with http headers splitting using php function "header()" was fixed, so I thought that they fixed email headers injection problem too.. But I can't find anything on the web that confirms this.
______________________EDIT________________________________________
Example working, modifying header within php code:
$to = "admin#mail.com";
$sub = "this is the subject";
$msg = "this is the message";
$header = "From: foo#foo.com"."\r\n"."Cc: bar#bar.com";
$if(mail($to, $sub, $msg, $header."\n")){
echo "sent";
}else{
echo "error";
}
The email is correctly received both from foo#foo.com and bar#bar.com
Examples NOT working (this is the problem I'd like to solve with your help):
Once I send the mail with "send" button, only foo#foo.com will get the e-mail, and in the "from" detail (inside the mail) I'll find (1st case) foo#foo.comrnCc: bar#bar.com or (2nd case)foo#foo.com%0D%0ACc: bar#bar.com.
I always find i need to use both \r\n in order for the headers to be sent properly.

Basic phpmailer question

I am using phpmailer to send my newsletter to my subscribers.
I set my headers as following:
$headers = "From: Sales - Blah Blah <sales#blahblah.com>";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
NOW, in the delivered email, for example in a gmail account, if you press the "show details" link, the following appears.
from Sales - Blah Blah MIME-Version: 1.0
sales#blahblah.com via ecbiz103.inmotionhosting.com
to blahblah#gmail.com
date Wed, Aug 3, 2011 at 11:02 AM
subject sales !
***mailed-by ecbiz103.inmotionhosting.com***
HERE IS THE QUESTION:
HOW CAN I MAKE IT ACTUALLY SEND THE EMAIL FROM THE ACTUAL EMAIL ACCOUNT, that is "sales#blahblah.com" and be able to see the outgoing emails on my "sent" tab?
I guess, when this happens, the last line mentioned in bold would say
"mailed by blahblah.com"
Regards, George
PHPmailer as in http://phpmailer.worxware.com? Or do you mean PHP's built-in mail() function? It would appear you're using mail(), since you're building your own MIME message. Don't do that. It's too much main and unreliable. use PHPmailer (at the link mentioned earlier) or Swiftmailer to do that sort of thing for you.
Either way, if you want the mails you send to show up in your account's "sent" folder, you'd have to send the emails via your own email account using blahblah.com's mail server. Another option would be to set a BCC: to silently copy yourself on each mail, and have a mail rule on your account to divert those mails to a folder.
The simple answer is: you cant.
All phpmailer does is connect to the mail server to send the mail. It does not link to your actual e-mail account in any way at all.
EDIT:
Also, you will not be able to remove the ecbiz103.inmotionhosting.com reference as that is the hostname of the server that is sending the mail. A mail server runs on one IP address, this IP has a hostname, in this case ecbiz103.inmotionhosting.com.
You don't have a newline after the first line of headers, specifically the From header.

Categories