Link to home
Start Free TrialLog in
Avatar of fwstealer
fwstealerFlag for United States of America

asked on

send php email

I'm trying to send an email from php and I'm not having much luck. I'm using Vista, Apache and PHP. Below is my sample code. I even tried the code on a Linux server that is capable of send emails and had no luck. Any suggestions?
other php code above
 
//send email
 $mailcheck = spamcheck($txtbxEmail);
	 if ($mailcheck==FALSE)
		{
		echo "Invalid input; mail not sent";
		}
	  else
		{//send email
			$mailTo = $txtbxEmail;
			$mailFrom = "sales@me.com";
			$mailSubject = "test";
			$mailMessage = "hello test";
			$headers = "From: $mailFrom";
			$headers .= "BCC: admin@me.com\r\n";
			try
			{
				$mailsend = mail($mailTo, $mailSubject, $mailMessage, $headers);
			}
			catch(Exception $e)
			{
				echo 'Message: ' .$e->getMessage();
			}
		
			if ($mailsend == 1)
			{
				echo "mail sent <br />";
			}
			else
			{
				echo "mail not sent <br />"; // I get this all the time.
			}
		}
}
 
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
 
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

Open in new window

Avatar of racmail2001
racmail2001
Flag of Romania image

from your localhost it's harder to send emails
usualy this it's working when you put your script on a server with a domain

for the localhost you have to try to install smtp server i think
or use phpmailer http://sourceforge.net/projects/phpmailer

after that you have to make changes on php.ini for the smtp setings
Avatar of fwstealer

ASKER

i tried phpmailer and got an instantiation error - couldn't connect
blows up here
$mail             = new PHPMailer(); // defaults to using php "mail()"
wow that seems complex just to send an email
usualy the hosting company has all this configured. but you want to make it work from your local machine.

so you need to make the configurations :D
right; what is need to work from the hosting company?
ASKER CERTIFIED SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Remember to change the email from and the $email and 'Reply-To' lines to make them variables so you can set them elsewhere on another page if you like. I just tested the code and recieved an email by putting my info in the right sections. Let me know how it goes ok.

Cheers
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how do i bcc to an email address?
i noticed in message there are no line breaks. How do I get the copy to break on separate lines?
nervermind on the bcc - figured it out
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial