Reputation: 447
I have switched my old project to a new server in which i am using mail to sent mails sendmail is my mail server I am using php mail() function but it is not working properly
The php code which i am testing is this.
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "[email protected]";
$to = "[email protected]";
$subject = "PHP Mail Test script";
$message = "This is a test to check the PHP Mail functionality";
$headers = "From:" . $from;
$val = mail($to,$subject,$message, $headers);
echo "Test email sents";
echo $val;
?>
The mail is getting recived but it is getting deliverd from another address which is like this.
[email protected]
And the mail log is this
Oct 23 18:43:51 xyz2 sendmail[14831]: w9NIhp9R014831: from=www-data, size=177, class=0, nrcpts=1, msgid=<[email protected]>, relay=www-data@localhost
Oct 23 18:43:51 xyz2 sm-mta[14832]: w9NIhpIF014832: from=<[email protected]>, size=396, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=www.xyz.com [127.0.0.1]
Oct 23 18:43:51 xyz2 sendmail[14831]: w9NIhp9R014831: [email protected], ctladdr=www-data (33/33), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30177, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (w9NIhpIF014832 Message accepted for delivery)
Oct 23 18:43:53 xyz2 sm-mta[14835]: w9NIhpIF014832: to=<[email protected]>, ctladdr=<[email protected]> (33/33), delay=00:00:02, xdelay=00:00:02, mailer=esmtp, pri=120396, relay=mail.mailinator.com. [23.239.11.30], dsn=2.0.0, stat=Sent (Ok)
If i use gmail id the mail is not at all getting delivered not even in spam box
What can i do here?
Upvotes: 0
Views: 863
Reputation: 846
To answer your question...
It looks like you are missing some headers I used on a similar project a while ago. Try replacing your $header line with the following:
$message = wordwrap($message , 70);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
Now, a word of advice...
I struggled with the native PHP mail() function for a loooong time while stubbornly resisting other options. Believe me, you will save yourself a lot of time and trouble using something like PHPMailer. When I finally gave in and implemented it (which was way easier than I imagined), all of the problems I was fighting through with mail() vanished. Unless you are willing to write your own complex mailing libraries, please consider PHPMailer or a similar tool.
Upvotes: 1