1

I want to send e-mail from a php file (Windows 10, localhost, XAMPP).

I followed this tutorial: Link

My php.ini file looks like this:

[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net

; For Win32 only.
sendmail_from = some.email@gmail.com

My php file contains these:

$to = "another.email@gmail.com";
$subject = "Subject";
$mesaj = "Message";

$headers = "From:some.email@gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
mail($to, $subject, $message, $headers);

When running, this warning appears:
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() in C:\xampp\htdocs\store\email_sender.php on line 61 No e-mail sent.

I saw similar questions (Link_1, Link_2, Link_3).

But I don't understand what I have to do. I have read that I need to install a SMTP server. What server should I install?

I have also followed this example (sending e-mail from mail function php), but the warning is still there and no e-mail is sent.

EDIT: I have modified the information provided into the php.ini file:

[mail function]
SMTP=smtp.gmail.com
smtp_port=587
smtp_port=465
sendmail_from = some.mail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

I have also modified the information provided into the sendmail.ini file:

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
;debug_logfile=debug.log
auth_username=some.mail@gmail.com
auth_password=somepassword
force_sender=some.mail@gmail.com

I have to mention that, in Gmail, the 2-step verification is disabled and the access to less secure apps is enabled. I have also stopped and started the Apache server.

tp123
  • 47
  • 6
  • 2
    See also: [Where can I find php.ini?](//stackoverflow.com/q/8684609), and [Do I need to restart Apache after changing the php.ini file?](//stackoverflow.com/q/12892331), as well as the obligatory [PHP mail function doesn't complete sending of e-mail](//stackoverflow.com/q/24644436) – mario Feb 06 '21 at 13:38
  • 2
    https://stackoverflow.com/q/15965376/231316 – Chris Haas Feb 06 '21 at 13:51

1 Answers1

2

All of the help links that you included in your question are correct. What I hear you saying, is that you don't fully comprehend what those links are telling you to do. Let me see if I can help you understand what is necessary to accomplish what you are trying to do.

When you send an email message from any program that you create, whether you're writing code in PHP, C++, Java ... makes no difference, the underlying libraries from your programming language do fully understand how to send an email. But you can only send an email using an email server that is actively working on the Internet, and one with which you have an account that has permission to send an email.

If email servers just let anyone send email through them, you can imagine how much worse spam would be on this planet.

Installing an SMTP server on your local machine won't solve your problem either, because you would need to have a subdomain that you control (whateverwhatever.com) and you would need to create MX records in a publically visible DNS server. You could buy a domain name with GoDaddy, then create your MX records and point them to your IP address, etc. but that's a lot of work.

What I suggest you do, is if you have a GMAIL account, you can use a Gmail server to send your email through, but you will need to configure your PHP code (either using ini_set() commands or in your php.ini file under the [mail function] heading with the information that the Gmail servers require.

Here are the fairly common pieces of information that most SMTP servers require, which you must define in your code or the php.ini file:

  • SMTP Server address (smtp.gmail.com)
  • Your Gmail account name
  • Your Gmail account password
  • The port numbers that the Gmail server requires

And there may be other pieces of info that it needs to see before it allows you to send the email.

Take a look at this page which explains how to use your own Gmail account to send an email for free. Also, do some Google searches using phrases like 'how to send SMTP through Gmail using my personal account' ... the information is out there.

Once you have learned what the Gmail servers require in order to send SMTP email, you simply input all of those required pieces of information into your PHP code or in the php.ini file. And there is plenty of documentation out there on how to do that.

Further discussion:

A little more clarification on what you're actually doing: ... you need to understand that your PHP program that you are writing is - for lack of a better term - en ad-hoc email client. You are use to sending email either with Outlook, or a web interface or some other email client, and you just write the email, put in the address of the person that you are sending to and you just click send and it goes ... but now, you're writing software to do the portion of the email sending that happens after you click send from an email program ... the part of the email process that you never have to think about ... you are now needing to create with your code. So your code needs to know where to place that email message, and email servers will not accept an email message from any place without proper credentials.

So you're basically writing with PHP code, a very light version of an email client that needs to be taught how to send an email... which is all the stuff that happens after you click SEND when you send an email to your mom.... you have never needed to know what happens to your email after you click send until now ... because you're hard coding the process literally in your PHP code.

I hope that helps you understand what's happening here a little better than you did.

Michael Sims
  • 2,360
  • 1
  • 16
  • 29