8

I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:

  $to = "myweb@gmail.com";
  $subject = "Test mail";
  $message = "My message";
  $from = "webp@gmail.com";
  $headers = "From:" . $from;
  mail($to,$subject,$message,$headers);

When I try running the program this is what I get:

 Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().

Please tell me what address to include in the $from variable. Do I need a smtp server for this? How do I send mails using a localhost? Please tell me what exactly to edit in the php.ini file

I am new to all this.. Please help me..

neves
  • 33,186
  • 27
  • 159
  • 192
abcdefgh
  • 151
  • 2
  • 3
  • 6
  • I take it you're on a Windows box? You'll need to, as specified in the error message, define your SMTP server's address and port #. You're trying to connect to a local SMTP server, which you do not have. – Marc B Jul 08 '13 at 18:51
  • [Test mail server tool](http://www.toolheap.com/test-mail-server-tool/) is a nice tool to test email on localhost. I will suggest you to install it. – Fallen Jul 08 '13 at 19:02
  • Just thought i would mention XAMPP can use **mailtodisk** that works well on localhost for development – Ricardo Saracino May 28 '20 at 14:36

8 Answers8

3

Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer

How to use it:

require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';

$body = 'This is the message';

$mail->IsSMTP();
$mail->Host       = 'smtp.gmail.com';

$mail->SMTPSecure = 'tls';
$mail->Port       = 587;
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;

$mail->Username   = 'me.sender@gmail.com';
$mail->Password   = '123!@#';

$mail->SetFrom('me.sender@gmail.com', $name);
$mail->AddReplyTo('no-reply@mycomp.com','no-reply');
$mail->Subject    = 'subject';
$mail->MsgHTML($body);

$mail->AddAddress('abc1@gmail.com', 'title1');
$mail->AddAddress('abc2@gmail.com', 'title2'); /* ... */

$mail->AddAttachment($fileName);
$mail->send();
pouria
  • 949
  • 8
  • 21
2

You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.

If you own a server or VPS upload the script and it will work fine.

1

You won't be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.

neves
  • 33,186
  • 27
  • 159
  • 192
1

To fix this, you must review your PHP.INI, and the mail services setup you have in your server.

But my best advice for you is to forget about the mail() function. It depends on PHP.INI settings, it's configuration is different depending on the platform (Linux or Windows), and it can't handle SMTP authentication, which is a big trouble in current days. Too much headache.

Use "PHP Mailer" instead (https://github.com/PHPMailer/PHPMailer), it's a PHP class available for free, and it can handle almost any SMTP server, internal or external, with or without authentication, it works exactly the same way on Linux and Windows, and it won't depend on PHP.INI settings. It comes with many examples, it's very powerful and easy to use.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Marcovecchio
  • 1,322
  • 9
  • 19
  • On anything other than MSWindows it doesn't use SMTP - so why should it implement SMTP authentication? – symcbean Jul 08 '13 at 20:24
  • @symcbean Because in MANY situations you need to. PHP Mailer is just much better: it allows you to use security if you need to, but it can work without it. However, the mail() function won't allow it in any way. So, the choice is clear. Go see, for example, what WordPress and other CMS use to send their emails: it's PHP Mailer, and NOT mail(). – Marcovecchio Jul 08 '13 at 20:37
  • I think you're missing the point PHP is not a webserver. PHP is not an MTA. There are lots of tools to extend PHP's functionality to support different protocols hence it DOES NOT NEED to do those things. If you want a single tool which implements everything badly then PHP is not a good choice. I have no problem with you recommending phpmailer, nor would I criticise you for suggesting a smart relay which support SSMTP / SMTP/TLS / SMTP auth / pop before SMTP.... but it's absurd to criticize PHP for not implementing functionality that is better implemented elsewhere and trivial to integrate. – symcbean Jul 08 '13 at 20:46
  • 2
    Am I criticizing PHP? I'm just saying the mail() function lacks some important features, just like you did in your own answer, and I recommended a free class, written in PHP, that does everything he may ever need, just like you did, after me. – Marcovecchio Jul 08 '13 at 20:49
1

Here's the link that gives me the answer and we use gmail:

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
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
1

configure your php.ini like this

SMTP = smtp.gmail.com

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury

; SMTP = smtp.gmail.com

; smtp_port = 465

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
achref akrouti
  • 575
  • 1
  • 6
  • 21
1

This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.

<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $to = 'your.email@address.com';
    $subject = "New Message on YourWebsite.com";
    $body = '<html>
                <body>
                    <h2>Title</h2>
                    <br>
                    <p>Name:<br>'.$name.'</p>
                    <p>Email:<br>'.$email.'</p>

                </body>
            </html>';

//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers = "Reply-To: ".$email."\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=utf-8";

//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
    echo '<br>';
    echo "Success. Thanks for Your Message.";
} else {
    echo 'Error.';
}
}
?>

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="name" placeholder="Your Name"><br>
            <input type="text" name="email" placeholder="Your Email"><br>
            <button type="submit">Subscribe</button>
        </form>
    </body>
</html>
Kris M
  • 81
  • 7
0

Usually a good place to start when you run into problems is the manual. The page on configuring email explains that there's a big difference between the PHP mail command running on MSWindows and on every other operating system; it's a good idea when posting a question to provide relevant information on how that part of your system is configured and what operating system it is running on.

Your PHP is configured to talk to an SMTP server - the default for an MSWindows machine, but either you have no MTA installed or it's blocking connections. While for a commercial website running your own MTA robably comes quite high on the list of things to do, it is not a trivial exercise - you really need to know what you're doing to get one configured and running securely. It would make a lot more sense in your case to use a service configured and managed by someone else.

Since you'll be connecting to a remote MTA using a gmail address, then you should probably use Gmail's server; you will need SMTP authenticaton and probably SSL support - neither of which are supported by the mail() function in PHP. There's a simple example here using swiftmailer with gmail or here's an example using phpmailer

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94