Send e-mail from php using mail function [duplicate] - php

This question already has answers here:
How to configure XAMPP to send mail from localhost?
(11 answers)
Closed 2 years ago.
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.

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.

Related

Why does the mail function stop the php script instead of sending the mail?

I am trying to call the mail function, but whenever I put it in the script, the page does not load.
I have the following code for my php.ini file in XAMPP:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost
; http://php.net/smtp-port
smtp_port=80
auth_username = XX_MYEMAIL_XX
auth_password = XXXXX_MYPASSWORD_XX
I have a 64-bit computer, but an error message said it was missing a sendmail_from, so I gave this variable a value. I have Mercury running from XAMPP, but I don't know if I configured anything that needs to be configured.
I get the following error
mail(): Failed to connect to mailserver at "localhost" port 80, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
I used the following php code:
<?php
$header = "From: varunsingh87#yahoo.com";
$to_email = 'VSpoet49#gmail.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
if (mail($to_email, $subject, $message)) {
echo "<p>Email sent!</p>";
} else {
echo "<p>Email not sent.</p>";
}
?>
Below this is the default html tags.
Update
I removed the sendmail_from and set the smtp_port to 25.
mail(): Bad Message Return Path i
Related
Warning: Bad Message Return Path (PHP)
Failed to connect to mailserver at "localhost" port 25
First, I never heard for mail server listening on port 80.
I have XAMPP installed too, but with configured with "smpt_port=25".
Second, you have "SMTP=localhost" so in order to send email you must have installed mail server on your machine, for example "Mercury" from XAMPP package.
Third, it can be very tricky to properly send email using "mail()" function (authentication, spam detection...) so best solution is to avoid usage of "mail()" function and use some robust library/component/script for that.
It is good advice by Baranix to learn how to use PhpMailer or SwiftMailer (my favourite) and configure them to target real, well configured mail server on real hosting.
Learn how to use PhpMailer and don't mess with this embarrassing mail function.
https://github.com/PHPMailer/PHPMailer/wiki/Tutorial
With this class you will send all messages with and without authorization, with or without tls/ssl and attachments (files, images).
!!! Install smtp server: hmailserver on localhost first !!!
https://www.hmailserver.com/download
And create your domain email mailbox.
Regards
http://php.net/manual/language.operators.errorcontrol.php
Let us learn about the # symbol, but be warned of a possible fake return status.
People typically try this first though
Try Catch Block

How does Mail PHP work?

I stumbled on the following script today for sending an e-mail using PHPMail.
<?php
$to = "some_address#domain.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "my_address#domain.com";
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
echo "Mail Sent.";
?>
Above can be runnable through php mail.php and instantly you'll get an e-mail sent to $to from $from despite not needing to set outgoing/ingoing servers out.
It really intrigued me, since my CMS uses an SMTP outgoing server (well, same way Mail PHP does), which I need to set up with my Outlook SMTP username and password - some sort of verification.
However, about Mail PHP just.. sends an e-mail. To the address you set it as. From the address you set it as.
Looking at PHP docs it does not really reveal how it works. Does Mail PHP not have any issues with spamming since anyone can send anyone anything anytime programmatically without verification of the from identity?
EDIT:
It's rather funny the people in the comments were talking about the POTUS, since I had the exact thing in mind:
It did land in my junk folder, but I'm sure it isn't hard to make this look convincing enough and still be considered "oh damn spam filter lost my e-mail!"
The mail function uses the settings from php.ini. The details of this configuration can be found in Mail Runtime Configuration.
The defaults can be set in php.ini, although you can override them using ini_set.
I bet you sent the mail from a PHP script on a hosted server. That server probably has SMTP settings configured beforehand. If you would try this locally on a WAMP/LAMP server, you would have to do this configuration yourself, since PHP cannot read your Outlook/WhateverMailclient settings.
As stated in the comments, you can specify the sender/from address yourself. SMTP doesn't require this to be the actual sender domain, so that's why this works. The missing link is the pre-configured SMTP server of your host.
Some relay servers do check for this, and your mail might be blocked or sent to a junk mail folder. You can however configure this in your DNS to indicate that <Your server's IP> is indeed allowed to send email for <yourdomain>. For more information about that subject, you might want to read this question on ServerFault.
It uses the smtp protocol or send_mail, you can even configure what php should use to send mails in php.ini. It can send e-mail but the e-mail will end-up in your spam filter take a look to DKIM and SPF records for more information

Email is not being send in PHP

I am trying to send an email using mail function in php. I have read the documentation and followed the examples but in vain. I don't know if I am doing anything wrong.
Can anyone please help me. I also followed other examples from the community without success.
And here also;
Send email with PHP from html form on submit with the same script
Here is my code:
$name =array($_POST['name'],$_POST['email'],$_POST['phone'],$_POST['comments']);
$to = "fasdjgasgd#yahoo.com";
$subject = "Form submission";
$message = "$name[0] wrote the following: <br/> $name[3]";
$header = "FROM:".$name[1];
mail($to, $subject, $message, $header);
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.
PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed [mail function].
Windows users should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.
The configuration for Windows should look something like this:
[mail function]
SMTP = localhost
sendmail_from = email#domain.com
This link will help you.

PHP: how to test mailing to localhost on Linux and actually receiving it?

I'm building a daemon in php that checks for received emails which it then stores in the database leading them through a whole process. The thing is that I want to build some unit tests for this, for which I don't want to setup a whole mail server.
So for tests I want to somehow send emails to localhost, which should then be picked up by the daemon and processed further. So I tried the following:
$headers = 'From: me#mydomain.com \r\n Reply-To: me#mydomain.com \r\n X-Mailer: PHP/' . phpversion();
mail('www-data#localhost', 'THE SUBJECT', 'THE BODY IS HERE', $headers);
When I then run mail from the command line, I just get a message saying No mail for kramer65.
So my question; does anybody know how I can send emails to localhost in php, and how I can then read these emails from within php again? All tips are welcome!
[EDIT]
So I figured that it is sending an email to the www-data account, and not to my personal kramer65 account. I changed the to email address into kramer65#localhost, and when I now run mail I get
kramer65#php0:~$ mail
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/kramer65": 1 message 1 new
>N 1 kramer65e#php0 Fri Apr 25 10:48 16/495 THE SUBJECT
&
My following question is now; how do I read or somehow get this email from within php?
This depends on how you have configured the php internal mail settings. If you configured it to use a local mail forward agent (sendmail or similar) then you should be able to send messages to a local account (not a local email address) by just specifying the account name. At least this is what such agents offer. Unless php explicitly prevents such usage it might be worth a try.
You cannot send to a local email address, since that requires an email server, specifically an smtp server (exim or the like). Without it there is no component that could accept an incoming message.

how do I get and use php.ini for sending email

I was trying to sending email by mail() in php. The following is the code I'm using.
But W3School suggest tsing php.ini
They say:
"Requirements
For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file."
But I have no idea how to find this php.ini and how to use it. Any suggestions? Thanks
<html>
<head>
<?php
echo 'hello';
$to = "aaa#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
</head>
<body>
<h1>Mail page</h1>
</body>
The php.ini file is stored in your webserver configuration. The location of that depends on your OS. On linux, it'll be /etc/php/php.ini or /etc/php.ini. On windows with WAMP or XAMPP, you can open it using the toolbar icon for the server.
What W3School suggests is that you make sure that mail is enabled in your php.ini file. php.ini is the configuration file for PHP. If mail is not properly configured in it, then it won't work.
Be warned that many web hosts have PHP mail disabled because it is incredibly easy to automate spam using it. Fewer hosts do that now that spam filters have gotten much better, but it still does happen (especially on free hosts).
You can search for [mail function] inside php.ini file, however, if you are using Apache on your local server, you can't send email using php mail() function, because sending email requires SMTP server at that case (Public IP Address) to talk with remote host that you send the email, generally, people don't send email from local server, and if so, you need to send email using SMTP method, another option is to install SMTP server on your local pc but this is not recommended.
you can look at http://swiftmailer.org/ and try to read more about it.
php.ini is very useful and it is a configuration file that is used to customize behavior of PHP at runtime.
PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
This means you have to install some sort of mailserver and configure php to use it. Usually this mailserver is postfix in Ubuntu. However - if the php side you are coding will eventually be stored on a hosting service's side (e.g. xmission), a mail server will most likely already be installed there. In that case just test your site online instead of localy.
Hope this helps!

Categories