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!
Related
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.
I keep getting this error when I try to send an e-mail in PHP:
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:\wamp\www\dressoholic\register.php on line 50
my php.ini looks like this:
[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#yoursite.com
I'm using my laptop as a server.. What am I doing wrong? Thanks.
You need to be running a mail server locally.
If this is Unix, enable sendmail
If this is Windows install the Simple Mail Transfer Server (not sure if the name is correct) component of IIs. E.g. for windows 2003 follow this: http://msdn.microsoft.com/en-us/library/8b83ac7t.aspx
For sending mails using php mail function is used.
But mail function requires SMTP server for sending emails.
we need to mention SMTP host and SMTP port in php.ini file.
Upon successful configuration of SMTP server mails will be sent successfully sent through php scripts.
On windows, nearly all AMPP (Apache,MySQL,PHP,PHPmyAdmin) packages don't include a mail server (but nearly all naked linuxes do have!). So, when using PHP under windows, you need to setup a mail server!
Imo the best and most simple tool ist this: http://smtp4dev.codeplex.com/
SMTP4Dev is a simple one-file mail server tool that does collect the mails it send (so it does not really sends mail, it just keeps them for development). Perfect tool.
If you are running your application just on localhost and it is not yet live, I believe it is very difficult to send mail using this.
Once you put your application online, I believe that this problem should be automatically solved. But i think ini_set() helps you to change the values in php.ini during run time.
First of all, you aren't forced to use an SMTP on your localhost, if you change that localhost entry into the DNS name of the MTA from your ISP provider (who will let you relay mail) it will work right away, so no messing about with your own email service. Just try to use your providers SMTP servers, it will work right away.
PHP mail function can send email in 2 scenarios:
a. Try to send email via unix sendmail program
At linux it will exec program "sendmail", put all params to sendmail and that all.
OR
b. Connect to mail server (using smtp protocol and host/port/username/pass from php.ini) and try to send email.
If php unable to connect to email server it will give warning (and you see such workning in your logs)
To solve it, install smtp server on your local machine or use any available server. How to setup / configure smtp you can find on php.net
Change SMTP=localhost to SMTP=smtp.gmail.com
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
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.
I am new to PHP and I'm using the mail function to send emails which is not working. I get a success message, but still it does not work
same code
<?php
$email_to = "abc#abc.com";
$email_subject = "Test mail";
$email_body = "Hello! This is a simple email message.";
if(mail($email_to, $email_subject, $email_body)){
echo "The email($email_subject) was successfully sent.";
} else {
echo "The email($email_subject) was NOT sent.";
}
?>
Am I missing anything, do I need to include any files for this function.. I am from asp.net & this is the basic script which found on website.
I tried other scripts related to mail they didn't work either..
I AM RUNNING THIS SCRIPT ON THE WEBSITE NOT on the localhost
If you are using Ubuntu and it seem sendmail is not in /usr/sbin/sendmail, install sendmail using the terminal with this command:
sudo apt-get install sendmail
and then run reload the PHP page where mail() is written. Also check your spam folder.
This is probably a configuration error. If you insist on using PHP mail function, you will have to edit php.ini.
If you are looking for an easier and more versatile option (in my opinion), you should use PHPMailer.
This might be the issue of your SMTP config in your php.ini file.
Since you new to PHP, You can find php.ini file in your root directory of PHP installation folder and check for SMTP = and smtp_port= and change the value to
SMTP = your mail server e.g) mail.yourdomain.com
smtp_port = 25(check your admin for original port)
In case your server require authentication for sending mail, use PEAR mail function.
"Just because you send an email doesn't mean it will arrive."
Sending mail is Serious Business - e.g. the domain you're using as your "From:" address may be configured to reject e-mails from your webserver. For a longer overview (and some tips what to check), see http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html
The mail function do not guarantee the actual delivery of mail. All it do is to pass the message to external program (usually sendmail). You need a properly configured SMTP server in order for this to work. Also keep in mind it does not support SMTP authentication. You may check out the PEAR::Mail library of SwiftMailer, both of them give you more options.
Check your SMTP settings in your php.ini file. Your host should have some documentation about what credentials to use. Perhaps you can check your error log file, it might have more information available.
For HostGator, you need to use the following for your headers:
$headers = 'From: user#yourdomain.com' . " " .
'Reply-To: user#yourdomain.com' . " " .
'X-Mailer: PHP/' . phpversion();
It only worked for me when the from user was host e-mail while the Reply-To can be something different e.g. From: owner#domain.com, Reply-To: info#domain.com
http://support.hostgator.com/articles/specialized-help/technical/php-email-from-header
http://support.hostgator.com/articles/specialized-help/technical/how-to-use-sendmail-with-php