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";
Related
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
I am using html email templates to send mail with image. and when i forward it to another mail then html template format is broken. I am using html templates in the following manner -
$message = file_get_contents('templates_folder/templatename.html');
mail($to, $from, $subject, $message);
and the image is defined in html template format is as follows -
<img src="urlofimage" />
Thanks in advance!
You should build your headers for mail to make it view like a good template, else you might see only your code
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To:' . "\r\n";
$headers .= 'From: Admin<youremail#email.com>' . "\r\n";
mail($to, $subject, $message, $headers);
Note :
Also make sure that the image you include should be available to public and it should not be your localhost image.
You can also look this example
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");
The one email From:support#lead.com works fine. But when changed to john.doe#lead.com it doesn't work? See below.
Both are valid email addresses (fictitious for this example) in the domain from which they are sent.
I have the question into Netfirms support too. I expect to move to phpmailer or API to a ESP (e.g., mailchimp) account soon, but this is just bugging me that the little change breaks the email function.
Code:
Works:
$headers = "Mime-Version: 1.0\n";
$headers .= "Content-Type: text/html;charset=UTF-8\n";
$headers .= 'From: support#lead.com' . "\r\n";
$headers .= 'Bcc: bcc#lead.com' . "\r\n";
// $headers .= 'Return-Path: bcc#lead.com' . "\r\n";
mb_internal_encoding("UTF-8");
if (!mail($to, $subject, $body, $headers, "-f bcc#mylead.com")) echo ("Message delivery failed");
Doesn't Work: (only changed support to john.doe):
$headers = "Mime-Version: 1.0\n";
$headers .= "Content-Type: text/html;charset=UTF-8\n";
$headers .= 'From: john.doe#lead.com' . "\r\n";
// $headers .= 'Return-Path: bcc#lead.com' . "\r\n";
mb_internal_encoding("UTF-8");
if (!mail($to, $subject, $body, $headers, "-f bcc#lead.com")) echo ("Message delivery failed");
Don't know how this really should matter, but you're mixing \n and \r\n in the headers, which may confuse your mail server... Would you mind to try this out?
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);
?>