Jump to content

Can't get e-mail form to work


bugzy

Recommended Posts

I have this code as my session_init.php

 

<?php
$_SESSION['APP_MX'] = "mail.mywebsite.com";
$_SESSION['APP_SERVER'] = "mywebsite.com";
$_SESSION['SITE_EMAIL'] = "admin@mywebsite.com";

$_SESSION['MYSQL_SERVER1'] = "localhost";
$_SESSION['MYSQL_LOGIN1'] = "root";
$_SESSION['MYSQL_PASS1'] = "password";
$_SESSION['MYSQL_DB1'] = "sample";

$_SESSION['LOGGEDIN'] = "";
$_SESSION['USERID'] = 0;

$_SESSION['EMAIL'] = "";
$_SESSION['FNAME'] = "";
$_SESSION['LNAME'] = "";

$_SESSION['SESSION'] = true;

?>

 

 

and I'm calling it here

 

<?php

if(!isset($_SESSION['SESSION'])) require('../email/session_init.php');



$fname = $_POST["fname"];
$lname = $_POST["lname"];
$email= $_POST["email"];
$company = $_POST["company"];
$subject = $_POST["subject"];
$phone = $_POST["phone"];
$msg = $_POST["msg"];





$mail = "Hello $fname,\n\nThank your for your email.\nWe look forward to your evaluation of our product.\n\n";
$mail .= "Here is the information you have given us:\n\n";
$mail .= "Name: ".$fname." ".$lname."\n";
$mail .= "Company: ".$company."\n";
$mail .= "Email Address: ".$email."\n";
$mail .= "Phone: ".$phone."\n\n";
$mail .= "Subject: ".$subject."\n";
$mail .= "Your Message:\n".$msg."\n\n\n";
$mail .= "Best Regards,\nCustomer Service\n\n";


// If any lines are larger than 70 characters, we will use wordwrap()
$message = wordwrap($msg, 70);

mail($email, $subject, $mail, "From: admin@".$_SESSION['APP_SERVER']."\r\n");

$mail = str_replace("\n", "<br>", $mail);

?>

 

 

And I'm getting this error

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 26, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\php\website_interactivity\email\email_sent.php on line 33

 

 

I wonder if there's any modification that I need to do in my php.ini file. Anyone?

Link to comment
Share on other sites

OT but, Why on earth are you storing that data in a users $_SESSION? That is application specific data, the $_SESSION is *client* specific.

 

You will have duplicate copies of the same data for every user on your site for no good reason.

Link to comment
Share on other sites

OT but, Why on earth are you storing that data in a users $_SESSION? That is application specific data, the $_SESSION is *client* specific.

 

You will have duplicate copies of the same data for every user on your site for no good reason.

 

Hey thanks for the advise there. I'm just new and I'm just trying things out.

 

My main concern is this email issue I'm getting....

Link to comment
Share on other sites

Failed to connect to mailserver at "localhost"

 

^^^ Do you have a mail server installed and running on your localhost Windows computer that is listening on that port number?

 

The php mail function acts like a simple unauthenticated email client (i.e. Outlook/Thunderbird but without a username/password.) It needs a locally installed mail server (that it accesses through a command line interface - sendmail) or an SMTP mail server that has been configured to trust the IP address of the web server that your php script is running on.

 

Do you actually need to send email from your localhost computer or are you just trying to test a script that uses the mail() function in it?

Link to comment
Share on other sites

Failed to connect to mailserver at "localhost"

 

^^^ Do you have a mail server installed and running on your localhost Windows computer that is listening on that port number?

 

The php mail function acts like a simple unauthenticated email client (i.e. Outlook/Thunderbird but without a username/password.) It needs a locally installed mail server (that it accesses through a command line interface - sendmail) or an SMTP mail server that has been configured to trust the IP address of the web server that your php script is running on.

 

Do you actually need to send email from your localhost computer or are you just trying to test a script that uses the mail() function in it?

 

Hey thanks for the quick response.

 

No, I'm not trying to send a mail on my localhost computer. But I do have an email server from another server.

 

with an incoming mail server at: mail.mysamplewebsite.com

 

Sorry for my ignorance on this as I'm just starting to learn php.

 

Is there a simple way to do it. I'm basing that code from a video tutorial I have here and maybe they set it up for a local machine...

Link to comment
Share on other sites

But I do have an email server from another server.

 

Network-wise, what is the relationship between your localhost computer and where the mail server is at? Are they on the same local network AND the mail server has been configured to trust the IP address of your localhost web server or are they on completely separate networks, such as your mail server is at your ISP or a web host and your localhost computer is at your office or home?

 

A general purpose solution would be to use one of the popular php mailer classes and use SMTP Authentication (i.e. supply a username/password for a mail box on that mail server), because the php mail() function does not support SMTP Authentication.

Link to comment
Share on other sites

But I do have an email server from another server.

 

Network-wise, what is the relationship between your localhost computer and where the mail server is at? Are they on the same local network AND the mail server has been configured to trust the IP address of your localhost web server or are they on completely separate networks, such as your mail server is at your ISP or a web host and your localhost computer is at your office or home?

 

A general purpose solution would be to use one of the popular php mailer classes and use SMTP Authentication (i.e. supply a username/password for a mail box on that mail server), because the php mail() function does not support SMTP Authentication.

 

 

I don't have a localhost server that is setup here right now and there's no relation with the one that I will be using which is at my web host.

 

So to make it clear, I would be using solely the mail server which is also on my webhost.

 

I have tried searching it on Google but it is giving a simple e-mail form w/out SMTP authentication(username & pass) like what you're saying..

 

 

Can you point me out or link me to php mailer classes that you're talking?

 

 

Thank you very much!

Link to comment
Share on other sites

I would be using solely the mail server which is also on my webhost.

 

Then you should consult with them/their FAQ as to the hostname and port number that you should be using in your php script. If the default values in the php.ini (localhost, port 26) didn't connect to a valid mail server, then either the mail server was down at the time you tried or you are expected to set the "SMTP" and "smtp_port" settings to the correct values using ini_set() statements in your script, as the error message suggests.

Link to comment
Share on other sites

I would be using solely the mail server which is also on my webhost.

 

Then you should consult with them/their FAQ as to the hostname and port number that you should be using in your php script. If the default values in the php.ini (localhost, port 26) didn't connect to a valid mail server, then either the mail server was down at the time you tried or you are expected to set the "SMTP" and "smtp_port" settings to the correct values using ini_set() statements in your script, as the error message suggests.

 

 

I have already contacted them.. and they said it is correct. Port 26 is right and my php.ini smtp is also 26.

 

If we are going to base it on my code... notice that there's no password authentication there? maybe that's the cause of the problem?

 

Though I don't know how and where to put it there..

Link to comment
Share on other sites

The error message you posted at the start of this thread is from a Windows/wamp system. Did that error message come from a php script running on your web host?

 

If you are not using SMTP Authentication and the mail server requires it, you would be likely (but not always) getting errors indicating permission problems/authentication required/relaying restrictions... and your web host would have probably told you if you need to SMTP Authentication. There error message you did get indicates that there is no mail server listening on that port number (or it was not running) on the localhost computer where the web server/php is running at.

Link to comment
Share on other sites

The error message you posted at the start of this thread is from a Windows/wamp system. Did that error message come from a php script running on your web host?

 

If you are not using SMTP Authentication and the mail server requires it, you would be likely (but not always) getting errors indicating permission problems/authentication required/relaying restrictions... and your web host would have probably told you if you need to SMTP Authentication. There error message you did get indicates that there is no mail server listening on that port number (or it was not running) on the localhost computer where the web server/php is running at.

 

 

Alright!

 

So the easiest way to do it is by using PEAR Net_SMTP class? and I need to install it right?

 

Is there anyway possible you can code an e-mail form with a smpt authentication without using 3rd party class?

Link to comment
Share on other sites

So the easiest way to do it is by using PEAR Net_SMTP class? and I need to install it right?

 

No. That's why Muddy_Funster suggested doing so would be masochistic.

 

You first need to determine what requirements there are and why it is not working now. You could go through the trouble of using a phpmailer class only to learn that there is a problem with a firewall between your web server and the mail server.

 

Is there anyway possible you can code an e-mail form with a smpt authentication without using 3rd party class?

 

Does your web host have any FAQ information available? If they gave you information about the mail server's hostname, mail.mysamplewebsite.com, you should be using that as the "SMTP" setting instead of 'localhost'.

 

Yes, you can open a socket connect to the mail server and directly send and parse smtp commands and replies (this is what the phpmailer classes do for you.)

Link to comment
Share on other sites

Not if it's windows baised, PHP sendmail can handle SMTP authentication on a linux server, by altering the php.ini file to include it, but it's not supported in a windows environment.  And PEAR Net_SMTP isn't even close to the easiest way to get the job done (in fact I don't think they could see each other with a telescope!), using some well documented third party stuff would probably be easier. 

 

If you do opt for the PEAR rout you'll find a heavily commented mail function on my blog page in my sig that you are free to use and abuse to suit your needs, it should take a little bit of the pain out of it (not much mind you).

Link to comment
Share on other sites

good luck with that, I hope your running on a Linux server, because (and I speek from experience) there just isn't an easy way to code it yourself on Windows.

 

Hey I totally agree. It was really a pain in the @ss. I thought this is just a simple in php.

 

Anyway I finally get it working with smtp authentication, though the feature is just sending an e-mail to me using my own e-mail(to) and my another email (from). So basically visitors will just input their information the feedback form page and it will send it to me.

 

This is the code that I used:

 

<?php  
//new function  

$to = "you@your-domainname.com";  
$nameto = "Who To";  
$from = "script@your-domainname.com";  
$namefrom = "Who From";  
$subject = "Hello World Again!";  
$message = "World, Hello!";  
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);  
?>  


<?php  
/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */   

//This will send an email using auth smtp and output a log array  
//logArray - connection,   

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)  
{  
//SMTP + SERVER DETAILS  
/* * * * CONFIGURATION START * * * */ 
$smtpServer = "mail.ukdns.biz";  
$port = "25";  
$timeout = "30";  
$username = "your-email-address@domain.com";  
$password = "Your-POP3-Password";  
$localhost = "mail.ukdns.biz";  
$newLine = "\r\n";  
/* * * * CONFIGURATION END * * * * */ 

//Connect to the host on the specified port  
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);  
$smtpResponse = fgets($smtpConnect, 515);  
if(empty($smtpConnect))   
{  
$output = "Failed to connect: $smtpResponse";  
return $output;  
}  
else 
{  
$logArray['connection'] = "Connected: $smtpResponse";  
}  

//Request Auth Login  
fputs($smtpConnect,"AUTH LOGIN" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authrequest'] = "$smtpResponse";  

//Send username  
fputs($smtpConnect, base64_encode($username) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authusername'] = "$smtpResponse";  

//Send password  
fputs($smtpConnect, base64_encode($password) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authpassword'] = "$smtpResponse";  

//Say Hello to SMTP  
fputs($smtpConnect, "HELO $localhost" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['heloresponse'] = "$smtpResponse";  

//Email From  
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailfromresponse'] = "$smtpResponse";  

//Email To  
fputs($smtpConnect, "RCPT TO: $to" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailtoresponse'] = "$smtpResponse";  

//The Email  
fputs($smtpConnect, "DATA" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data1response'] = "$smtpResponse";  

//Construct Headers  
$headers = "MIME-Version: 1.0" . $newLine;  
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
$headers .= "To: $nameto <$to>" . $newLine;  
$headers .= "From: $namefrom <$from>" . $newLine;  

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data2response'] = "$smtpResponse";  

// Say Bye to SMTP  
fputs($smtpConnect,"QUIT" . $newLine);   
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['quitresponse'] = "$smtpResponse";   

//insert var_dump here -- uncomment out the next line for debug info
//var_dump($logArray);
}  
?>  

 

 

 

 

What do you think of it? anyone?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.