send php email

fwstealer
fwstealer used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Author

Commented:
i tried phpmailer and got an instantiation error - couldn't connect

Author

Commented:
blows up here
$mail             = new PHPMailer(); // defaults to using php "mail()"
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
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

Author

Commented:
right; what is need to work from the hosting company?
Principal Data Engineer
Commented:
Here's how I use send mail() on my localhost (laptop) server. My server does not have a domain aither it's just a local server so I can develope websites at home. All I have running is WampServer2.0 which has Php V5.2.9-1 and it has apache 2.2.11

<?php

/**
 * @author elvin66@xtra.co.nz
 * @copyright 2009
 */

//<!-- Generate the email <!-->

$email = "someone@somewhere.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: you@yourplace.com" . "\r\n";
$headers .= "Reply-To: you@yourplace.com" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

$subject = "Subject line goes here";
$message= "Hello "
."This is a test email"
."Goodbye";

if($send_contact = mail($email,$subject,$message,$headers)
){
      echo "Mail sent to ".$email;
}else{
      echo "Failed to send email";
}

?>
Mark BradyPrincipal Data Engineer

Commented:
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
Commented:
The reason you cannot send mail is probably because you don't have an MTA (Mail Transfer Agent) or SMTP server installed.
You need to either install and setup an MTA or configure PHP to use a remote server to send mail (such as on your hosting account).
The main reason it generally works on Linux is because there is usually an MTA installed such as sendmail, postfix, exim etc. Many Linux distros automatically install an MTA as a dependency for LAMP components.

Also, unlike other languages PHP functions do not throw exceptions, your try/catch is doing absolutely nothing in your code. You would need to throw an exception or remove it. Example below of how you can get it to work, it would also make your send check unnecessary so I have moved this into the try/catch as well.

It's also a bit cleaner to extend the exception class such as:

class MailException extends Exception {}

Then you can catch specific exceptions rather than all exceptions, which can be useful in more complicated pieces of code.
try
{
  $mailsend = mail($mailTo, $mailSubject, $mailMessage, $headers);
  if (!$mailsend)
  {
    throw new Exception('Failed to send email.');
  }
  echo 'Mail sent';
}
catch(Exception $e)
{
  echo 'Message: ' .$e->getMessage();
}

Open in new window

Author

Commented:
how do i bcc to an email address?

Author

Commented:
i noticed in message there are no line breaks. How do I get the copy to break on separate lines?

Author

Commented:
nervermind on the bcc - figured it out
Mark BradyPrincipal Data Engineer
Commented:
Sorry fwstealer

Ok so you figured out how to add more headers like CC and BCC right?

As for the line breaks in the actual message just add at the end of each line:  \n\n"

each /n will give you a line break. So $message =  "Hello \n\n"
."This is a test email \n\n\n\n"
."Goodbye";

will give you:
Hello

This is a test email



Goodbye

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial