why the mail function not working - php

Why the mail function not sending mail to me ,Could any one please help me ?
Here i have wrote script to mail sending.
Is there any other settings to send mail.
But the mail function printing always "Success" , but the mail not received to me.
$Msg = "Your Suggestion is saved successfully. Please wait for admin approval";
$subject="New Suggestion Posted - Waiting for approval";
//$email_id = 'bajrang.lal#sunarctechnologies.com';
$email_id = 'bajrang.lal#sunarctechnologies.com';
$mail_msg="Hello Admin,<br><br>New Suggestion Posted. Waiting for approval.<br><br>
Suggestion : ".$frmdata['description']."<br><br> Click here for <a href='http://www.tatanykshipping.com/index.php'>login</a><br><br> Thanks,<br>The TATA NYK Team";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Tata NYK <".$_SESSION['EMAIL_ID'].">\r\n";
$headers .= "Organization: Tata NYK\r\n";
echo '<br>sending mail to = ',$email_id;
if(mail($email_id,$subject,$mail_msg,$headers))
echo '<br>Successs';
else
echo '<br>Fail';
Thanks a lot in advance.

If your PHP application is running on Linux and expecting to send mail from that system, you need a transport. I use sendmail, but there are other open source transports you can use. You may also have to install an email client, but I do not know how PHP interacts with sending mail. My applications act like an email client, and need the email client installed.
You also need to make sure that the email recipient will accept unsolicited port 25 traffic. Because of spam-ware, our firewall won't accept unsolicited port 25 traffic, so I have doctored up sendmail to log into our email server as a legitimate email account.

Related

Mail ends up in junk folder - PHP contact form [duplicate]

I wrote a PHP script to send emails.
My script is like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: abc#yahoo.com' . "\r\n";
// Email Variables
$toUser = "someone#yahoo.com"; // recipient
$subject = "testing"; // subject
$body = "<html><body><p>
Example of including an image via html \<img\> tag:
<br>
<img src='../images/profile.jpg'>
<br>
My new picture
<br></p></body></html>"; // content
if (mail($toUser,$subject,$body,$headers)) {
echo "sent";
} else {
echo "failed";
}
Well, of course I use a valid email address for sender and receiver. I did receive the email, but it goes to junk mail. So I went for google research. Is it because of my "header" script problem? If it isn't, then what could cause my script to send a junk mail? Any solution?
Please try this:
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
Perhaps the problem is that yahoo uses domainkeys verification, which will likely fail for your application given that the mail is not actually coming from yahoo's servers.
When I've once had a similar problem I looked at the headers and found out that my host uses SpamAssassin. So I googled for 'SpamAssassin score' and found a multitude of information on how to incorrectly (and thus correctly) form an email.
For example: SpamAssassin score list
1. Check mail content
As others have hinted it is probably marked as spam because your mail looks like spam.
I am not sure if you the script that you have posted is the actual one that you are testing.
If it has the actual mail body & headers, then running this message through a standard installation of SpamAssassin gives it a spam score of 4.9
X-Spam-Status: No, score=4.9 required=5.0 tests=BAYES_50,HTML_IMAGE_ONLY_04,
HTML_MESSAGE,MIME_HTML_ONLY,NO_DNS_FOR_FROM,NO_RELAYS autolearn=no
version=3.2.5
Since the email body has only HTML it has a greater chance of being handled with suspect by most anti-spam solutions.
2. Mail server's IP
Another aspect worth checking will be the IP address of your mail server. Any mail originating from dynamic IP addresses will potentially be considered as SPAM.
3. Blocklists
Also check if your IP address is listed in one of the block lists. To start with please check your IP address with http://www.spamhaus.org/lookup.lasso.
Use mxtoolbox.com to check the servers IP to be blacklisted or not. As well this website can help you with a couple of email related checks.
Of course there are a long list of checks running inside spam filters. As already suggested, check the email headers for details about the spam filters rating of the spam email.
Hope that helps!
**This Works Perfectly fine for me**
$to="reciever#reciever.com";
$subject="This is Your Message";
$from = 'Sender <noreply#sender.com>';
$body='Hi '.$name.', <br/><br>Now You can See Yor main in inbox';
$headers = "From: " .($from) . "\r\n";
$headers .= "Reply-To: ".($from) . "\r\n";
$headers .= "Return-Path: ".($from) . "\r\n";;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($to,$subject,$body,$headers);
I was having the same problem:
The problem is that when you specify content-type before the "From:" part , the mail comes as a spam.
But if you specify "From:" before the content part it comes as a normal mail and makes you smile and curious.
As schnalle said, one problem surely is that the smtp server that you use to send the email and the one thet you specify as From, is different.. the from's domain whould be the same that the server youre running on.
So, you can use the yahoo server to send the email (check if they allow the smtp remote connection, but i guess they do) connecting by smtp, and this will solve 1 problem.
Another one is the html contents without the alternative plain text contents, but, this one is less important.
I suggest you phpMailer, free and open-source php class to send email, easly to use (i use it event o send mail through gmail server)
On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
Make sure you send your e-mails from an existing account on your server/domain.
Make sure you have the reply-to address in your header.
These are the basic things you can try.
if your website domain is mydomain.com then in From headers make sure to use someone#mydomain.com
Remove the Content-type: text/html and add $headers .= "X-Priority: 2\nX-MSmail-Priority: high"; to get rid of Spam. This method has been tried and tested.
the problem is, the server you're sending the mail from is not a yahoo server. most spam filters check if they match, otherwise it would (and is - or was) possible to easily fake the sender. ever wondered why you get spam from bill.gates AT microsoft.com or your own mail address?
You've got two solutions:
use Yahoo's SMTP using abc#yahoo.com credentials to send mail from abc#yahoo.com;
use other from, with your own domain;
You can try the mail class and test file that I have created here. I have tested the files and can send emails to my hotmail and gmail under a different mail name. The main reason why the emails are mark as junk is because the structure (both header and message) is not correctly done. In most cases, it is the line feed that is causing the problem.
I can use it to send mail with attachments to Gmail. However, the attachments dont work for hotmail. Hope this helps =)
You can check the files here..

Moved to a dedicated server - automated web emails not sending?

I recently moved over to a dedicated server. I changed the nameservers on my domain and the new website is now working fantastically. However, automatic emails such as the newly registered confirmation email members are supposed to receive, etc. don't seem to be sending. Why might this be?
I know there are likely to be a wide range of potential causes but if anyone has any suggestions it'd be fantastic to hear them! I used Squirrel Mail to check a couple of the email accounts and both sending and receiving on the new dedicated server seems to be fine.
Thanks in advance for any suggestions! :-)
Stop php mail() being treated as spam mail
You must to add a needle headers:
Sample code :
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>

PHP mail may be filtered out by spam?

I am creating a small company, and would like to send out emails to my clients once they have signed up for my service to activate their accounts. I am currently using PHP's mail() function, however I am worried that my emails are being filtered out by spam filters. Is there a better way to go about this?
$email = 'XZY Client Email address # somedomain.com';
$emailSubject = "Welcome to XYZ Service!";
$to = $email;
$subject .= "".$emailSubject."";
$headers .= "From: no-reply#XYZService.com\r\n" .
"X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= "Welcome to XYZ Service! \n Activate your account by clicking the following link: link...";
mail($to, $subject, $message, $headers);
Is there a way to authenticate these emails so that my clients know that they are from my actual service? Thank you in advance!
You can try to get your mail server white listed by some of the major postmasters (AOL, Gmail, Hotmail, etc.).
I am not sure if this is what you want, but you can setup a mail server to handle all your emails.
Try using that mail server for sending emails and see if they are still filtered or not.
BTW, setting up a mail server on *nix systems is free and worth giving a try.
I have a simple arcade site that people from my highschool use and for quite a while I've been using the method that you showed. I didn't look at my code to verify that they're identical, but they look fairly close and mine works with gmail and all other big name services I've ran into!
Just remember that some hosting companies limit the amount of PHP mail that can be sent per minute. Mine only allows 9 per minute, I know I've gotten suspended a few times on accident while running test mail sends, haha.

Send mail through mail relay with PHP

I ran into a wee issue when using mail(). I wasn't able to send to addresses off the domain I was hosting the form. I understand this is for security reasons but it makes the creation of a 'send to friend' system a little tough.
Here's what I had working (albeit it only sent to my address):
<?php
$senderName = $_POST['name'];
$friendsEmail = $_POST['friendsEmail'];
if ($_POST['formName'] == 'refer') {
$to = $friendsEmail;
$subject = "$senderName has referred you";
$message = "Message goes here";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: Me <no-reply#test.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
header("Location: referConfirm.html");
?>
I've talked to my hosting company who setup a mail relay (yay!). Trouble is, I have no idea how to get what I working above through a mail relay. I have the following details:
IP: 000.000.000.000
Domain: domain.company.com
UN: username
PW: password
(Details are dummy.)
Can anyone give me a clue?
Thanks,
#rrfive
mail() uses the smtp/sendmail settings found in php.ini. If you need to send it via another smtp, or one with authentication (like in your example) mail is simply not enough.
There are good mailer libraries out there, just to name a few:
Swift Mailer
Zend_Mail
PHPMailer
They are all capable of sending emails via an authenticated smtp server.

Handling mail failures using PHP?

If I send an email through GMail to this address nkhkhlkhlkjlkjkljlkjlk#gmail.com
I got error like:
Delivery to the following recipient failed permanently
My question is, if I send using the PHP mail function, how can I catch bounce emails?
My code:
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
$to = "nkhkhlkhlkjlkjkljlkjlk#gmail.com";
$subject = "Testing";
$message = "Testing body";
mail($to, $subject, $message, $headers);
You can't detect such errors just by using mail(). You have to specify a valid return address and then connect to that mailbox (via IMAP or POP, there are functions for that) and check if any message bounced. Bear in mind that bounces may take a long time (even hours, due to transient errors) so you have to do this check asynchronously.
you could get the message by mail when you use this:
$bounce = 'test#domian.com';
mail($to, $subject, $message, $headers,"-f $bounce");
This will send all bounce back mails to your address
You could use PEAR MAil, it contains a parseAddressList() method which checks if the server of an email address accepts the email address. It also returns useful error messages.
The PHP mail() function does have an error status as the function return value, but it won't pick up this kind of error because it only checks that the email has been sent, not that it has been received.
(this is because it needs to return control back to the program immediately; it can't wait for a possible bounce message because they can take a long time to come back - you wouldn't want your program to sit and wait for five days just in case the email bounced, would you?)
Therefore, the only way to determine whether a message has bounced is to have a separate program which checks the mailbox which the bounces would be sent to.
You can specify the mailbox for bounces using the -f option in the mail options string (see the PHP mail() function man page for more info on this).
Then have a separate program periodically check that mailbox for bounce messages, and report them to you as required. (there are a number of PHP libraries that allow you to check a mailbox; Google will help here, or look in PEAR)
Determining which email it was will depend on the quality of the bounce message. You should definitely be able to see the intended recipient's email address, but you may not be able to tell which email it was, ie to match it up to a specific instance of when you called the mail() function in the first place.
Hope that helps.
You should login via IMAP or POP3 to your inbox and parse the email address and delivery error.
It's also worth mentioning that you can specify (while sending) custom headers to better track the message source, I believe MailChimp and others do this do track campaigns, etc...
PHP does not know anything about the mail after it got out of the mail function, so no way to do it at the same time as you send the mail.

Categories