Sending mail with php: Header gets broken - php

I use the following code to send a mail:
$recipient="mymail#example.com";
$body="<html><body>This is magic</body></html>";
$subject="hi folks!";
$thisMail="sender#example.com";
$replyHeader = "MIME-Version: 1.0\r\nContent-Type: text/html; charset=UTF-8\r\nFrom: ".$thisMail. "\r\nReply-To: ".$thisMail."\r\n";
mail($sender, "Re:".$subject, $body, $replyHeader);
The mail is sent, but when I open it in a client mailer, the header seems to be broken. Everything after the first New Line (\r\n) is shown in the mail. Also some other header-contents are displayed. So the visible body of the mail looks like this in a mail client:
Content-Type: text/html; charset=UTF-8
From: sender#example.com
Reply-To: sender#example.com
Envelope-To: mymail#example.com
X-UI-Filterresults [... lots of stuff]
<html><body>This is magic</body></html>
What is going wrong there?
One (out of many) examples that show it "should" work that way: http://css-tricks.com/sending-nice-html-email-with-php/

Rather than relying on the mail function there are some really great libraries out there that handle headers, attachments, content and sending for you.
One of them - which is used by Laravel and other frameworks - is SwiftMailer, you should check it out.
They have some good documentation on setting headers too!
Honestly, you'll gain a lot from making use of the library and not reinventing the wheel.

Related

Mail go to spam instead of inbox and some errors in header part of mail using PHP

i am trying for send mail using php.My mail go to spam and some other errors are also in mail.
My header code is
$header_mail="select content from mail_header where id='1'";
$header_mail2=mysql_query($header_mail);
$fet=mysql_fetch_array($header_mail2);
$content= htmlentities($fet['content']);
$Headers = "From:$content\r\n" .
"Reply-To:$content\r\n" .
"Content-type: text/html; charset=UTF-8 \r\n";
$Headers.= "MIME-version: 1.0\n";
$Headers.= "Content-type: text/html; charset= iso-8859-1\n";
data in $content is zamisoft<zamisoft.com> but i got the mail as with
from: Zamisoft&lt
reply-to: Zamisoft&lt,
zamisoft#gmail.com&gt
I got the these message in mail
"Be careful with this message. Many people marked similar messages as phishing scams, so this might contain unsafe content. Learn more "
Mail is going to spam and errors are in header section part of mail.
Any body help me for solve these issue?
The problem is simple that the PHP mail() function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.
Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are gone.
https://github.com/PHPMailer/PHPMailer
remove the htmlentities() from $content= htmlentities($fet['content']); and then try!
Since you are already setting the Content Type and Character Encoding, the contents of the array $fet['content'] will be read properly by the browsers!
htmlentities() converts the html tags to htmlentities (eg. < to <) which is what you are facing!!
Try it and let us know if the problem persists
Remove the line "Content-type: text/html; charset=UTF-8 \r\n"; as you have defined the header in the last line of header.
Add $Headers .= 'X-Mailer: PHP/' . phpversion()."\r\n"; at the last line of your code.
It tells which version of php is being used!
Also the email address should be a valid one as well!!

Issue with headers using fsockopen for ESMTP connection on php

I send email without a problem but when i look on the headers i can see that before of the ones i create appears "X-Mailbox-Line: " and i have no idea why, does any one knows why ?
An example:
X-Mailbox-Line: From : <contacto#nidida.com>
Reply-To: <contacto#nidida.com>
MIME-Version: 1.0
Usually the X-* headers add added by receiver SMTP, so your code it's not the problem!

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.

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

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.

PHP send MIME emails...doesn't work anymore

I just switched from a shared server to a virtual dedicated. The whole site works, but my mail forms have quit sending MIME emails the way they used to.
What used to appear before the server switch:
hallo fwend
What appears now in the emails (appears as though my client doesn't support MIME):
Content-Type: multipart/alternative; boundary="==DL-
bound_b08bcfabdc459ae7e87e493a984115b3"
This is a multipart MIME message from
your friends at Example.com. If you
are reading this, it seems we have a
message to deliver, but your mail
client is not set to receive these
types of messages. We are sorry for
the inconvenience :(
--==DL-bound_b08bcfabdc459ae7e87e493a984115b3
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: 7bit
hallo fwend
Is there some switch in PHP or something that I need turned on to send MIME?
It look like the headers are send in the body part of the message.
I had this problem a while back.
Possible causes are:
1. EOL
I used "\r\n" as eol for the MIME headers (following the spec)
, but the (shared)server wanted an "\n"
2. Last header
Some mail server add some headers of their own. If the last header end with "\n" and the mail server could also mess thing up.
3. Empty header
An empty header which causes a "\n\n" in the $headers will corrupt the headers. The "\n\n" aka "empty line" marks the end of the headers part of a email (or the start of the body)

Categories