php mail function goes straight to junk folder [duplicate] - php

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");

Related

How Do I Include PHP Strings In The Message Of The Mail() Function?

I'm trying to figure out how to include custom PHP strings in the message of the PHP mail function.
I have a user registration area on my website. Once a user signs up, I want to email them a success message, such as "Welcome to The site...." I want to include their name in the email, so I would need to pull the value from the field where they entered their name. I will be pulling over field values as well, but this is just an example.
I have:
$name = $conn->real_escape_string($_POST['name']);
$email = 'myemail#gmail.com'
$subject = 'Welcome!'
$message = '<HTML><body><p>Thank you for signing up' .$name. 'We are so glad that you are here</body></html>';
mail($email,$subject,$message);
What am I doing wrong?
add header in your mail like this
$headers = "From: myemail#gmail.com\r\n";
$headers .= "CC: myemail#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $message, $headers);

Send an email templating during a include in PHP

I try to send a email with templates. I have severals templates (one to reset password, one to upgrade the account, ...).
All of these templates are stored in a mail directory and it looks like this:
<?php
// Template to reset password
$message = "Hello Mr, ...";
$subject = "Your new password";
$email = "mr#company.com";
?>
Now I have a function:
function sendMail($template, $USR_Id) {
ob_start();
include 'mail/'.$template.'.php';
$message = ob_get_contents();
ob_end_clean();
// Start configuring the email
$headers .= 'From: company <noreply#company.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $message, $headers);
}
My problem:
How to get back the infos store in my template files (i.e.: $subject and $email) ?
Good to see you using my previously suggested setup. That setup depended on the echo however, and if you're not using that you can make your function even simpler. If your templates look like that; you can simply do:
function sendMail($template, $USR_Id) {
include 'mail/'.$template.'.php';
// Start configuring the email
$headers .= 'From: company <noreply#company.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $message, $headers);
}
If you use include / require; you can use all variables across multiple files

PHP Emails are sending, but headers are not

I'm using several different mailers for a single website, because right now, none of them are working correctly. And they're all malfunctioning in the exact same way, ,so I don't believe that it's any of the mail functions, but I'd like another set of eyes on this to make sure I'm not missing something.
Here's what's happening:
The emails send just fine, but it looks like none of the header info I set is being used. When you get an email, the html is displaying as code, and the subject shows as part of the message.
Here's the header info:
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine
Here's the Send:
if ($test == true) {
mail($to, $from, $subject, $message, $headers);
echo "<script>window.location = '/key-holders/index.php';</script>";
}
And here is what the email looks like:
<html><body><center><table bgcolor='#ededed' width='680px'><tr><td><img src='http://example.com/img/logo.png' /></td></tr><tr><td><p>User Name has left you a special message on The Website.<br /> Please go to <a href='http://example.com/message/5493536206ab2'>http://example.com/message/5493536206ab2</a> to view your message.</p></td></tr></table></center></body></html>
User Name Has a Special Message For You
Edit - Here's $newLine:
$newLine = "\r\n";
Remove $from parameter from mail function:
mail($to, $subject, $message, $headers);
Make sure, that $newLine = "\r\n";

PHP mail can not send when there is a url a tag in the body

When I added "a href tag " in the mail body the mail is not sent.
If I remove this 'a href and www' tag, the mail sends and all other content display as per my requirement.
I don't know where is the exact problem, I'm using GoDaddy hosting with PHP 5.3 version.
If anyone has a better solutions please share with me .
<?php
// multiple recipients
$to = 'ali.dzinemedia#gmail.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '<a href=www.google.com>Click here</a>';
// To send HTML `enter code here`mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
// Additional headers
$headers .= 'To: Mary <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
echo "To : ".$to;
// Mail it
mail($to, $subject, $message, $headers);
Use this as the header:
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
that's if you want to use HTML in the body, but you have to create well formatted HTML, you know with all of its tags: html, head, body, and close them all.
<html>
<head></head>
<body>Content here and this is a link</body>
</html>
I had the same problem once, turned out my spam filter blocked the mail when a link was in it and let it trough when I removed the link.
Took me some time to notice that

How to add custom header to PHP mail?

I have a mail function with the standard parameters:
mail($to, $subject, $message, $headers);
Where:
$headers = "From: admin#mysite.org";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
Now, I'm trying to get the sent e-mails to show 'My Site' as the sender instead of 'admin#mysite.org' but if someone responds to the mail it still needs to go to admin#mysite.org.
I've tried putting "My Site" in the "From" field before and after the e-mail address but it just appends to the address. I've tried appending it to the $headers variable but that messes the whole e-mail up. I've also tried adding it this way:
$headers .= "\r\nMy Site";
but that doesn't seem to do anything.
I know this can't be complicated as I've seen it done a hundred times, but I can't seem to find a straight-forward answer for this - how do I 'mask' the admin e-mail with the site name but still keep it as the address any response goes to?
Change your From: header to "From: My Site <admin#mysite.org>"
$headers = "From: My Site <admin#mysite.org>";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
From the PHP Docs.
There is the "From: something <something#something.com>"; header, and then there is also the optional fifth parameter of mail() which allows you to override the from setting that may or may not be set in php.ini.
<?php
$to = 'name#domain.com.au';
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From: Name <noreply#domain.info>" . "\r\n";
$fifthp = '-f noreply#domain.info';
mail($to, $subject, $message, $header,$fifthp);
?>

Categories