Sending multiple CCs and BCCs with PHP PEAR MAIL

I have a project that I am working on and am using Pear mailing. I need to use smtp because we need to track everything from our mail server. And users should be able to log in before sending company email. We cannot use php mail function for this.

My problem is that I cannot find any documentation on the net for submitting CC and Bcc, nor for submitting multiple BCCs. It's very easy to do this with php 'mail funciton. All you do is add it to the $ header variable like so:

$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

      

This is my code for a php function where I am using PEAR

function sender_mail($email,$subject,$mailmsg, $cc, $bcc){

    include("Mail.php");
    /* mail setup recipients, subject etc */

    //DEFAULT VALUE OF FROM 
    $from = "noreply@addata.net";

    //GET EMAIL OF USER
    $result = mysql_query("SELECT email, email_pass FROM u_perinfo WHERE user_id = '$_SESSION[uid]'")
    or die("There was an error when grabbing your email information");
    if(mysql_num_rows($result) > 0){
        $row = mysql_fetch_array($result);
        if($row[0] != ''){
            $from = $row[0];
        }
        $email_pass = $row[1];
    }

    $recipients = "$email";
    $headers["From"] = "$from";
    $headers["To"] = "$email";
    $headers["Subject"] = $subject;
    $headers["Cc"] = "$cc";  //Line added by Me to see if it works
    $headers["Bcc"] = "$bcc";  //Line added by Me to see if it works


    //$mailmsg = "Welcome to Addatareference.com! \r\n\r\nBelow is your unique login information.  \r\n\r\n(Please do not share your login information.)$accountinfo";
    /* SMTP server name, port, user/passwd */
    $smtpinfo["host"] = "smtp.emailsrvr.com";
    $smtpinfo["port"] = "25";
    $smtpinfo["auth"] = true;
    $smtpinfo["username"] = "$from";
    $smtpinfo["password"] = "$email_pass";
    /* Create the mail object using the Mail::factory method */
    $mail_object =& Mail::factory("smtp", $smtpinfo);
    /* Ok send mail */
    $mail_object->send($recipients, $headers, $mailmsg);

}

      

I am trying to find a solution to this, with no real information coming back. If someone could help me I would be very grateful.

+13


source to share


3 answers


Have you tried adding multiple addresses "," are separated?

$headers['Cc'] = 'cc@example.com, bb@example.com, dd@ex.com';

      



This might work as per line 218 in the source .

+14


source


There is a problem with Bcc though when using the PEAR Mail: Thunderbird feature displays the Bcc header so the recipient is not hidden, which is a whole bunch of Bcc. It also appears in the To: header list (since you need to enable the Bcc list for recipients).

Edit: SOLVED - I found from another site that the solution is only to include the Bcc list in the $ recipients and not in any of the headers. He works!

Thus:



$bcc = "foo@example.com";
$to = "bar@example.com";
$headers = array(..other stuff.., 'To' => $to, ..rest of header stuff); // No Bcc header!
$recipients = $to.", ".$bcc;
$mail = $smtp->send($recipients, $headers, $message);

      

Edit # 2: Acknowledge my source - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/

+25


source


You just need to add everything in, cc, bcc to the $ recipients variable. The header decides where to send.

+3


source







All Articles