php mail() function not sending mail when I use text/html - php

So when I execute this without modifying the header information to send html type emails it sends however when I have it send with it being an html email it never gets sent.
here is the code:
<?php
$to = "someone#gmail.com";
$subject = "Order Confimation - mywebsite.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: <contact#mywebsite.com>";
$message = "
<!DOCTYPE html>
<html lang='en-us'>
<head>
<meta charset='utf-8'>
<title>Order Confirmation</title>
<style type='text/css'>
//style information
</style>
</head>
<body>
<div class='box'>
<h1 class='right'>Thanks!</h1>
//Blah blah blah
</div>
</body>
</html>
";
mail($to, $subject, $message, $headers);
if (mail($to, $subject, $message, $headers)) {
echo "<p class='hide'>E-mail Sent</p>";
} else {
echo "<p class='hide'>Problem</p>";
}
?>
And it returns E-mail sent however the email never goes to the inbox
Any suggestions?
Also pear package is enabled.

There's nothing wrong with your code as written above (with the exception that you call mail() twice - once outside the error check loop, once inside the 'if' - so you send 2 emails)
A quick test delivered fine to a Google Apps email address, so there's nothing about the code that specifically should be causing problems.
Spam filtering is a cumulative game. There can be multiple small things wrong none of which mark you as spam, but cumulatively tip your score over the limit.
In this case, you're sending a solely HTML email without a text/plain component. This is a negative mark against your score which seems to be acting as the straw to break the camel's back. If you're sending from a shared host with poor reputation you may get a few more points against you, and your PHP mail settings may be pushing an invalid return-path or other origin-based error which also count against.
You could try sending a multipart with both text/plain and text/html per http://krijnhoetmer.nl/stuff/php/html-plain-text-mail/
If that doesn't work, then if you post the full headers of the successful plaintext email then I might be able to see if you have other indicators.
Gmail will accept most mail, but just blackhole it after acceptance, so if you're using that to test you won't get much feedback.
Email's a tricky game, and if you're needing to get delivered for your app's success, you may want to consider using a third party service:
http://SendGrid.com
http://PostageApp.com
http://PostmarkApp.com
These services are designed around sending event-driven email from apps and handling all the mess on the ISP delivery end.
Full Disclosure: I am the Deliverability guy at PostageApp

I noticed you don't have
$headers .= 'To: ';
Could that have something to do with it?

I think you need to include the SMTP settings for outgoing mail if you are using mail function in php. So technically your mail will be reported as sent but would not have gone through the mail provider.
(I know you need to do that in java when using the mail function.)

mail function returns true, so you should say :
// mail() function return 0 if all right, so compare this with 0
if (mail($to, $subject, $message, $headers)) {
echo "E-mail Sent";
} else {
echo "Problem";
}
Not :
if (mail($to, $subject, $message, $headers)==0)

Related

Subject issue with mail function - PHP

I'm trying to send e-mail using PHP. I took a piece of code that I have been using earlier, but it doesn't work anymore. I figure out the problem was coming from the subject.
When my subject is "test", the mail is sent, but when my subject is anything else with for example capital letters, like "Test", it doesn't work. I'm pretty sure this is a quick fix, but I can't find the answer.
Here is my code.
In index.php
$subject= 'test';
$message = implode("\n" , $_SESSION['data']);
mail_html($subject, $message, $email);
In functions.php
function mail_html ( string $subject, string $message, string $receiver) {
$headers = 'From: Template <me#me.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8';
mail($receiver, $subject, $message, $headers);
}
Thank you everyone for helping me. I feel stupid getting blocked by things like that.
Sending E-Mails is a very difficult process. So you can't send E-Mails so easy from your Computer or Server if you don't have alle the common SPAM prevention mechanisms enabled.
So when you don't a Reverse-DNS entry and you send an Email with "Test" you should not wonder if the emails can't send to the other site. So Take a look at the SPF-System, Reverse-DNS and generic names for servers (not working for some providers). Then you can send some E-Mails.
To find some problems check your mail log /var/log/mail.log.
When you try to prevent that problems use an SMTP account from your provider and send that E-Mails over SMTP.

HTML Tags are not rendering in the Gmail - Using PHP language

I have used used the PHP Language for Sending the email and HTML tags are not rendering in the gmail account sometimes.I have used Content Type as - charset="iso-8859-1" I have attached the image.
And also am receiving the Message ID, which should not be come in the mail.
I don't recommend/use php built in mail() function to send and receive emails. Use php open source libraries like PHPMailer and SwiftMailer. I have been using PHPMailer after facing many issues when using mail() alone. Very easy to implement and has lots of features. You don't have to spend your valuable time for email sending related development.
http://phpmailer.worxware.com/
http://swiftmailer.org/
If the HTML isn't rendering, that usually means the headers weren't set properly.
Try this:
<?php
//change this to your email.
$to = "m#m.com";
$from = "m#m.com";
$subject = "This is HTML email";
$message = "
<b>htmlemail....</b> <br>
<a href='http://www.google.com'>Google</a>";
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: $from\r\n";
mail($to, $subject, $message, $headers);
echo "Message sent..";
?>
From http://www.webhostingtalk.com/showthread.php?t=416467

Cannot use variable $_SERVER['HTTP_HOST'] in php mail()

I tested this:
<?php
$to = "recipient#example.com";
$subject = "Hi!";
$server = $_SERVER['HTTP_HOST'];
$body = "From: ". $server. "<br>";
$body .= "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
This will never send emails to me although the feedback "successfully" is displayed.
The code will work (email actually sent) however if I removed the inclusion of
$server = $_SERVER['HTTP_HOST'];
in the email body.
Very weird, it doesn't make sense ?
This is just a PHP page. I call this page from a browser !! Please try ...
UPDATE!! okay, Instead of using $_SERVER['HTTP_HOST'], I use a string "user.server.com" directly. AND, it did not work !!
But, when I modified a string a bit, like "user.server.com.us", it works !!
So basically, the mail server filler its own reference to its domain, not sure why its doing this...
Some spam servers grade your message as 'spammy' for urls in the body. Unfortunately you'll be hard-pressed to find a more specific error message with mail() unless you have access to the sender and recipient's mail server logs. Give SwiftMailer or another PHP Mailing library for better support.
When using mail() function you are required to specify 'From' header. Please refer to this documentation page, look for 'additional_headers' parameter's Notes section.
So your mail() call will look like this:
mail($to, $subject, $body, 'From: some.guy#example.com');
Of course you don't receive it because there these days all major webmails use anti spam filters and probably your message is detected as a spam.
The solution is to specify exactly your email for $from variable, if you don't specify your real email your message is detected as spam.

How to test if email is send (or received)?

below is a part of a website where orders are sent to a small a computer that prints the email out directly on a receiptprinter.
The problem is that sometimes (1 in 50 times) the email is never printet, even though you are send to the kvittering.php page.
Any ideas of how I can make a test, that only forwards you to the kvittering.php if the email is 100% sent?
Then I can eliminate the website as a source of error and focus on the printer.
Any advice is welcome.
<?php
$headers = "From: www.testsite.dk \r\n";
$headers.= "Content-Type: text/plain; charset=iso-8859-1 ";
$headers .= "Content-Transfer-Encoding: 8bit ";
$body = "$name_field\nTel. $phone_field\n$email_field \nVil gerne bestille følgende:$menucard_to\nMed følgende ændringer\n $message";
header("Location: $redirect_field");
mail($to, $subject, $body, $headers);
?>
<script>
<!--
window.location= "kvittering.php"
//-->
</script>
orders are sent to a small a computer that prints the email out directly on a recieptprinter.
I'd strongly suggest you to choose one of hundreds more convenient and secure protocols, such as FTP, SSH XML-RPC, HTTP or any other.
If you can't you have to:
study the particular mailserver serviong this "small a computer"
obtain one of hundreds PHP SMTP scripts interacting with SMTP manually and use it instead of mail().
study it's return values and check them with your sending code
Probably, you can try embedding 1px image in e-mail and point its path to a PHP script which should trace opening an email (which means successful delivery) and deliver 1px blank PNG. You can find information on such READTAG on the net.
Hope it will help you.
Use PHPMailer class at first and connect to SMTP server some how. Maybe you can get an access to same SMTP where the printer is connected. And move header() function after mail() function.
EDIT: Here is the article about redirects.
The most basic way to check if the e-mail has been sent is just:
<?php
$headers = "From: www.testsite.dk\r\n";
$headers.= "Content-Type: text/plain; charset=iso-8859-1\r\n";
$headers.= "Content-Transfer-Encoding: 8bit";
$body = "$name_field\nTel. $phone_field\n$email_field \nVil gerne bestille følgende:$menucard_to\nMed følgende ændringer\n $message";
if(mail($to,$subject,$body,$headers))
{
header("Location: $redirect_field");
echo'
<script>
<!--
window.location= "kvittering.php"
//-->
</script>';
}
else
{
echo'Error...';
}
?>

Odd behaviour with links in PHP Mail function

I'm currently trying to get links working in emails sent via the PHP mail function. Here is my code - I've also included some things I've tried (commented out along with notes):
$to = "test#testing.com";
$subject = "Testing email";
//$body = '<strong>This is strong text</strong>'; <-- Works
//and the text is correctly emphasised.
//$body = 'Link Test'; <-- Works
//but without http:// at the start makes the link relative to the server root
//$body = "<a href='http://www.yahoo.com'>Link Test</a>"; <-- Does not work
//$body = "Link Test"; <-- Does not work
$body = 'Link Test'; //<-- Does not work
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Steven Parler <". $to . ">\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
As can be seen html is working without links, and links also work providing I don't include "http://" in the link itself. If I do include the "http://" then no email is received / sent at all (I'm unsure which as the mail() command returns true to say it was sent).
I'm not really sure why this isn't working. Could it be some setting that needs changing on my webhost's server? I'm on windows shared hosting.
Thanks in advance for any advice anyone can give me - been pulling my hair out over this lol. :)
Don't build MIME messages by hand. It's just too painful and fragile. Use something like PHPMailer or SwiftMailer to do it for your automatically. You provide the HTML, they'll provide the appropriate headers
I completely agree with #Marc B.
Here are a couple more options: XPertMailer, Zend_Mail, Rmail, HTML MIME MAIL
I've worked with all of these but since I'm working mostly with the Zend Framework I'm lately using Zend_Mail.
Having said that, your hosting provider might be blocking your emails because they might think it's SPAM. Try generating valid html markup that passes validation and see if that helps.
That's very odd behaviour, I'd expect the last 2 to work perfectly. I tried this out on my server, and mail($email, $subject, $body, $headers); worked perfectly with a $body of
text text text: \n http://website.com.
Perhaps it's a setting somewhere? If you're using awardspace, they require a from header to be used, and the mail sent from an e-mail registered with them. Other hosts might have a similar procedure.
Thanks for all the responses. I finally got around it by using the SMTP feature of Swiftmailer which correctly created the email with links.
Out of interest I tried the code I used in my original post again... but this time I used a random web address (http://www.trustedreviews.com). The email arrived... I then tried a lot of other web addresses - google.com, hotmail, yahoo.co.uk etc and they all arrived. Changed it back to (http://www.yahoo.com) and lo and behold the message did not arrive again. So it seems out of the millions of web URL's there are I chose the one that my webhost has decided to block lol...
That said the yahoo link does arrive ok using the smtp function of swift mail; it's only with the php mail function it doesn't seem to work. I guess I'll contact them and ask why it's blocked. I'll most likely stick with using the smtp method as at least it seems like it bypasses any restrictions.
Thanks again for everyone's time and help :)

Categories