Jump to content

Adding Code to enable CC/BCC in a Contact Form


chome4

Recommended Posts

I'm not a PHP programmer but have successfully used PHPMailer to set up a form that works by using pre-written code available out there.

I've been asked to modify a contact form that has an extra empty 'CC' field. I can do that! But how can I make this blank field accept any email address so another person can be copied in. There doesn't seem to be any help, with examples.

Thanks...

Link to comment
Share on other sites

17 hours ago, requinix said:

Would be nice to see some code...

How will the code know what email address should be added in?

Here is the code from a working contact form.

I've seen "foreach ($mail_data['cc'] as $email => $name) {

$driver->addCC($email, $name); }" ...but don't know anything about incorporating this into the code below.

===============================================================================

<?php

    
include 'phpmailer/class.phpmailer.php';
function GetIP(){
    if(getenv("HTTP_CLIENT_IP")) {
         $ip = getenv("HTTP_CLIENT_IP");
     }elseif(getenv("HTTP_X_FORWARDED_FOR")) {
         $ip = getenv("HTTP_X_FORWARDED_FOR");
         if (strstr($ip, ',')) {
             $tmp = explode (',', $ip);
             $ip = trim($tmp[0]);
         }
     }else{
     $ip = getenv("REMOTE_ADDR");
     }
    return $ip;
}

$ip_adress     = GetIP();
$name       = addslashes(strip_tags($_POST['name']));
$sub         = addslashes(strip_tags($_POST['subject']));
$email         = addslashes(strip_tags($_POST['email']));
$message    = addslashes(strip_tags($_POST['message']));
 
 if(empty($name) || empty($email) || empty($message) || empty($sub)){header("Location:form.php?empty"); }else{
 
$mail = new PHPMailer();
$mail->IsSMTP();                                   
$mail->Host     = "smtp.****.com"; // smtp host
$mail->Port     = "587"; //587  // Port
$mail->SMTPAuth = true;    
$mail->Username = ******";  //mail address
$mail->Password = "******"; //email password
$mail->From     = "******"; // from mail address
$mail->Fromname = "no-reply"; // From Name
$mail->AddAddress("*****","no name"); //your mail address and name
$mail->WordWrap     = 50;
$mail->Subject      = $sub; // Mail Subject
$mail->Body         = "    Name : ".$name. "
                        <br>E-mail: ".$email. "
                    <br>Message: ".$message . "
                    <br>IP : ".$ip_adress ;
     

$mail->AddReplyTo($email,"Contact Form");
$mail->AddAddress('******');  //mail address
$mail->IsHTML(true);

if($mail->Send())

echo "";

}

 

?>

Link to comment
Share on other sites

1 hour ago, requinix said:

Have you added those fields to the form yet? What does the HTML look like?

<form action="send.php" class="form-horizontal" id="contactForm" method="post" name="contactForm" style="width:100%">
                                    <div class="form-group">
                                        <label for="name">Name</label><br>
                                        <input id="name" name="name" type="text" placeholder="Your Name"> <span class="alert alert-danger e_name">*Name.</span>
                                    </div>
                                    <div class="form-group">
                                        <label for="email">Email</label><br>
                                        <input id="email" name="email" type="email" placeholder="Your Email"> <span class="alert alert-danger e_email">*Email.</span> <span    class="alert alert-danger e_email-1">*Email Validate !</span>
                                    </div>
                                    
                                    <div class="form-group">
                                        <label for="email">CC</label><br>
                                        <input id="email" name="email" type="email" placeholder="CC (optional)"> <span class="alert alert-danger e_email">*Email.</span> <span class="alert alert-danger e_email-1">*Email Validate !</span>
                                    </div>
                                    
                                    <div class="form-group">
                                        <label for="email">BCC</label><br>
                                        <input id="email" name="email" type="email" placeholder="BCC (optional)"> <span class="alert alert-danger e_email">*Email.</span> <span class="alert alert-danger e_email-1">*Email Validate !</span>
                                    </div>
                                    
                                    <div class="form-group">
                                        <label for="subject">Subject</label><br>
                                        <input id="subject" name="subject" type="text" placeholder="Reason for Email"> <span class="alert alert-danger e_sub">*Subject.</span>
                                    </div>
                                    <div class="form-group">
                                        <label for="subject">Message</label><br>
                                        <textarea class="textinput" cols="40" id="message" name="message" rows="10"></textarea> <span class="alert alert-danger e_mes">*Message.</span>
                                    </div>
                                    <div class="btn btn-info">
                                        <a class="link" href="javascript:gonder();" id="btn">Send</a>
                                    </div>
                                    <div id="info"></div>
                                </form>

Link to comment
Share on other sites

See how all those new inputs are named "email"? That's not good. The name is how PHP knows which field's value to get so they all need to be different.
Also the IDs, for that matter. IDs must be unique on a page.

When those are fixed, post what you have (so we can see the new names) and then try adding some PHP code to get those values. It will be a couple lines that look very much like the lines to get the other fields' values.
Give that a shot and post what you come up with so we can add them into the email message.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.