954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot send mail through php

Can you take a look at this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$emai=$_REQUEST['txtemai'];
$subj=$_REQUEST['txtsubj'];
$mess=$_REQUEST['txtmess'];
if (@mail($emai,$subj,$mess)) {
 echo('<p>Mail sent successfully.</p>');
} else {
 echo('<p>Mail could not be sent.</p>');
} 
?>
</body>
</html>


I keep on getting the else statements.

Here's the code from the form page this email page works with:

<form method="post" action="Emailed.php">
        <input name="txtemai" type="text" id="textfield" size="75" />
      </p>
      <p> Subject:        </p>
      <p>
        <input name="txtsubj" type="text" id="textfield" size="75" />
                  </p>
      <p>Message:
        <textarea name="txtmess" cols="87" rows="15" id="textarea"></textarea>
           
        <input name="button" id="button" type="submit" value="Send" />
         </form>
      </p>


Here is my php.ini in the mail portion of it:

[mail function]
; For Win32 only.
SMTP = smtp.va.com
smtp_port = 25

; For Win32 only.
sendmail_from = [email]Admin@va.com[/email]

I am using Apache 2.2.8 and Win XP Pro SP2

Can anyone find a solution why I can't send emails to my test address in yahoo.com?

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Is your smtp.va.com server a different server ? If so, are you sure it supports open relay smtp. Normally you would need to authenticate to able to send email. If it is the same server try localhost instead.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
Is your smtp.va.com server a different server ? If so, are you sure it supports open relay smtp. Normally you would need to authenticate to able to send email. If it is the same server try localhost instead.

I'm very sorry for the confusion but I'm not not yet live so I'm obliged to use localhost but it doesn't work it seems my problem is authentication. How do I solve this?

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

I don't think mail() supports authentication. You can use the PHPMailer package. It's easy to use and well documented. You can find it on SourceForge.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
I don't think mail() supports authentication. You can use the PHPMailer package. It's easy to use and well documented. You can find it on SourceForge.

I tried your suggestion and took the very first code I laid my eyes on:

<?php
require_once("PHPMailer/class.phpmailer.php");
$mail=new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->From = "****@****.com";
$mail->AddAddress("****@yahoo.com");

$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Message has been sent.';
}
?>


Output: Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.

I still seem to be encountering the problem I was having with vanilla mail.

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

You may need to add:

$mail->SMTPAuth = true;
$mail->Username = "XXX";
$mail->Password = "YYY";
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

You may need to add:

$mail->SMTPAuth = true;
$mail->Username = "XXX";
$mail->Password = "YYY";

I added the lines, and just to play on the safe side I used my gmail account

<?php
require_once("PHPMailer/class.phpmailer.php");
$mail=new PHPMailer();
$mail->IsSMTP();
$mail->Host = "ssl://smtp.gmail.com:465"; 
$mail->SMTPAuth = true;
$mail->Username = '****@gmail.com';
$mail->Password = "*****";
$mail->From = "*****s@gmail.com";
$mail->AddAddress("*****@yahoo.com");

$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Message has been sent.';
}
?>


Output: It didn't output anything!

I checked my test address at yahoo and no emails were added :(

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

$mail->Host = "smtp.gmail.com";

and I think

$mail->Port = 465;

But you'll have to check the manual.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

$mail->Host = "smtp.gmail.com";

and I think

$mail->Port = 465;

But you'll have to check the manual.

still nothing. Any ideas? I'm still open for suggestions

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
$mail->SMTPSecure = "ssl";


This stuff can all be found by googling for it....
Can you be more specific about the error, perhaps it is something completely different.
Maybe try this one instead: http://www.klenwell.com/is/PhpGmailMailer

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hi or you could have a go at this one

<?php
function spamcheck($field){
if (eregi("to:",$field) || eregi("cc:",$field))
{	return TRUE;
}else{	
return FALSE;
}
}
if (isset($_REQUEST['email']))
{
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==TRUE){
echo "Invalid input.";
}else{	
$name = $_REQUEST['name'];	
$email = $_REQUEST['email'];	
$subject = $_REQUEST['subject'];	
$message = $_REQUEST['message'];	
mail("email@email", "Subject: $subject", 	
$message, "From: $email"); 		
echo "Thanks. We'll be in touch soon.";
}
}else{
echo 
"<form method='post' action='contact.php'>

Name:
<input type='text' name='name'>

Email:<input type='text' name='email'>

Subject:<input type='text' name='subject'>
Message:
<textarea name='message' rows='15' cols='40'>
</textarea>

<input type='submit' />
</form>";
}
?>

Harrence

harrence
Newbie Poster
14 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

I've tried all your suggestions and its still not working. Maybe tomorrow I'll find out what the problem is since I'm going live tomorrow. For the meantime, I'm open for more suggestions.

This problem is harder than I thought :)

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Turn on all errors and look what's in the error-log.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
Turn on all errors and look what's in the error-log.

Can you advise me as to the methods of how to turn on all errors and point me to the location of the error log. As I would like to point out, I am extremely new to PHP. :icon_question:

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

It is well documented within the PHP.INI file. All the info your need is there. Just follow the instructions therein.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

The error that you are getting may not be exact, there is a whole other error system in class.smtp.php and you can see where the error accumulates around line 1395.

function setError($msg)
{
    $this->error_count++;
    $this->ErrorInfo = $msg;
}


If you can find a way to echo this variable "$this->ErrorInfo" inside class.smtp.php upon failure you will know at what point the smtp class is failing which will give you the exact error message you are looking for.

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

If you are not live at present, do you have a local server like mamp or zamp?
You can't execute php without a proper server. A browser locally won't do it.

Best harrence.

harrence
Newbie Poster
14 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

If you are not live at present, do you have a local server like mamp or zamp? You can't execute php without a proper server. A browser locally won't do it.

Best harrence.

If you meant a http server, I have Apache 2.2

Andrew F.
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Are you running all your php files through your apache server?
With MAMP(for mac) or ZAMP(for windows) all your php files
need to come from ht.docs and your local server (MAMP or ZAMP)
must be running. Does your remote server support php?
Apologise if you know all this already. Just trying to get a picture.

H

harrence
Newbie Poster
14 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You