How to add cc in python mail

To add a CC (Carbon Copy) recipient in Python using the smtplib library, you can modify the msg object before sending the email. Here's an example:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Set up the email server
server = smtplib.SMTP('your_smtp_server', 587)
server.starttls()
server.login('your_email_username', 'your_email_password')

# Create the message
msg = MIMEMultipart()
msg['From'] = 'your_email_username'
msg['To'] = '[email protected]'
msg['Subject'] = 'Test email with CC'

# Add the CC recipient
cc_recipient = '[email protected]'
msg['CC'] = cc_recipient

# Add the email body
body = 'This is a test email with CC.'
msg.attach(MIMEText(body, 'plain'))

# Send the email
server.sendmail('your_email_username', ['[email protected]', cc_recipient], msg.as_string())
server.quit()

In this example, we create a MIMEMultipart message object and set the From, To, and Subject headers. We then add the CC recipient to the CC header using the msg['CC'] attribute. Finally, we attach the email body using MIMEText and send the email using server.sendmail.

Note that the CC header is not a standard header in the email.mime library, so we need to use the msg['CC'] attribute to set it. Also, the sendmail method takes three arguments: the sender's email address, the list of recipient email addresses (including the CC recipient), and the message body as a string.

Alternatively, you can use the yagmail library, which provides a simpler way to send emails with CC recipients:

import yagmail

yag = yagmail.SMTP('your_email_username', 'your_email_password')
yag.send('[email protected]', 'Test email with CC', 'This is a test email with CC.', cc='[email protected]')

In this example, we create a yagmail object and use the send method to send the email. We pass the recipient's email address, the subject, the email body, and the CC recipient as arguments.