Why Headers Preventing mail from sending? - php

This might sound similar like previously asked questions but trust me it's not
I Was trying to send an email that uses an HTML template via PHP mail() function from Localhost and a Hostinger Server but they created different problems.
On localhost the email was being sent as plain text although there were headers
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
I have gone through all the similar questions in stackoverflow and tried each and every thing but I couldn't make it work. After some more research on this I found out this
I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it
Therefore I switched over my hosting and tried to do the same but this time I found that the headers are causing the problem. The email is not sent if the header variable is passed in the mail() function. I tried to concatenate the headers which didn't worked. Then I made an array of headers and joined them with php implode which too didn't worked. On a similar question on stackoverflow I found that webmails mess up if html, head, body tags are used as they use xhtml. I removed them and still no success.
I tried error reporting too and it showed module sqlite3 already loaded which I think is not related to mail.
Below is my code
php
<?php
$email_template = file_get_contents("path/to/my/template");
$lucky_number = rand(999999, 111111);
$email_template = str_replace("{{user}}", "User", $email_template);
$email_template = str_replace("{{lucky_number}}", $lucky_number, $email_template);
$sender = "from:iusername#host.com"; // I found that if I dont use from, my mail ends up in spam folder
$receiver = "username#host.com";
$subject = "Random Subject Name";
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
if(mail($receiver, $subject, $email_template, $sender, $headers))
{
echo "Email Sent Successfully";
}
else
{
echo "Email Sending Failed";
}
P.S I can't use PHPMailer or other similar libraries

The sender information should be inside the headers
Hence, please change the following lines:
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
if(mail($receiver, $subject, $email_template, $sender, $headers))
to
$sender = "iusername#host.com";
$headers = "From: $sender <$sender>\r\nReply-To: $sender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
if(mail($receiver, $subject, $email_template, $headers))

Related

php mail function goes straight to junk folder [duplicate]

This question already has answers here:
Prevent sent emails treated as junk mails using php mail function
(14 answers)
Closed 9 years ago.
I have set up a contact form, with basic fields. The form is sent to the site owner and a message sent to the customer submitting the form. The function works but allways send the message to the junk folder. Is there something wrong with my headers or is it just hotmails junk settings.
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: contact#mysite.com\r\n";
mail($to, $subject, $message, $headers)or die("mail error");
The message's are HTML emails, just a nicer way of displaying the message. Also in my email account instead of displaying my from varable it says ,' CGI Mailer' -- confusing.
UPDATE - no longer works at all
//mail customer
$from = 'donotreply#mysite.co.uk';
$subject = 'Your message has been recieved.';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: My Site <'.$from.'>' . "\r\n";
$msg = '
<html>
<head>
<link href="http://linktocss/.../etc" rel="stylesheet" type="text/css" />
</head>
<body>
formatted message...
</body>
</html>
';
mail($email, $subject, $msg, $headers)or die("mail error");
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.
Edit
This worked for me, and did not end up in SPAM/Junk, but in my Inbox.
<?php
//mail customer
$from = 'donotreply#mysite.co.uk';
$to = "email#example.com";
$subject = 'Your message has been received.';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: The Sending Name <$from>\r\n";
$msg = '
<html>
<head>
<link href="http://linktocss/.../etc" rel="stylesheet" type="text/css" />
</head>
<body>
formatted message...
</body>
</html>
';
mail($to, $subject, $msg, $headers)or die("mail error");
?>
Footnotes: I noticed that you are using an external stylesheet. Many Email services such as Google will not render those. It's best to use inline styling to achieve better/desired results.
Original answer
The (most likely) reason (as per your posted code) why your mail ends up in SPAM is that your headers are invalid.
You have rn bunched up together, instead of using \r\n
Use this instead:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: contact#mysite.com\r\n";
mail($to, $subject, $message, $headers)or die("mail error");
Or better yet, use: As taken from the PHP website's mail() function
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: contact#mysite.com\r\n";
mail($to, $subject, $message, $headers)or die("mail error");

Wierd characters at the end of any email: PHP MAIL

Well I am using the PHP MAIL function and for some reason every email it sends has a weird;

This is at the end of any email as I said, I am not quite sure why this is happening.
$from = 'From: support#phycraft.co.uk';
$to = $user_email; // Send email to our user
$subject = 'PhyCraft Support Ticket :: Closed :: ' . $t_subject; // Give the email a subject
$message = '
Hello '. $username.'.
Your support ticket '.$t_subject.' has been closed due to being inactive for 48 hours.
If you still need help with the ticket please reopen the ticket by replying to it.
~PhyCraft
';
$headers = 'From:support#phycraft.co.uk' . "\r\n"; // Set from headers
mail($to, $subject, $message, $from); // Send our email
I can't see what in the code woud make that appear to be honest.
Most issues with php's mail() function are problems with the MTA and not with PHP itself. I've never heard of this before making it even more likely it's a MTA issue.
You've not provided any useful information beyond the PHP code. What OS is this on (mail() on MSWindows is very different from everyhere else). Do you control the server? What MTA is configured? Have you tried sending an email from the command line?
The extra stuff at the end looks like HTML - is this byte for byte what's in the email or what you see in your mail client?
BTW it's not a good idea to explicitly put "\r\n" at the end of your headers - but you seem to have forgotten to add them as a parameter. Also, your missing a space between "From:" and the email address.
Can you try it with following $headers ( only \n ).
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset = \"ISO-8859-1\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: support#phycraft.co.uk\n";
$headers .= "\n";
mail($to, $subject, $message, $headers);
and 2. try it without
$headers .= "Content-Transfer-Encoding: 8bit\n";

Mail sent with mail() function does not get sent [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php mail function
I am doing this website where I send emails to users when they for example forget their password or similar, but for some reason I can not send emails with the following function:
$email = 'somemail#mail.com';
$subject = 'subject';
$message = 'message blablablablabla';
mail($email, $subject, $message);
Am I doing something wrong or missing something in the code, or is it the hosting company's fault? (I make my website on x10hosting.com). I checked in the manual about mail() but it didn't help me.
Thanks in advance.
Update
Thanks for the help guys, but it turned out to be a problem on the web hosting company I'm on. Everything's working fine now.
try to use with headersenter code here
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "From: <".$frommail.">\r\n";
$headers .= "Reply-To: ".$frommail."\r\n";
$mail_sent=mail($tomail, $msg, $headers);`enter code here`
I would guess either there is no sendmail_from value set in php.ini or your host does not support email or has not set it up correctly.
Try setting a from header, and if that doesn't work, contact your host:
mail($email, $subject, $message,'From: you#example.com');
is this on local host? or is it on a webserver
Also remember that mail($to, $subject, $contents) returns a boolean,
if(mail($to, $subject, $body){
echo "Message has been sent";
}
else{
echo "Error has occurred"
}

PHP Mail Send when using a tag

PHP mail sending problem when using a tag, it doesn't come to new line.
HERE is my code having same problem
$subject = 'Watch Out Our Colorful Web Design Presentation';
$headers = "From: " . $email . " \r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "Bcc: test#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "Watch Out Our Colorful Web Design Presentation.\r\n";
$message .= "<a href='http://www.stackoverflow.com'>CLICK HERE</a>\r\n";
mail($to, $subject, $message, $headers);
Mail send successfully but having problem in \r\n. It doesn't take new line. I tried br tag too. But it goes in junk mail.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
You're sending a HTML email. This means that you should be using HTML instead of newlines. To avoid having your emails placed in the junk folder, you should read some of the many Stackoverflow topics on the subject.
You have to use <br> tag for new line.
You can't use \n for new line for html printing.
https://bugs.php.net/bug.php?id=9542
As others have said, you need to be using HTML line breaks <br /> if you are sending an e-mail with Content-Type: text/html. The newlines/carriage returns will be interpreted in the source of the message as line breaks but they will probably not be rendered as HTML.
When sending e-mail from PHP I would always suggest using an e-mail class rather than PHP's native mail functions.
I tend to use SwiftMailer. It has the advantage that all mail headers are sanitized and escaped to avoid header injection exploits that could potentially fire out spam through your script. Also it's easier to use a variety of e-mail transports. There's also a great decorator plugin which can send thousands of messages with customised strings, useful for doing things like "Dear {first_name} {surname}" or customised unsubscribe/tracking links.
Here's some sample code for SwiftMailer just in case you are interested...
// START SWIFTMAILER
require_once($swiftmailer_path);
$swift_transport = Swift_SendmailTransport::newInstance($sendmail_cmd);
$swift = Swift_Mailer::newInstance($swift_transport);
$swift_msg = Swift_Message::newInstance($swift_transport);
$swift_msg->setMaxLineLength(150);
$swift_msg->setFrom( array('NoReply#domain.com' => 'MyWebsiteName'));
$swift_msg->addTo($user);
$swift_msg->setSubject($subject);
$swift_msg->setBody($msg_html, 'text/html');
$swift_msg->addPart($msg_txt, 'text/plain');
// SEND E-MAIL
if ($swift_result = $swift->send($swift_msg)) {
// SENT SUCCESSFULLY
} else {
// ERROR - E-MAIL NOT SENT
}

Form doesn't send info to email address (works on others though)

My client has a Wordpress content management system to which I added a simple contact form with a php form handler. The contact form sends the information by email correctly to all three of my email addressses, but when I change to my client's email address the email never arrives. I have run out of ideas where I could look for the problem. No it does not go to his junk mail folder. :)
Sounds like the email is being routed "internally" through your clients network and not out onto the internet. The chances are they have some restrictions on what machines can be used to send emails internally, or the mail routing system sees the internal email as being "different" and does something odd with it.
Try using (from a cli) :
echo "Testing " | mailx -"Test Subject Line" user#company.co.uk
What is the mail function that you are using? Do you attach a header to it? It sounds like it is being marked as spam from the exchange server. What I use (and have always worked for me) is something like this:
`
function mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="")
{
$subject = nl2br($subject);
$sendmailbody = nl2br($sendmailbody);
if($bcc!="")
{
$headers = "Bcc: ".$bcc."\n";
}
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8 \nContent-Transfer-Encoding: 8bit\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP/"."MIME-Version: 1.0\n";
$headers .= "From: " . $from . "\n";
$headers .= "Content-Type: text/html\n";
mail("$sendto","$subject","$sendmailbody","$headers");
}
`

Categories