15

I am new at php. I was trying to send mail from php using this code.

<?php

    $to      = 'sohil@gmail.com';
    $subject = 'The subject';
    $message = 'hello';
    $headers = 'From: sohil@yahoo.in' . "\r\n" .
        'Reply-To: receiver@yahoo.in' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

?>

I have change settings in php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = sohil@gmail.com

& in sendmail.ini

# A freemail service example
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from sohil@gmail.com
auth on
user sohil@gmail.com
password xxxxxxxxx

# Set a default account
account default : Gmail

Now code runs successfully but I am not getting any mail.

Sohil Desai
  • 2,940
  • 5
  • 23
  • 35

7 Answers7

4

You must change the php.ini file:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

It won't work if localhost is set, for that reason change to your mail server.

mram888
  • 4,899
  • 5
  • 33
  • 59
3

You won't have SMTP server installed by default so you can't send emails from localhost directly. Either you can set up SMTP server on local or use third party SMTP servers. Have a look at http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp which gives you insight about how to send mail from localhost using third party SMTP server.

Mittal Patel
  • 806
  • 6
  • 8
1

The function will not work on your localhost, as the locahost doesn't works as a SMTP server, upload your content to a valid server with SMTP installed, and then go for the mail call.

K Cloud
  • 271
  • 1
  • 5
  • 15
0

Your server does not have local mailserver.

There are few solutions:

  • Install local mail server if you have sufficient rights
  • Change your PHP settings to use other mail server (other open mailserver or auth-based ones like Gmail, Yahoo etc)
  • Use one of available mail libraries which supports IMAP / POP3 to handle mail sending. SwiftMailer or Pear Mail are one of most commonly used.
Tomasz Banasiak
  • 1,540
  • 2
  • 13
  • 19
0

Try to set below things in your php.ini,

  1. "SMTP" to "mail.YourDomain.com"
  2. "smtp_port" to "25"

OR you can set this option using php script also,

// Please specify your Mail Server oR other mail server which you are going to use.(e.g gmail, yahoo)

ini_set("SMTP","mail.YourDomain.com");

// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.

ini_set("smtp_port","25");
Manish Chauhan
  • 595
  • 2
  • 7
  • 14
0

Here's the link that gives me the answer:

Install the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip

Modify the php.ini file to use it (commented out the other lines):

mail function

For Win32 only.

SMTP = smtp.gmail.com

smtp_port = 25

For Win32 only.

sendmail_from = <e-mail username>@gmail.com

For Unix only. You may supply arguments as well

(default: "sendmail -t -i").

sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

(ignore the "Unix only" bit, since we actually are using sendmail)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

sendmail

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com
0

I had this issue the past weeks, on my centos boxes, sharing this for others also having issues with mail() in php not sending... This resolved the issue for all of my mail() php scripts.

// Enable the sendmail in selinux
setsebool -P httpd_can_sendmail 1

// Add the following to /etc/postfix/main.cf
relayhost = smtp.server.com

// Then from command line
service postfix restart