Sohil Desai
Sohil Desai

Reputation: 2988

php send mail from localhost

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

<?php

    $to      = '[email protected]';
    $subject = 'The subject';
    $message = 'hello';
    $headers = 'From: [email protected]' . "\r\n" .
        'Reply-To: [email protected]' . "\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 = [email protected]

& in sendmail.ini

# A freemail service example
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from [email protected]
auth on
user [email protected]
password xxxxxxxxx

# Set a default account
account default : Gmail

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

Upvotes: 15

Views: 70457

Answers (7)

wieljer
wieljer

Reputation: 1

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

Upvotes: 0

Mani Kandan
Mani Kandan

Reputation: 31

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

Upvotes: 0

Mittal Patel
Mittal Patel

Reputation: 806

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.

Upvotes: 3

Manish Chauhan
Manish Chauhan

Reputation: 570

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");

Upvotes: 0

mram888
mram888

Reputation: 5139

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.

Upvotes: 4

Tomasz Banasiak
Tomasz Banasiak

Reputation: 1560

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.

Upvotes: 0

K Cloud
K Cloud

Reputation: 271

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.

Upvotes: 1

Related Questions