Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php send mail from localhost

Tags:

php

email

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.

like image 933
Sohil Desai Avatar asked Aug 17 '13 10:08

Sohil Desai


People also ask

Can we send mail from localhost?

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost. For example, you can configure C:\xampp\php\php. ini and c:\xampp\sendmail\sendmail.

Can PHP send email?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );


2 Answers

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.

like image 159
mram888 Avatar answered Sep 24 '22 18:09

mram888


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.

like image 23
Mittal Patel Avatar answered Sep 22 '22 18:09

Mittal Patel