We have a confirmation script for our users, which uses the PHP mail() function. If I enter my own address (GMail or Yahoo) everything works perfectly. If however I enter the client's address, the function returns 1 but the email never reaches its destination. We checked spam but the email is not there either.
This is the PHP script I prepared:
$msg = "Body of the message";
$headers = "From: OUR TEAM <team#our_email.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$sendmail = mail('some#address.com', 'Trial email (testing...)', $msg, $headers);
echo ("Sent mail = ") . $sendmail;
The return value is always 1.
Is there a way around this?
Thanks.
Related
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))
I have a problem with the dynamic sending of emails from my site
Customers who have hotmail, live emails do not receive emails (which are in bcc receive emails) as well as some customers with gmail for example receive them but in spam and others in main. The code I use
function emailDemande($email, $nameL){
if (!preg_match("#^[a-z0-9._-]+#(hotmail|live|msn).[a-z]{2,4}$#", $email))
{
$passage_ligne = "\r\n";
}
else
{
$passage_ligne = "\n";
}
$subject = 'Votre demande d\'expérience mystère';
$headers = "From: site <contact#site.com>".$passage_ligne;
$headers .= "Bcc: compte#gmail.com".$passage_ligne;
$headers .= "MIME-Version: 1.0".$passage_ligne;
$headers .= "Content-Type: text/html; charset=UTF-8".$passage_ligne;
$message = '<html><body><p>Cher(e) '.$nameL.',</p></body></html>';mail($email, $subject, $message, $headers);};
I assume you are using Cpanel hosting.
What you want to do is.
Go to Cpanel > MX entry > Select Domain name in question > Select Remote Mail Exchanger.
Thats it.
My website runs the following code: (I BCC myself so that I have a copy of all the emails that my website sends out)
//prepare email headers
$headers = "From: " . "info#mysite.com" . "\r\n";
$headers .= "Reply-To: ". "info#mysite.com" . "\r\n";
$headers .= 'Bcc: sent#mysite.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = getMsg( ... );
mail( $buyer_email, 'mysite.com - Verify your information.', $message, $headers );
$message = getMsg( ... );
mail( $seller_email, 'mysite.com - Verify your information.', $message, $headers );
The emails get sent out perfectly fine. The problem is with the second email that gets BCC'ed to me. The recipient's email address is blank so I can't see who the email was sent to. The first email that's BCC'ed to me is fine, all the info shows up. In other words, I can see $buyer_email, but I can't see $seller_email. Any ideas?
You can debug it like this
echo "Seller Email: $seller_email";
mail( $seller_email, 'mysite.com - Verify your information.', $message, $headers )
The page will print the seller email and you can see what it actually is.
Addition
If you can not use the above code because you have to test it a user (which is normal btw), use the following technique.
Since you are getting the first email, send $seller_email as part of test code in that email and see what value it has.
$message = getMsg( ... );
mail( $buyer_email, "mysite.com - Test Seller Email: $seller_email .", $message, $headers );
You will find out the seller email value in the email you get.
Does sending additional headers help? (see mail()) That way you don't have to use 2 mail functions.
Like this:
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
I'm very sorry, but I made a huge mistake. Long story, but basically I was confusing code on one page with very similar code on a different page. I was testing the validity of $seller_email on the wrong page. On the page in question, it was in fact NOT being set. Once again, sorry. I should have posted the entire code.
Using the php mail() function to send out emails when a user requests a login.
It was working fine last week, emails were being received by all my coworkers who share the same mail server for our company. Now however, the emails are not being received by that mail server, but received on others (comcast.net, uservoice.com, gmail.com, etc.) just fine.
No error from the php mail() function so the emails are being sent, just for whatever reason they are all of a sudden blocked by our mail server.
No settings have been changed to the php scripts or the mail server.
Any ideas??? I have tried everything!
<?PHP
$timestamp = date('Y-m-d H:i:s');
$to = 'james.hickman#MYCOMPANY.com';
$from = 'support#MYCOMPANY.uservoice.com';
$subject = 'Admin Test';
$message = 'Just a simple test message! - '.$timestamp;
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "Organization: MYCOMPANY Support\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
if(mail($to, $subject, $message, $headers, "-f ".$from))
echo "Success!";
else
echo "Failed";
?>
If you are able to send mail out to other services and just not your own server then the problem is not with php's mail function. Php's mail function just sends out mail and you yourself said that u are able to send mails to other hosts. So the problem is not as devious as it sounds. Try and check if your server is out of disk-space.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: How to avoid a system generated e-mail going into spam?
How to send 100.000 emails weekly?
I am using the following script to send mail
$to = 'name#test.com' . ', ';
$to .= 'name2#test.com';
$subject = 'Green apple';
$message = 'Enquiry posted by test ';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Green Apple <info#greenappleme.com>' . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo "mail send successfully";
}
else
echo "mail can't send";
When I use this script to some servers, the mail is going to spam. But in some servers, it is going to the Inbox as desired.
How can I prevent email going to spam?
To prevent an email going into spam, don't send with mail from PHP. Send it with SMTP from your server, you can use PHP to connect to SMTP and submit the message. You will then need to set the SPF records on your server, and reverse DNS records with whoever your IP address comes from. If you do these three things then you'll be on the whitelist and all your emails will go into the inbox everywhere, assuming you don't abuse the privilege and get put on a blacklist.
So: send using SMTP, research SPF records and reverse DNS.
You won't be able to do this unless you have a dedicated server with dedicated IP for the domain from which you are sending the email from.
Be sure to set the RETURN PATH as the optional 5th parameter to the mail() function.
if (mail($to, $subject, $message, $headers, '-finfo#greenappleme.com'))
TO send ur majority mail in inbox set below headers and dont use test like keywords in your subject line
$headers = "From: My site<noreply#example.com>\r\n";
$headers .= "Reply-To: info#example.com\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";