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.
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.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am creating a login page.For that, after the signup I need to send the mail to the user for verification.Therefore, I need to send mail.To send the mail, I tried with mail() function like this,
<?php
//sending email with the php mail()
mail('mahadev.3333#gmail.com', 'Subject Line Here', 'Body of Message Here', 'From: vidya.5555#gmail.com');
?>
I have configured php.ini file also like below,
[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 ="vidya.merahkee#gmail.com"
I am using windows 64-bit.
And also I have tried with phpmailer. But,I didn't find any solution.Since, 2 day I am trying this.I am unable to do this one.
Can anybody help me to solve this?
First of all if you are using the script on production server then ask your admin for the mail function is active or not . Some time on production server it's blocked by Site admin.
Second if you are using this script on localhost server means on your desktop then first you have to use the mail server start.
for example i am considering that you are using XAMMPP on windows then Your have to start the Mercury Mail Server From the XAMPP control panel.
Mercury by default comes with postmaster and newuser two users.
Then You have to download Mozilla Thunderbird mail client and configure the
newuser and postmaster user in mail client by following the Create New account > email using the newuser#localhost and manually configure the pop and smtp server and port . usually default ports.
you can use the sendmail_from ="newuser#localhost"
then check Mercury Mail server is running then test your script.
it will send the mail and you then check it in the Mozilla Thunderbird mail client.
Third : for setting the From: you have to use the header like below
$headers = "From: newuser < newuser#domain.com >\n";
<?php
$send_to = 'postmaster#localhost';
$mail_subject = 'Subject of Your mail';
$message_body = 'Body of your email like message form php script.';
$headers = 'From: newuser#localhost' . "\r\n";
mail($send_to, $mail_subject, $message_body, $headers);
?>
I hope this will solve the problem .
NOTE if you want to send email form localhost to gamil or any other mail site then you have to use the sendmail application or more tutorial Search the google Term like 'Send email via Gmail in PHP '
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!
Do I understand correctly what I read on the CI website for the email class that if I am using PHP mail() that I don't need to do any configuring?
Here is my email in my controller. It is not sending. We are on a new server and I'm wondering what if anything I need to configure.
$this->email->from('oemsales#xxxx.com', 'OEM Sales');
$this->email->to('shummel#xxxx.com');
$this->email->subject('Contact Page Request');
$this->email->message($message);
$this->email->send();
$message is defined higher up in the code. This worked on our old server.
you can find following information in php.ini file. SMTP value contains the address of your mail server, you can change it to your mail server address.
[mail function]
SMTP = localhost
smtp_port = 25
I use the latest WAMP and I get this when I try to send emails:
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\main\createaccount.php on line 8
Message delivery failed...
The message:
$to = "xxx#hotmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Do you need do download a "mailserver" also?
Please help.
This works for me and should work for you: Use Fake Sendmail and a webhost mail server (i.e. - Godaddy, 1and1, etc.).
1.) Download the sendmail zip and extract it to C:\Wamp\bin\sendmail (for purposes of this example).
2.) Edit C:\wamp\bin\sendmail\sendmail.ini and set the following to your mail server's requirements (mine are below):
smtp_server=mail.yourdomain.com
smtp_port=26
smtp_ssl=none
;default_domain=yourdomain.com
auth_username=smtpuser#yourdomain.com
auth_password=smtppassword
;pop3_server=
;pop3_username=
;pop3_password=
;force_sender=
;force_recipient=
3.) Set the path of sendmail.exe in your php.ini file.
[mail function]
; For Win32 only.
SMTP =
; For Win32 only.
sendmail_from =
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\wamp\bin\sendmail\sendmail.exe -t"
4.) Restart Wampserver.
You might have success using Gmail, but there are a few extra tweaks to make it work. I prefer using the mail server of the webhost where I upload my code.
You are not running an smtp server on your machine, but you don't have to. Just set SMTP to a open smtp server for example:
ini_set('SMTP', 'smtp.yourisp.com');
Take a look at your ISP's home page or http://www.e-eeasy.com/SMTPServerList.aspx for list of SMTP servers.
If you have a desktop mail program, you can use the same address as you use for outgoing mail.
I think your mail server (SMTP) outgoing mail server is not configured in your php.ini file.
Have a look at this:
http://email.about.com/od/emailprogrammingtips/qt/Configure_PHP_to_Use_a_Remote_SMTP_Server_for_Sending_Mail.htm
Also hotmail doesn't allow you to use their mailservers. You should use yahoo or gmail.
Are you sure these servers have a mail program installed on them? If not, that's your problem. For example, XAMPP comes with a mail program called Mercury which you must start before you can send mail through the server.
Follow this article, it works if you have a gmail account.
or at least any email account in which you know the server, port and this stuff.
Send Email From localhost
here is another solution - WAMP send Mail using SMTP localhost
KEEP IN MIND, everytime, after You change php.ini,
you must restart wamp (! ! !)
p.s. in php.ini, i have used:
SMTP = localhost
smtp_port = 25
sendmail_from = your_user#gmail.com
or if oyu cant edit php.ini, try to insert these lines in your php script.
ini_set("SMTP", "localhost");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "your_user#gmail.com");