Problem in sending email from localhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Problem in sending email from localhost

Post by shariefbe »

Hi friends,

I have designed and one form and php code to send mail. I am developing this in my localhost,

So now i want to send email from my localhost to test my programming.

I googled and i find something like editing "php.ini" file. But it didnt work for me.

So can anyone help me and i am using wamp server.

Thank you.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Problem in sending email from localhost

Post by social_experiment »

Do you receive any error messages when attempting to send an email? Take a look at this url

http://www.wampserver.com/phorum/read.p ... #msg-70522

Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

I already downloaded phpmailer. But i dont know how to use that php mailer.

I am not getting any wrror message.

for you referencec i will post my code with this post

Code: Select all

<form name="contactform" method="post" action="sendmail.php">
              <tbody><tr>
                          <td colspan="2" class="text" valign="top"><strong>Please fill the following mandatory fields to get quick response from our edtech team.</strong></td>
                        </tr>
                        <tr>
                          <td valign="top" width="36%">&nbsp;</td>
                          <td class="h2" align="right" valign="top" width="64%"> <span class="red-text">required field *</span></td>
                        </tr>
                        
                        <tr>
                          <td class="text" valign="top"><span class="red-text">*</span> Name : </td>
                          <td valign="top"><input name="name" class="input" size="35" type="text"></td>
                        </tr>
                        
                        <tr>
                          <td class="text" valign="top"><span class="red-text">*</span> Company :</td>
                          <td valign="top"><input name="company" class="input" id="Company" size="35" type="text"></td>
                        </tr>
                        
                        <tr>
                          <td class="text" valign="top"><span class="red-text">*</span> E-mail : </td>
                          <td valign="top"><input name="email" class="input" size="35" type="text"></td>
                        </tr>
                        <tr>
                          <td class="text" valign="top"><span class="red-text">*</span> Contact No: </td>
                          <td valign="top"><input name="contact" class="input" size="35" type="text"></td>
                        </tr>
                        <tr>
                          <td class="text" valign="top"><span class="red-text">*</span> Address :</td>
                          <td valign="top"><input name="address" class="input" id="Address" size="35" type="text"></td>
                        </tr>
                        <tr>
                          <td class="text" valign="top"><span class="red-text">*</span> Requirment </td>
                          <td valign="top"><textarea name="description" cols="38" rows="4" class="input" id="Description"></textarea></td>
                        </tr>
                        
                        <tr>
                          <td class="heading3" valign="top">&nbsp;</td>
                          <td valign="top"><input name="submit" value="Submit Now" class="more-button2" type="submit">
                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <input name="Reset" value="Reset" class="more-button2" type="reset"></td>
                        </tr>
                      
                    </tbody>
					</form>
Now sendmail.php code

Code: Select all

<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "shariefbe@gmail.com";
    $email_subject = "Inquiry from you']";
     
     
    /* function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['company']) ||
        !isset($_POST['email']) ||
        !isset($_POST['contact']) ||
        !isset($_POST['address']) ||
		 !isset($_POST['description'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }*/
	    
	$name = $_POST['name']; // required
    $company = $_POST['company']; // required
    $email_from = $_POST['email']; // required
    $contact = $_POST['contact']; // not required
    $comments = $_POST['description']; // required
     
   /* $error_message = "";
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";*/
	
	
	// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $comments, $headers); }
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Problem in sending email from localhost

Post by social_experiment »

Code: Select all

 @mail($email_to, $email_subject, $comments, $headers); 
Remove the '@' in front of the mail() function. Then you should receive an error message. Once it's fixed you can replace the '@'
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

yes, when i remove this '@" symbol. I am getting the below warning as

"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 F:\test\xampp\htdocs\sendmail.php on line 42'
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Problem in sending email from localhost

Post by josh »

PHP mailer is not an SMTP server, its just a wrapper for the regular mail() function for the most part.
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

Now i am not using php mailer. Now i a using mercury smtp server which come from "xampp".
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Problem in sending email from localhost

Post by josh »

First enable telnet, then open a command line and try "telnet localhost 25". Try to manually send an email over the command line

http://www.wikihow.com/Send-Email-Using-Telnet
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

Thanks for your reply

If i follow the steps in that link i am getting one error when i use 'MAIL FROM:you@server.com."

"Must issue a STARTTLS command first"

So i used that command first and then used the "MAIL FROM" command. But the connection is getting lost when i use this STARTTLS command.

So please help me
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

Now i am getting this following error

Code: Select all

Warning: mail() [function.mail]: SMTP server response: 550-5.7.1 [122.162.237.107] The IP you're using to send mail is not authorized 550-5.7.1 to send email directly to our servers. Please use the SMTP relay at 550-5.7.1 your service provider instead. Learn more at 550 5.7.1 http://mail.google.com/support/bin/answer.py?answer=10336 f1si42544973wfp.91 in F:\test\xampp\htdocs\sendmail.php on line 43
Can any one help me plz. I am struck with this from long time...
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Problem in sending email from localhost

Post by Zyxist »

In order to send e-mails from localhost, you must install a mail server on your computer and configure it to send mails. All PHP does is passing the message to the mail server and nothing more. It does not send it anywhere else. There is however an alternative. You can register some mailbox at some external server (i.e. at GMail), and connect to it directly via SMTP. It requires from you to handle the entire protocol manually, and in case of GMail, you must also encrypt the transmission with SSL (OpenSSL extension should be installed), but it works and does not require chaning php.ini. There are some mailing libraries that can handle it for you, i.e. SwiftMailer. PHP mail() function won't work here, because the configuration for it supports neither authentication nor encryption.

Personally, I use a very simple solution for handling mails at localhost. I wrote a simple program in C with less than 1KB of code that reads the mails from the standard input and simply saves them as files on the hard disk. PHP thinks the mail has been sent, I have the message content, and everyone is happy :).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Problem in sending email from localhost

Post by josh »

shariefbe wrote:Thanks for your reply

If i follow the steps in that link i am getting one error when i use 'MAIL FROM:you@server.com."

"Must issue a STARTTLS command first"

So i used that command first and then used the "MAIL FROM" command. But the connection is getting lost when i use this STARTTLS command.

So please help me
Turn off the TLS on the localhost's MTA (mail server), also lock it down to sending emails originating from localhost only. Then, try again and post back, thanks! Alternatively switch to a client with TLS support (3rd party mail library for PHP, or outlook express for testing the mail server outside of PHP). I'd recommend turning off TLS because that's 100x harder to configure than the basic mail server, get the basics working first.
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

Zyxist wrote:In order to send e-mails from localhost, you must install a mail server on your computer and configure it to send mails. All PHP does is passing the message to the mail server and nothing more. It does not send it anywhere else. There is however an alternative. You can register some mailbox at some external server (i.e. at GMail), and connect to it directly via SMTP. It requires from you to handle the entire protocol manually, and in case of GMail, you must also encrypt the transmission with SSL (OpenSSL extension should be installed), but it works and does not require chaning php.ini. There are some mailing libraries that can handle it for you, i.e. SwiftMailer. PHP mail() function won't work here, because the configuration for it supports neither authentication nor encryption.

Personally, I use a very simple solution for handling mails at localhost. I wrote a simple program in C with less than 1KB of code that reads the mails from the standard input and simply saves them as files on the hard disk. PHP thinks the mail has been sent, I have the message content, and everyone is happy :).
i dont want to encrypt anything. Just i want to check whether my code is right. for that i am using "xampp". it contains mercury mail server inbuilt. i started that service before sending the mail. even though its not working....
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Problem in sending email from localhost

Post by josh »

The most recent error you post is because you're trying to use Google's SMTP servers without a non Google account. Use localhost, not mail.google.com Disable TLS, you want no encryption.
shariefbe
Forum Newbie
Posts: 16
Joined: Thu Jan 20, 2011 12:10 am
Location: Trichy, India

Re: Problem in sending email from localhost

Post by shariefbe »

josh wrote:The most recent error you post is because you're trying to use Google's SMTP servers without a non Google account. Use localhost, not mail.google.com Disable TLS, you want no encryption.
I have mail accounts in gmail and yahoo...I am trying this from my home. Now what to use as smtp?
Post Reply