Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Mail adding CC

Tags:

php

email

I have this PHP Mail Function:

if(!function_exists("sendemail"))
{
    function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto)
    {
        if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
        {
            require_once "/usr/local/lib/php/Mail.php";

            $from = $email_from;
            $to = $email_to;
            $subject = $email_subject;
            $body = $email_body;

            $host = "mail.domain.co.uk";
            $username = "[email protected]";
            $password = "*******";

            $headers = array ('From' => $from,
              'To' => $to,
              'Subject' => $subject,
              'Content-type' => 'text/html');
            $smtp = Mail::factory('smtp',
              array ('host' => $host,
             'auth' => true,
             'username' => $username,
             'password' => $password));

            $mail = $smtp->send($to, $headers, $body, $cc);
        }
    }
}

I have added in the ability to CC Email addresses in:

if(!function_exists("sendemail"))
{
    function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc)
    {
        if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
        {
            require_once "/usr/local/lib/php/Mail.php";

            $from = $email_from;
            $to = $email_to;
            $subject = $email_subject;
            $body = $email_body;

            $host = "mail.domain.co.uk";
            $username = "[email protected]";
            $password = "*******";

            $headers = array ('From' => $from,
              'To' => $to,
              'Cc' => $cc,
              'Subject' => $subject,
              'Content-type' => 'text/html');
            $smtp = Mail::factory('smtp',
              array ('host' => $host,
             'auth' => true,
             'username' => $username,
             'password' => $password));

            $mail = $smtp->send($to, $headers, $body, $cc);
        }
    }
}

but it only seems to add the CC'd email address(es) into the header and not send the email. it only sends the email to the address in the $email_to variable

Any ideas how i can get the CC working?

like image 906
charlie Avatar asked Jun 03 '26 14:06

charlie


1 Answers

I'm assuming you are using the Pear class Mail, If not disregard.

According to this (http://pear.php.net/manual/en/package.mail.mail.send.php#2073) you need to have the email address you want to cc to in the receipients and the headers. I personally have never used this code so I can't attest to whether it works or not.

Here is another link saying the same thing: http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/

Pulled from link:

$to = '[email protected]';
$cc = '[email protected]';
$recipients = $to.", ".$cc;
$headers['From']    = '[email protected]';
$headers['To']      = $to;
$headers['Subject'] = 'Test message';
$headers['Cc']      = '[email protected]';
$headers['Reply-To'] = '[email protected]';

$send = $mail->send($recipients, $headers, $body);

Also just in case you ever need it according to the link to BCC you simply add an email address to the $recipients and don't add it to the $headers.

like image 133
Pitchinnate Avatar answered Jun 06 '26 02:06

Pitchinnate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!