How to Add a CC E-mail Address with PHPMailer

I’ve moved to the fantastic PHPMailer PHP class to handle all my e-mail send outs for my projects and have thus far been quite impressed by the ease-of-use and robustness of the class.

Today’s code snippet will show you just how easy it is to add CC (“carbon copy” or “courtesy copy” depending in what era you were born!) e-mail addresses to a e-mail send out, thus saving computing time by pushing out a single mail send instead of a number of separate ones for each attached address (well technically, we’re just foisting all the work onto the SMTP server, but still, less work that the web server needs to do, i.e. a win for us!).

In code:

//create the mail class and fill in all the required settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.net";
$mail->SMTPAuth = true;
$mail->Username = "username@domain.com";
$mail->Password = "password1";
$mail->From = "username@domain.com";
$mail->FromName = "Software Simian";
$mail->AddAddress("targetguy@domain.com", "Target Guy");
$mail->AddReplyTo("username@domain.com", "Software Simian");
$mail->Subject = "Subject";
$mail->Body = "Message";
$mail->AltBody = strip_tags("Message:);
 
//and now for the actual bit of adding multiple CC e-mail addresses
$mail->AddCC("extra-address1@domain.com");
$mail->AddCC("extra-address2@domain.com");
$mail->AddCC("extra-address3@domain.com");
 
//and send. Now was that not easy?
if(!$mail->Send()){
    $resultstatus = 'Failed';
}

The code above will result in a mail being sent out to four addresses instead of just the main specified address.

Nifty.

Related Posts:

  • I've moved to the fantastic PHPMailer PHP class to handle all my e-mail send outs for my projects and have thus far been quite impressed by the ease-of-use and robustness of the class. Today I'll quickly note how you can send an attachment (like a PDF file for example) with a mail send out. As you can see from the ...

  • PHPMailer is a great little mail sending workhorse class for PHP, widely expanding on the regular PHP mail() function and particularly easy to make use of, making it a personal favourite of mine. Anyway, to check whether or not your mail send was successful when using PHPMailer, all you need to do is check the Boo ...

  • A port is just a port, and just because there is a standard one that is used by something, it doesn't mean you always have to use the default port for doing that something. Enter sending out an e-mail message via SMTP when using a GoDaddy e-mail account. Although GoDaddy restricts incoming e-mail to ports 110 a ...

  • Given a string, it would often be quite useful to extract any e-mail addresses that it might contain. Luckily for us, PHP makes this fairly trivial through the use of its powerful filter_var functionality. Essentially the plan of attack is to take the string, tokenize it by breaking it up on the space character ...

  • Annoyingly, we ran into a problem the other day when all of a sudden our outgoing e-mail stream started to skip send-outs. On closer inspection of the e-mail account, hosted by GoDaddy, we found that the account now comes with what is termed a relay limit of 250. So what is a relay limit? Well basically GoDad ...

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.