how to send email from Apache server by PHP script - php

I have installed Apache and PHP on my Windows 7 PC. I learning the PHP now. Following is my PHP script to send email.
<?php
if(isset($_REQUEST['email']))
{
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
mail("padhy.surya#gmail.com","$subject","$message","From:","$email");
echo "Thank you for using the email !!!";
}
else
{
echo "Mail is not set properly. Please fill the form properly";
}
?>
I am using a html form to get the required parameters for sending email. Following is the error I am getting while I send the email.
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:\WebLearn\Apache-2.2\htdocs\SimpleWebsite\contact.php on line 7
Do I need to set anything to set in php.ini file or in httpd.conf? If yes how to configure it? Do I need an additional SMTP server on my PC to send email? Please suggest the necessary steps to send an email from my local PC.

The message is saying that it's trying to deliver the email to localhost:25, and there's nothing listening there.
PHP cannot email "the Internet" directly. The message must go to a mail server program such as Postfix or Sendmail or SSMTP, which then relays it to the appropriate destination.
You must install and configure a mail server program and set PHP to use it via php.ini. I believe you also have the option of configuring PHP to use a Sendmail binary instead of SMTP delivery.

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

Send an email using mail($to,$subject,$txt);

This is my first time trying to send email using php. I used this script which I found on W3school however it gives me the error shown below. What did I miss here?
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
mail($to,$subject,$txt);
?>
Error:
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()
mail() in PHP depends on an e-mail server. Without one, PHP can't send any emails.
Now, this is the important part: you can connect to a e-mail service like Gmail using SMTP to send e-mail, or have your own little local server which would usually run on localhost:25, as it tried to connect to. Some server stacks have email built in, but others not.
As the error states, you can change the addresses in php.ini or using ini_set.

whats wrong with my php mail() function

Am not having good knowledge in PHP, I have just started to work on it...
Now am trying to use mail() function as example given in w3schools.com
<?php
$to = "ramkumarpece05#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "ramp211087#gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
for this am getting error message as follows
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 F:\contact.ph
p on line 7
Mail Sent.
where the php.ini file is located or i have to download it from the internet.....
Please help me to solve this issue...
You probably didn't install a mail server on your machine, you can install one for testing purposes (mail from your server will probably be marked as spam) or you can configure PHP to use your email account.
Your mail method is fine, your local server isn't. It is attempting to send the email but there is no SMTP server setup on your local server to send the email. You can install programs like Tomcat or Mercury which can handle the sending of the emails. You will just have to provide it some credentials to authenticate. I used my Gmail account's SMTP for example to send emails from my local server.

mail(): SMTP server response: 550 The address is not valid error on hmailserver

I am getting this error despite configuring php.ini to a valid send_from address. I know it is valid because it works when I send it from squirrel mail but when sending mail in php it will just not work. the invalid address presumably refers to the send_from address. so I can't see how it can think it's wrong. here is the php code:
$email="tobiasvogel1#googlemail.com";
$subject = "Your New Password";
$from="admin#dayshare.local";
$message = "Your new password is as follows:
xxxxxxxxxxxxxxxxxxxxxxxxxxx
This email was automatically generated.";
if(!mail($email, $subject,$message,$from)){
echo ("error");
}else echo "success";
and in php.ini:
SMTP = localhost
sendmail_from = admin#dayshare.local
550 Delivery is not allowed to this address
This error means that the sender is trying to send an email to an
address which he is not allowed to send to. This message is generated
after hMailServer has checked the IP range settings. As an example,
the default IP range configuration does not allow external users to
send messages to other external users. This is to prevent people from
using your server to send spam. So if an external user tries to send a
message to another external user, he will get this message.
That is the meaning of the error you are getting. This is from the hMailServer Documentation.
Can you try if the following will work?
<?php
mail('tobiasvogel1#googlemail.com','Test Email','This is a test email.',"From: tobiasvogel1#googlemail.com");
?>
If it doesn't work, then it's probably due to a misconfiguration in your hMailServer and you would need to check your hMailServer Logs.
The 4th parameter of the mail() function is not plain "from". In your code, you are only passing the email address without "From: " - The fourth parameter is for additional mail headers, so you have to format it like this:
mail($email, $subject,$message,"From: admin#dayshare.local\r\nX-Mailer: PHP");
I added another header as an example.
Try this, worked for me:
ini_set("sendmail_from", "info#yourdomain.com");
You need quotes and a semi-colon:
$email="tobiasvogel1#googlemail.com";
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");

What does the below Warning mean?

<?php
$to = "someone#example.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.";
?>
When I run these script I get these error
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:\xampp\htdocs\website\mail.php on line 7
What I have to do now? Could anyone tell me the way to do it ? Okay I have found that file but what are the fields I need to update could you suggest me some code so that i paste it these and save?
You have to configure your php.ini with a valid smtp server and email address.
You need to configure PHP to use a working SMTP service in order to send SMTP email. Here's an article that discusses the subject.
Basically, it appears that the SMTP configuration for your PHP instance is using default values, which point to localhost. But your local computer doesn't seem to be running an SMTP service. So you'll need to point is to a server that is running one, and one which your application is permitted to use.
I guess that this question is the same as your other question...
If so, I guess you are trying to send an email through GMail's SMTP server. Their server requires authentication, which PHP's mail function doesn't support.
As I answered you in the other question, you can use Zend_Mail to do this. You can also use PEAR Mail like in the example in the other question.
Either way you will need to download some external file, include it in your code and use it.

Categories