We help IT Professionals succeed at work.

Problems using ISP SMTP server for PHP's Mail() function

primalfear1 asked
Medium Priority
Last Modified: 2008-02-07
I am trying to send an email to my email address using the mail() function, inside of a php form.

I am using a basic form... i pasted the code below.
This script and my server is on windows xp pro.


<form action="" method="post">
Name: <br>
<input type="text" name="name">
Email: <br>
<input type="text" name="email">
Subject: <br>
<input type="text" name="subject">
<textarea name="message" cols="" rows=""></textarea><br>
<input type="submit" name="submit" value="Send">
<input type="reset" name="Reset" value="Reset">

Copy the html

if ($submit) { // if the form was sent do the following

if($name && $subject && $email && $message ) { // if all field are filled-in send email
mail("primalfear@comcast.net","$subject","$message","From: $name <$email>") or die("email error");
echo "Message Sent"; // if all went well, display message was sent
} else {
echo "All fields must be filled in!<BR>"; // if not all were filled in, display error message
} // end php submission code


I changed my php.ini file to the following.

[mail function]
; For Win32 only.
SMTP = smtp.comcast.net
; For Win32 only.
sendmail_from = me@comcast.net
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path = c:/apache2/mail/bin/sendmail.exe

I changed the me@comcast.net to my email address...

Does anyone know why it does not work?
Is there some smtp code to change the smtp server to something other than localhost?

I would appreciate any help.



When i tried using a CGI script to send an email to the email specified in the form it worked.. so im pretty sure that my php.ini file is ok.
I even tried changing the smtp server to localhost

I also tried changing the recipitant email address from primalfear@comcast.net (or w/e it is) to admin@localhost.

When i did that, the little norton antivirus checked popped up (as is what happens when any email is being sent from my computer)  but, i need to send it to a real email address...  not admin@localhost.  

The goal of this is to create a validation script.. where they fill in their email and then the form sends an email to the email they specified.  But i already know how to do all that.. its figuring out my question above which is the important part.

I am running on a windows platform.

Watch Question

What EXACTLY happens when you put your primalfear@comcast.net email address?

Does it die saying 'email error' or it outputs 'Message Sent' but doesnt send the actual message.

If it dies saying 'email error'. We need to look in to your PHP code but if it doesn't it means that something is wrong with your SMTP server.

These days, SMTP servers are configured not to 'relay' mails. In that case, the webserver user will not be able to send mails through SMTP server outside the host which SMTP is serving. You may need to authenticate with SMTP server with valid credentials before you can send your mail to 'real' email address. To better interact with SMTP server directly with PHP, Try phpmailer from sourceforge. Go to http://phpmailer.sourceforge.net/

The example code (from phpmailer site) which allows you to authenticate yourself with SMTP server is provided below:

$mail = new PHPMailer();

$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password

$mail->From     = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams");
$mail->AddAddress("ellen@site.com");               // optional name

$mail->WordWrap = 50;                              // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true);                               // send as HTML

$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the <b>HTML body</b>";
$mail->AltBody  =  "This is the text-only body";

   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;

echo "Message has been sent";

Trying phpmailer along with above mentioned code will certainly give you very clear picture about what is happening, where it is going wrong and what is going wrong.

All the best!
Oh, I see. You mentioned that you are trying to use ISP's SMTP server. It's a sure case now that your SMTP server requires you to authenticate before it can send mail to outside world.


$mail->Host     = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password

above lines in previous code with

Your smtp server name, put your username/password which you use for corresponding POP account at your ISP. It should work.


Explore More ContentExplore courses, solutions, and other research materials related to this topic.