Problem with Sending Mail by PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
11Developer
Forum Newbie
Posts: 1
Joined: Sat Oct 31, 2009 5:14 am

Problem with Sending Mail by PHP

Post by 11Developer »

hello all,
I m new to php, can u plz provide me the code to send mail by php having CAPTCHA code in the form.
i m using the following code :

career.php

Code: Select all

 
 
<?php include('includes/logged_in.php'); ?>
<?php session_start(); ?>
<?php if( isset($_POST['submit']))
        {
        if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) )
            {
            [b]header ("Location: careers_mail.php");[/b]
             die();
            echo 'you have provided an valid security code';
            } 
            else {
            // Insert your code for showing an error message here
            echo 'Sorry, you have provided an invalid security code';
                }
        } 
?>
 
<html>
<body topmargin="0"> 
 
<?php include('includes/masthead.php'); ?>
<?php include('includes/left_navig.php'); ?>
 
       <?php
    if ((isset($_GET['id'])) && ($_GET['id']=='1')) 
        {
            echo '<span class="demoheads">Thanks for filling up the form. We will process your request at the earliest.</span>';
        }
    else
        {           
    ?> 
 
<form method="post" action="" name="dataform" > 
                   //controls
  <table>
       <tr bgcolor="#FFFFFF" align="left"> 
                        <td valign="top">Word Verification Image:</td> 
                        <td valign="top"><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /></td> 
                      </tr> 
 
    <tr bgcolor="#FFFFFF" align="left"> 
                        <td valign="top">Type the characters you see in the picture:</td> 
                        <td valign="top"><input id="security_code" name="security_code" type="text" /></td> 
         </tr>
 
   <tr align="center" bgcolor="#CCCCCC"> 
     <td colspan="2" valign="middle"><input name="submit" onClick="return set();"  type="submit" class="button" value="Submit"> 
    </td> 
   </tr> 
  </table>
</form> 
 
     <?php
        }
    ?>
 
 <?php include('includes/footer.php'); ?> 
 
 
career_mail.php

Code: Select all

 
<?php 
if ($_POST['action'] == 'submitted_info_career')
    {
        if(!empty($_SERVER['HTTP_REFERER']))
            { 
                $referer = $_SERVER['HTTP_REFERER']; 
            }
        else
        {
            $referer = 'None'; 
        }
 
        
        "Resume:\n==============================================================\n";
        
 
        $to1  = "mycompany <mycompany@mycompany.com>".",";
        
        $headers1 = "From: mycompany <info@mycompany.com>\r\n";
        mail($to1, "mycompany" , $msg ,$headers1);
 
        $URL = "careers.php?id=1";
        header ("Location: $URL");
    }
else 
    {
        header ("Location: careers.php");
    }
?>
 
CaptchaSecurityImages.php

Code: Select all

 
<?php
session_start();
 
/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/a ... aptcha.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/
 
class CaptchaSecurityImages {
 
    var $font = 'monofont.ttf';
 
    function generateCode($characters) {
        /* list all possible characters, similar looking characters and vowels have been removed */
        $possible = '23456789bcdfghjkmnpqrstvwxyz';
        $code = '';
        $i = 0;
        while ($i < $characters) { 
            $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
            $i++;
        }
        return $code;
    }
 
    function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
        $code = $this->generateCode($characters);
        /* font size will be 75% of the image height */
        $font_size = $height * 0.75;
        $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);
        $text_color = imagecolorallocate($image, 20, 40, 100);
        $noise_color = imagecolorallocate($image, 100, 120, 180);
        /* generate random dots in background */
        for( $i=0; $i<($width*$height)/3; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
        }
        /* generate random lines in background */
        for( $i=0; $i<($width*$height)/150; $i++ ) {
            imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
        }
        /* create textbox and add text */
        $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
        $x = ($width - $textbox[4])/2;
        $y = ($height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
        /* output captcha image to browser */
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
        $_SESSION['security_code'] = $code;
    }
 
}
 
$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
 
$captcha = new CaptchaSecurityImages($width,$height,$characters);
 
?>
 
when i run this code it shows error like : headers already sent

i dont know abcd of php, so plz plz plz help me......i dont know where i m getting wrong.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Problem with Sending Mail by PHP

Post by markusn00b »

You should be calling functions that need to send headers to the browser (in your case, session_start()) before you send any output (HTML, text, etc.) to the browser. Consider the following:

Bad.php

Code: Select all

 
print 'Starting the session...';
session_start();
 
Because, via the print language construct, you have sent output to the browser (and thus, any headers that PHP had buffered to send) you cannot use any functions that also need to send information (headers) to the browser.

Good.php

Code: Select all

 
session_start();
print 'Session started...';
 
You could use output buffering, but, to me, this seems like a cop-out and you should consider structuring your application better.

Mark.
Post Reply