Connecting Tech Pros Worldwide Help | Site Map

Problems sending email via smtp

Newbie
 
Join Date: Mar 2007
Posts: 9
#1: Mar 16 '07
I'm trying to setup a Form and send it to our helpdesk so that we don't have to email forms all day long. The problem is that I have very little knowledge of PHP and I cannot seem to find the same thing that I am tring to do on the internet so I can compare what exactly is going wrong. Here is my code(leaving a lot of html out to save space):
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$me = $_SERVER['PHP_SELF'];
?>
</div>
<form method="post" name="form1" id="form1" action="<?=$me?>">
<div align="center">
<?
$name = $_POST["name"];
$position = $_POST["position"];
$location = $_POST["location"];
$department = $_POST["department"];
$user_phone = $_POST["user_phone"];
$user_email = $_POST["user_email"];
$manager = $_POST["manager"];
$manager_phone = $_POST["manager_phone"];
$manager_email = $_POST["manager_email"];
$time_zone = $_POST["time_zone"];
$start_date = $_POST["start_date"];
$printer = $_POST["printer"];
$request_type = $_POST["request_type"];
$access_type = $_POST["access_type"];
$model_after_user1 = $_POST["model_after_user1"];
$model_after_1 = $_POST["model_after_1"];
$model_after_user2 = $_POST["model_after_user2"];
$model_after_2 = $_POST["model_after_2"];
$notes = $_POST["notes"];
?>
(htmlcodehere)
</form>
<?php
} else {
error_reporting(0);
$errors = array();
if (!$_POST['name'])
$errors[] = "Name is required";
if (count($errors)>0) {
foreach($errors as $err)
echo "$err<br>\n";
echo "<br>Please use your browser's Back button to fix.";
} else {
$recipient = 'aerosmith_freak_03@yahoo.com';
$from = stripslashes($_POST['name']);
$subject = "User Request";
$msg = "Message sent by $from\n";
$msg.="\nName: ".$_POST['name'];
$msg.="\nPosition: ".$_POST['position'];
$msg.="\nLocation: ".$_POST['location'];
$msg.="\nDepartment: ".$_POST['department'];
$msg.="\nUser's Phone: ".$_POST['users_phone'];
$msg.="\nUser's E-mail: ".$_POST['users_email'];
$msg.="\nManager: ".$_POST['manager'];
$msg.="\nManager's Phone: ".$_POST['managers_phone'];
$msg.="\nManager's Email: ".$_POST['managers_email'];
$msg.="\nTime Zone: ".$_POST['time_zone'];
$msg.="\nStart Date: ".$_POST['start_date'];
$msg.="\nPrinter: ".$_POST['printer'];
$msg.="\nRequest Type: ".$_POST['request_type'];
$msg.="\nPosition: ".$_POST['position'];
$msg.="\nAccess Type: ".$_POST['access_type'];
$msg.="\nModel After User1: ".$_POST['model_after_user1'];
$msg.="\nModel After 1: ".$_POST['model_after_1'];
$msg.="\nModel After User2: ".$_POST['model_after_user2'];
$msg.="\nModel After 2: ".$_POST['model_after_2'];
$msg.="\nNotes: ".$_POST['notes'];
$msg.="\nColor: ".$_POST['color'];
if (mail($recipient,$subject,$msg)){
echo "<p>Your request has been sent</p>";
echo nl2br($msg);
} else
echo "";
}
}
?>
</body>
</html>
If this matters, model_after_1, model_after_2, and access_type come from checkbox groups. location and time_zone come from drop down menus. request_type is a radio button group. The rest come from text boxes.
Thanks
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439
#2: Mar 17 '07

re: Problems sending email via smtp


what is the Error here you are getting.
in your mail() function some headers missing.
Please use [PHP] tags around your coding next time, its hard to read.
Newbie
 
Join Date: Mar 2007
Posts: 9
#3: Mar 19 '07

re: Problems sending email via smtp


Well, that's part of the problem, when I submit the form I don't recieve any errors. The page refreshes like it sent something but I never recieve an email or any real indication that it did something. I added "error_reporting(E_ALL);" to see if that would tell me anything and I get nothing but "Notice: Undefined index: name in F:\intranet\php\index.php on line 27" thru to the "notes" variable. I can send you the whole page if you would like, I just couldn't post it all here Here is a link to it http://docs.google.com/Doc?id=d773v34_0g28tv6
Thanks, Brad
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439
#4: Mar 19 '07

re: Problems sending email via smtp


First of all we'll check whether this one is working or not. change $to variable here in to your email (gmail/yahoo). if you got the "Mail Sent." message then we'll try to fix the original coding.

Check Your Spam box if the mail is not there in your mail inbox.

[PHP]<?php

$to = "your_Mail_id_@gmail.com";//Change this to your Mail Address
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "some1@gmail.com";
$headers = "From: $from";
if(mail($to,$subject,$message,$headers))
{
echo "Mail Sent.";
}else{
echo "Error Occured";
}
?>[/PHP]

if this one is working in your host, send me the HTML form and PHP script to the this forum.
if this one is not working the problem is in your SMTP settings.
Where did you test the application localhost or ??
Newbie
 
Join Date: Mar 2007
Posts: 9
#5: Mar 19 '07

re: Problems sending email via smtp


This didn't work, here is the error I got: "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:\intranet\php\test.php on line 14" [if(mail($to,$subject,$message,$headers))] I have set the php.ini file to point to our mail server (different location) and use the correct ports. This I know is right, I don't agree with this but I was told that I didn't need to specify a username and password for the mail server. I have to specify one when I log on, why wouldn't I have to for this? If that is the problem then where do I specify a username and password?
Newbie
 
Join Date: Mar 2007
Posts: 9
#6: Mar 19 '07

re: Problems sending email via smtp


I found my problem, I thought setting the smtp server in the php.ini file was enough. Apparently not, I added ini_set "("SMTP","XXX.XXX.com");
ini_set("sendmail_from","php@XXX.com");" and that got it. Thanks, without knowing where to start on this one it would have taken me forever to find out what was going on.
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439
#7: Mar 20 '07

re: Problems sending email via smtp


Quote:

Originally Posted by bcanter

I found my problem, I thought setting the smtp server in the php.ini file was enough. Apparently not, I added ini_set "("SMTP","XXX.XXX.com");
ini_set("sendmail_from","php@XXX.com");" and that got it. Thanks, without knowing where to start on this one it would have taken me forever to find out what was going on.

If you configured your php.ini file correctly again no need to go for ini_set().
I did not mentioned your O/S, this is for Windows.
by default there is a ";" of each and every line. that means php.ini file will ignore this line.so we have to remove that.

Eg:
Expand|Select|Wrap|Line Numbers
  1. ;smtp_port = 25
Settings:
Expand|Select|Wrap|Line Numbers
  1. [mail function]
  2. ; For Win32 only.
  3. SMTP = smtp_or_mail.your_ISP.com 
  4. smtp_port = 25
This is the default from Address, but we can change it from mail() function.

Expand|Select|Wrap|Line Numbers
  1. ; For Win32 only.
  2. sendmail_from = whatever@your_ISP.com
Reply