Important alert: (current site time 7/15/2013 10:46:52 AM EDT)
 

article

_Send E-mail_

Email
Submitted on: 7/12/2004 1:32:07 PM
By: BelgiumBoy_007  
Level: Advanced
User Rating: By 10 Users
Compatibility: PHP 4.0
Views: 28451
author picture
(About the author)
 
     This is a very simple script that will send an e-mail using the standard mail () function. Designed for beginners. I was going to upload it but it didn't work so here's the code.


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
<?php

//  See comments below (right above the form).

  
if ($mode == "submit") {
    if (
$enable_html != "on") {
/*
    The user does not want to send the e-mail in HTML format, so we must get
    rid of the HTML in the message.
*/
      
$mail_body = htmlspecialchars ($mail_body);
    }

//  Now we set the headers for our e-mail message.

    
$headers .= "MIME-Version: 1.0 \n";
    
$headers .= "Content-type: text/html; charset=iso-8859-1 \n";
    
$headers .= "from:$mail_from\r\nCc:$mail_cc\r\nBcc:$mail_bcc";

/*
    Now it's time to send the message, we'll use the mail () function.
    The function will return TRUE on success, FALSE on failure.
    We can use that to make sure the e-mail was sent without
    any problems.
    The "@" in front of the function is optional.  In PHP, whenever
    there is an @ in front of ANY function, the script will not show
    any errors.  As we will let the user know if anything goes wrong
    ourselves, we don't need PHP to give the errors for us and we can
    show the error however we want (not with Warning: ... on line ...).
*/

    
if (@mail ($mail_to, $mail_subject, $mail_body, $headers)) {
      print (
"<h1><font color=\"#004000\">The e-mail was sent successfully!</font></h1>");
    } else {
      print (
"<h1><font color=\"#880000\">An error occurred while sending the e-mail!</font></h1>");
    }

//  We don't need to show the form again.

    
exit;
  }
?>

<html>

<head>
<title>Send e-mail</title>
<script language="javascript">
  function DoSubmit ()
  {
/*
    This javascript will check if the important fields have been filled in.
    The control is pretty basic (if the user types " " it will see it as full,
    but it is enough to alert a person who forgot to fill out a form, without
    him / her having to wait until the form refreshes (good for slow modems).

    The return ""; command will exit the function, so that the form is only
    submitted when it is valid.
*/

    if (document.form.mail_from.value == "") {
      alert ("You forgot to enter the 'from' field.");
      document.form.mail_from.focus ();
      return "";
    }

    if (document.form.mail_to.value == "") {
      alert ("You forgot to enter the 'to' field.");
      document.form.mail_to.focus ();
      return "";
    }

    if (document.form.mail_subject.value == "") {
      alert ("You forgot to enter the 'subject' field.");
      document.form.mail_subject.focus ();
      return "";
    }

    if (document.form.mail_body.value == "") {
      alert ("You forgot to enter the 'body' field.");
      document.form.mail_body.focus ();
      return "";
    }

    document.form.submit ();
  }
</script>
</head>

<body>
<!--
    By setting the form's action to $PHP_SELF, this code will work even
    when you change the name from email.php into whatever you want (.php).

    The included (hidden) field, mode, will be used to see if the user has
    submitted the form or if the page is loading for the first time.  If the
    user has submitted the page, the variable $mode will have the string
    "submit" as its value, otherwise the variable will be empty.
-->
<form action="<?php print ($PHP_SELF); ?>" method="post" name="form">
  <table>
    <tr>
      <td>From:</td>
      <td><input type="text" name="mail_from" size="40"></td>
    </tr>
    <tr>
      <td>To:</td>
      <td><input type="text" name="mail_to" size="40"></td>
    </tr>
    <tr>
      <td>Cc:</td>
      <td><input type="text" name="mail_cc" size="40"></td>
    </tr>
    <tr>
      <td>Bcc:</td>
      <td><input type="text" name="mail_bcc" size="40"></td>
    </tr>
    <tr>
      <td>Subject:</td>
      <td><input type="text" name="mail_subject" size="40"></td>
    </tr>
    <tr>
      <td valign="top">Body:</td>
      <td><textarea name="mail_body" cols="40" rows="10"></textarea></td>
    </tr>
    <tr>
      <td></td>
      <td><input type="checkbox" name="enable_html"> enable HTML in this message.</td>
    </tr>
    <tr>
      <td><input type="hidden" name="mode" value="submit"></td>
      <td><input type="button" onclick="DoSubmit ()" value="Send e-mail"></td>
    </tr>
  </table>
</form>

</body>

</html>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

10/29/2004 5:06:11 AM

send-me
(If this comment was disrespectful, please report it.)

 
12/16/2004 2:17:54 PMUkLeo

Nicely commented, works fine.
(If this comment was disrespectful, please report it.)

 
2/23/2005 8:22:52 PM

Excelente !!!
(If this comment was disrespectful, please report it.)

 
3/18/2005 9:22:24 AM

is there any changes i should make?
(If this comment was disrespectful, please report it.)

 
6/13/2005 8:50:23 AM

i think its better to send it by cronjob and i can use it like news group...and if u can add function to send to group
(If this comment was disrespectful, please report it.)

 
1/10/2006 4:29:32 PMLawrence Cherone

Thanks. good tut
(If this comment was disrespectful, please report it.)

 
8/3/2006 7:47:04 AMGulshan L C

very good i appriciate ur research
(If this comment was disrespectful, please report it.)

 
4/10/2007 11:05:31 PMlinda

I encounter some problem when test it on my server:
for the first time, when download this page, there is a notice:
Notice: Undefined variable: mode in F:\webroot\test\sendemail.php on line 4

then when I try to ignore this notice and send sth to my mail box, it showed an error:
Notice: Undefined variable: enable_html in F:\webroot\test\sendemail.php on line 5

Notice: Undefined variable: headers in F:\webroot\test\sendemail.php on line 15
An error occurred while sending the e-mail!

the line 4 -
if ($mode == "submit") {
the line 5 -
if ($enable_html != "on") {
the line 15 -
$headers .= "MIME-Version: 1.0 \n";


(If this comment was disrespectful, please report it.)

 
4/18/2007 2:58:55 AMGunawan

Im still confiues with this code please explain me about configuration on PHP.ini....
(If this comment was disrespectful, please report it.)

 
12/2/2007 11:14:20 AMmehrdad

ey val
thnks
(If this comment was disrespectful, please report it.)

 
10/29/2009 4:16:21 PMPak Tech Solutions

for me it show nothing and do not send mail when i hit submit page reload and all fields become empty again
(If this comment was disrespectful, please report it.)

 
3/17/2010 5:16:54 PMarpita

I think i hv done some istakes in my config in php.ini file coz...
for the first time, when download this page, there is a notice:
Notice: Undefined variable: mode in F:\webroot\test\sendemail.php on line 4

then when I try to ignore this notice and send sth to my mail box, it showed an error:
Notice: Undefined variable: enable_html in F:\webroot\test\sendemail.php on line 5

Notice: Undefined variable: headers in F:\webroot\test\sendemail.php on line 15
An error occurred while sending the e-mail!

the line 4 -
if ($mode == "submit") {
the line 5 -
if ($enable_html != "on") {
the line 15 -
$headers .= "MIME-Version: 1.0 \n";
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.