Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send email from Apache server by PHP script

Tags:

php

email

apache

I have installed Apache and PHP on my Windows 7 PC. I learning the PHP now. Following is my PHP script to send email.

<?php
    if(isset($_REQUEST['email']))
    {
        $email = $_REQUEST['email'];
        $subject = $_REQUEST['subject'];
        $message = $_REQUEST['message'];
        mail("[email protected]","$subject","$message","From:","$email");
        echo "Thank you for using the email !!!";
    }
    else
    {
        echo "Mail is not set properly. Please fill the form properly";
    }
?>

I am using a html form to get the required parameters for sending email. Following is the error I am getting while I send the email.

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\WebLearn\Apache-2.2\htdocs\SimpleWebsite\contact.php on line 7

Do I need to set anything to set in php.ini file or in httpd.conf? If yes how to configure it? Do I need an additional SMTP server on my PC to send email? Please suggest the necessary steps to send an email from my local PC.

like image 902
Surjya Narayana Padhi Avatar asked Jan 22 '12 07:01

Surjya Narayana Padhi


People also ask

Can we send email through PHP script?

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

Can I send email from localhost PHP?

Send Email from Localhost with PHPSet SMTP credentials (host, username, password, and port). Specify sender name and email ( $mail->setFrom ). Set recipient email address ( $mail->addAddress ). Set email subject ( $mail->Subject ).


1 Answers

The message is saying that it's trying to deliver the email to localhost:25, and there's nothing listening there.

PHP cannot email "the Internet" directly. The message must go to a mail server program such as Postfix or Sendmail or SSMTP, which then relays it to the appropriate destination.

You must install and configure a mail server program and set PHP to use it via php.ini. I believe you also have the option of configuring PHP to use a Sendmail binary instead of SMTP delivery.

like image 84
Borealid Avatar answered Sep 29 '22 17:09

Borealid