how to cc users email address PHP send mail

FDMilwaukee
FDMilwaukee used Ask the Experts™
on
Hello PHP Experts,

I am trying to carbon copy the sender, and can't figure it out.  I am using the script below send an html formatted email.  So to be more clear.  A user is entering all this data in an HTML form on the website.  I want a the email address they enter to be carbon copied.

I am a PHP novice BTW

Thanks in advance

FDM
<?php
$to = "myemail@anemailaddress.com";
$subject = "Eagle Movers LOA";

$message = "
<html>
<head>
<title>Eagle Movers LOA</title>
</head>
<body>
<table width='800' border='0' cellspacing='0' cellpadding='0' style='font-family:Geneva, Arial, Helvetica, sans-serif'>
  <tr>
    <td style='font-size:30px; color:red; font-weight:bold; font-family:Arial, Helvetica, sans-serif; line-height:36px; vertical-align:bottom'>Milwaukee County<br /><span style='font-size:150%'>Letter of Authority</span></td>
    <td> <img src='http://www.moves.net/loa_logo.jpg' width='370' height='163' alt='Eagle Movers 414-383-1776' />
    </td>
  </tr>
    <tr>
    <td colspan='2'><hr /></td>
  </tr>
  <tr>
    <tr>
    <td>&nbsp;</td>
    <td style='text-align:right'>LOA ID:___________________</td>
  </tr>
    <td>This shall serve as a letter of authority for the eviction move of:</td>
  </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td style='color:red; font-weight:bold; font-size:16px;'>LANDLORD/AGENT INFORATION:</td>
    <td style='color:red; font-weight:bold; font-size:16px;'>TENANT INFORMATION:</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Letter Type: ".$_POST[ 'type' ]."</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>



</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <tensec@yahoo.com>' . "\r\n";
// THIS IS WHERE I AM HAVING THE PROBLEM XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
$headers .= 'Cc: tensec@yahoo.com . "\r\n";

mail($to,$subject,$message,$headers);
header( "Location: /thankyou.html" );
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Lines 143 to the end... Try this change and let us know if it helped.
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";

// More headers
$headers .= 'From: <tensec@yahoo.com>' . "\n";
// THIS IS WHERE I AM HAVING THE PROBLEM XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
$headers .= 'Cc: tensec@yahoo.com' . "\r\n";

mail($to,$subject,$message,$headers);
header( "Location: /thankyou.html" );
?>

Open in new window

Author

Commented:
no you're not understanding my problem.  The mail sends fine, and the way it is set up it is sending the cc to the email address tensec@yahoo.com

I need to know how to replace tensec@yahoo.com with name="email" from my html form

for example on line 36 I have ".$_POST[ 'type' ]." which outputs the name="type" input form the html form  I need to know how to put name="email" into that cc area

Thanks
Most Valuable Expert 2011
Top Expert 2016

Commented:
Hmm... I thought this line 150 would throw a parse error - mismatched quotes.

$headers .= 'Cc: tensec@yahoo.com . "\r\n";

I'll show you how to get information from the HTML form to the $_POST array in the action script.  Back in a moment...
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Most Valuable Expert 2011
Top Expert 2016
Commented:
Safety first.  If you are using an email address from an external source like a form input, you need to validate it.  This function will do that for you.  The code snippet also shows how to get the data from the form into the action script.  Please read it over and see if the design pattern makes sense to you, then post back with any specific questions.

Your line 150 would look something like this, assuming your email validation passed successfully:

$headers .= "Cc: {$_POST["email"]}\r\n";


Best, ~Ray
<?php // RAY_temp_email_validation.php
error_reporting(E_ALL);



// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
function check_valid_email($email)
{
    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    // MAN PAGE: http://us3.php.net/manual/en/intro.filter.php
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }
    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                       // START REGEX DELIMITER
        . '^'                       // START STRING
        . '[A-Z0-9_-]'              // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'            // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                       // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+' // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                 // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                   // TLD LENGTH >= 2 AND =< 6
        . '$'                       // ENDOF STRING
        . '/'                       // ENDOF REGEX DELIMITER
        . 'i'                       // CASE INSENSITIVE
        ;
        if (!preg_match($regex, $email)) return FALSE;
    }

    // FILTER or PREG DOES NOT TEST IF THE DOMAIN OF THE EMAIL ADDRESS IS ROUTABLE
    $domain = explode('@', $email);
    if ( checkdnsrr($domain[1],"MX") || checkdnsrr($domain[1],"A") ) return TRUE;

    // EMAIL NOT ROUTABLE
    return FALSE;
}




// DEMONSTRATE THE FUNCTION IN ACTION
$e = '';
if (!empty($_POST["e"]))
{
    $e = $_POST["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    } else
    {
        echo "<br/>BOGUS: $e \n";
    }
}
// END OF PHP - PUT UP THE FORM
?>
<form method="post">
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="<?php echo $e; ?>" />
<input type="submit" />
</form>

Open in new window

Author

Commented:
Good to Go!!!

Thanks Ray!
Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points!  Glad I could help, ~Ray

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