Sending only textual content with mail function in php - php

I have read that i must include some headers when using mail() function in PHP like:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
but in my case i want only to send text, not html. so are those headers will still be required to make a successful mail?

Headers of mail() function are optional.
Your email can be delivered successfully, without any header.
Note that some default values will be set. As an example, if you do not declare a sender (From), the server name will be used instead.

Related

PHP Email Bcc from form

I've created a form to get information from end users and email to a specific person. I'd like for the form to also be emailed to the submitter. Everything works, except when I try to Bcc the submitter. If I add the line to Bcc, the email is not sent. If I delete the line to Bcc the email is sent correctly, but the submitter doesn't have a copy of the email. Here is the code,
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: $email' . "\r\n";
$headers .= 'Bcc: $email_bcc' . "\r\n";
Add the BCC Receiver to the recipients but NOT to the headers!
Using Bcc in the header should be fine. However, make sure, that your mail server is not killing the mail because of it.1
Check the return value of the mail function. This should return false, if an error occured. If that is the case, you should send an extra mail to the sender.
Windows Server seem to have a problem with this.

how to use clickabe link (href link) in php mail

i want to use click able link in email, but it is not reflecting in email sending through php mail function, below is my code
$url = "<html><a href='www.google.com'>Link</a></html>";
$message = "Hi test url ".$url." ";
$$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Admin <test#ex.com> \r\n";
#mail('test1#ex.com',$subject,$message,$headers);
Content which i'm getting from email:
Hi test url <html><a href='www.google.com'>Link</a></html> ##NEW##
You don't seem to be sending HTML mail correctly. Usually I will recommend using a third-party PHP library, like SwiftMailer, to simplify the process.
Edit: If you still wish to use php's mail() function, you will have to specify the headers correctly. This article could help.
To use the HTML in phpmailler:
$mail->isHTML(true)
You review the documantation
phpMailler

Modify date header in PHP email?

Is it possible to manipulate the date header using PHP's mail function/sendmail (website server is linux so I believe it's using sendmail underneath)?
I'm trying to test error handling of a (windows based) email checker, and need to generate an invalid date header in a message, but when I send my message using php mail, even though I am including a custom Date header, it seems to get overridden by the mail server and my custom header ignored. I'm guessing I might need an additional parameter for sendmail to tamper with that header, but I can't seem to find what that parameter would be, or whether this is even possible.
Here is what I am sending (with the personal domain info removed)
$headers = "From: ..................\r\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#..................>\n";
$headers .= "MIME-Version: 1.0\n";
//$headers .= "Date: ".date("D, d M Y H:i:s") . " UT\n"; //a valid header for comparison
$headers .= "Date: Tuesday\n"; // intentionally bogus email header
$headers .= "Reply-To: ..................\n";
$headers .= "Return-Path: ..................\r\n";
$headers .= "X-Priority: 3\r\nX-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP/".phpversion()."\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "\n";
$success=mail($to, $s_subject, $s_emailmsg, $headers);
Assuming you wrote an email checker that runs via procmail, an easier way might be to construct an email message yourself and send that directly to the test script via standard input.
It's easier to write unit tests that way as it bypasses any interference by other mail servers. When you do follow this, make sure that the first line starts with MAIL FROM xx#yy.com.

Extra characters in PHP email "from" field

I'm trying to send email from PHP, and although it's working, I'm noticing that I'm getting a lot of extra stuff in the "from" part of the email. The extra stuff, I'm assuming, is
information from my server?
For example, if $from = "myname", instead of "myname" only coming through in the email from field as is, I get something like:
myname#p3nlhg.phx3.secureserver.net
Here is my PHP I'm playing with, the important part being my $from var:
$from = "myname";
mail($to,$formSubj,$formMssg,$from);
Honestly that's all I have, nothing more or less (as far as the $from is concerned). How can I clean this up or make it so that only the plain value of $from, which is in this case "myname", comes out?
The 4th parameter of the mail function doesn't work this way. It expect header information. To achieve what you want to do use:
$header = "From: myname <no-reply#test.com> \r\n";
mail($to,$formSubj,$formMssg,$header);
You shouldn't use a lonely myname either. You need a valid email addess string: myname <no-reply#test.com> or no-reply#test.com. If you just put myname, PHP will try to interpret it as an email address. That's why you're getting myname#p3nlhg.phx3.secureserver.net. p3nlhg.phx3.secureserver.net must be the hostname of your server.
A nice thing about the header parameter is that you can use it to specify other less common fields. e.g.:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
Notice that you need to place a carriage-return/line-feed (\r\n) at the end of each header attribute. A simple line feed will not work.
According to the mail documentation, you're using $from as the $additional_headers argument.
Instead, change $from to an appropriate header. For example,
$from = "myname";
$headers = "From: $from <user#example.com>";
mail($to, $formSubj, $formMssg, $headers);
According to the documentation for additional_headers,
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.
It's coming from the mail server that you have installed in your server, try to switch to another one, like postfix, courier or someting else.

Required Mail Headers

I have a website in which I send a confirmation mail as part of the registration process.
Some time ago, I had some troubles with the mails I sent since I used no headers (PHP mail function).
Once I put some headers, I've gotten more responses from users, but I suspect that not every message reaches its destination.
How can I be sure that the messages reach their destination?
Which are the headers that can be considered a 'must'?
This is the code of my SendMail function
mail($to,
$subject,
$message,
"MIME-Version: 1.0\n".
"Content-type: text/plain; charset=ISO-8859-1; format=flowder\n".
"Content-Transfer-Encoding: 8bit\n".
"Message-Id: <" . md5(uniqid(microtime())) . "#mysite.com>\n".
"Return-Path: <admin#mysite.com>\n".
"X-Mailer: PHP v".phpversion()."\n".
"From: admin# mysite.com");
You should use external library for working with e-mails in php like PhpMailer , SwiftMailer or Zend_Mail. All your problems will go away.
The headers need a white space at the bottom to separate the header from main body.
Tools like Spam Assassin will give you a big mark down for that.
Also you should use \r\n as a line terminator instead of just \n
From PHP.net
Multiple extra headers should be separated with a CRLF (\r\n).
The headers seems quite good to me. The only glitch I see is an extra whitespace in the From header.
I'm sure you already checked it, but just in case ...
"From: admin# mysite.com");
should be (?)
"From: admin#mysite.com");
This is a working mail function I'm using for html mail and variable $return is defined to get error report from mail server in case of fail delivery.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$from.'>' . "\r\n";
$return = '-f'.$from;
#mail($to, $subject, $msg, $headers, $return);
you can see more detail at here sugunan.com
The headers look ok, except for the details pointed by #Eineki. Also if you are using Windows you need to send the $to param in the form "user#mail.com" and not "Username ", because it may cause trouble, due to the way the mail() function is implemented on windows platform, the "to" address may be parsed incorrectly.
You should add a Date: header (its mandatory by RFC5322) and some mail-clients may assume January 1 1970 as an e-mail date if none is given (and it gets lost between all the other old messages).

Categories