We help IT Professionals succeed at work.

php send mail

doctorbill
doctorbill asked
on
Thsi code works fine for sending email if the "From" field is filled in
It does not work at all if the "From" and "CC" field s are filled in
Could someone please explain:

--------------------------------------------------

<?php

session_start();

if ($_POST['email']) {

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

if ($_POST['cc']) {
            $headers .= "Cc: $_POST[cc]" . "\r\n";
}            

            $email_to = $_POST['email'];
                $subject = "Support Links from TickTockIT";

            $headers .= 'From: Tick Tock IT <info@ticktockit.net>' . "\r\n";            

            ob_start();
                  include('email-content.php');
                  $message = ob_get_contents();
            ob_end_clean();

            mail($email_to, $subject, $message, $headers);

$_SESSION['sent'] = 1;
header('Location: send-email_b.php'); die;
}

?>

---------------------------------------
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Try this.
$headers .= "Cc: ".$_POST['cc']. "\r\n";

Open in new window

Author

Commented:
Sorry - still not doing it
Same problem
Most Valuable Expert 2011
Top Expert 2016
Commented:
Please give us a little more clue than "works" or "not."  When it fails, is there any error message?  Is there any line number?  If we can get a few more of these important clues we may be able to help.

Author

Commented:
If the "From" email firld is filled in then the email is sent
If the "From" email field and the "CC" email field are both filled in then nothing is sent
No error message - the script runs and the "your message has been sent" is displayed on my screen
I have attached the full page below
send-email-b.php
Fixer of Problems
Most Valuable Expert 2014
Commented:
After I killed the 'require_once' lines, I still get...

PHP Notice: Undefined index: email in C:\Inetpub\wwwroot\ee2\drbill\send-email-b.php on line 5

The first time the page is run, 'email' is undefined.

This version works to send email to both the 'To:' and the 'Cc:' after I killed the 'require_once' lines.  You can change it back to your email address.
<?php

session_start();

if (isset($_POST['email'])) {

		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

if ($_POST['cc']) {
		$headers .= "Cc: ".$_POST['cc']. "\r\n";
	}		

		$email_to = $_POST['email'];
    $subject = "Support Links from TickTockIT";

		//$headers .= 'From: Tick Tock IT <info@ticktockit.net>' . "\r\n";		
		$headers .= 'From: Dave <dave@dibsplace.com>' . "\r\n";		

		ob_start();
			include('email-content.php');
			$message = ob_get_contents();
		ob_end_clean();

		mail($email_to, $subject, $message, $headers);

$_SESSION['sent'] = 1;
//header('Location: send-email_b.php'); die;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Send Email</title>
</head>

<body>
<?php //require_once('../../../TickTock_int/menu_mailer.html'); ?>
<div align="center"><?php if (isset($_SESSION['sent'])) { ?>
Your message has been sent.
<?php } ?></div>

<br />
<br />
<div align="center">
<div style="width:350px;" align="left">
<center><?php //require_once('logo_header.php'); ?>Logo</center>

<form id="form1" name="form1" method="post" action="">
 
  <left>Contact email address:&nbsp;</em><input name="email" type="text" id="email" /></left>
  <br />
  <br />
  
  <left><em>CC:&nbsp;</em><input name="cc" type="text" id="cc" /></left>
  <br />
  <br />
  <input type="submit" name="Submit" value="Submit" style="float:right;"/>
</form></div>
</div>
</body>
</html>

<?php unset($_SESSION['sent']); ?>

Open in new window

Author

Commented:
I used your code bbut with my email address
Still the same problem

Could this be a php.ini config issue ?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
I don't see how unless you don't have a SMTP server defined.  If you used my code, you should end up back on the same page because I commented out the 'header' redirect line.

Author

Commented:
It is working now
It looks as if one of the cc email address I was using was not being recognised correctly

Thanks all

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 125 points for Dave Baldwin's comment #a40694106
Assisted answer: 125 points for Dave Baldwin's comment #a40694076
Assisted answer: 125 points for Ray Paseur's comment #a40694098
Assisted answer: 0 points for doctorbill's comment #a40694121
Assisted answer: 125 points for Dave Baldwin's comment #a40694127

for the following reason:

Solution

Author

Commented:
Solution