Python: Sending an Email including CC - python

msg['From'] = email_from
msg['To'] = "aaa#abc.com"
msg['Cc'] = "bbb#abc.com, ccc#abc.com"
...
server.sendmail(email_from, "aaa#abc.com, bbb#abc.com, ccc#abc.com", msg.as_string())
I sent an email through above code. (Those three emails are mine, so I can check up all of them)
I see an email that I sent through python code in the inbox of "aaa", and there clearly shows that two other emails ("bbb" and "ccc") on the CC list.
However, I cannot find the email either "bbb"/"ccc" inbox. I want to figure out what is happening behind and why the email is not sent to those two emails.

When you send the email the to and cc emails must be in an array. So the correct form of the command should be like this:
server.sendemail(email_from, ["aaa#abc.com","bbb#abc.com","ccc#qbc.com"], msg.as_string())

Related

Trying to send an Email through python with Gmail or Outlook

Hello I am fairly new to python and stumbled upon this cool feauture that with only a few lines of code, python can send emails.
I am currently not sure if the code that I have below works or if its something on my end? Since I am totally new to this I have no way to test if I am on the right track or not.
When running the code, it compiles with no problem but I never receive the messages.
Also, I am sending emails to myself, so I should see them rather quickly.
Here is my Outlook code:
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'myemail#hotmail.com'
mail.Subject = 'Hello this is you!
mail.Body = 'Hello!!!!!!'
mail.HTMLBody = '<h2>This is an H2 message</h2>' #this field is optional
# To attach a file to the email (optional):
attachment = "C:/Users/OneDrive/Documents/Desktop/Social_Network_Ads.csv"
mail.Attachments.Add(attachment)
mail.Send()
Here is my Gmail Code:
import smtplib
fromaddr = 'myemail#gmail.com'
toaddrs = 'myemail#gmail.com'
msg = 'There was a terrible error that occured and I wanted you to know!'
# Credentials (if needed)
username = '###username###'
password = '###password###'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Please let me know why I am not receiving the email or why its showing as not sent?
EDIT:
I am connected to my local hotmail account and I am logged into Gmail, so I am hopping its not a connection issue.
When I go to check my sent folder nothing seems to have been sent

Python SMTP send email with CC

I'm using Python 2.7.3 and I want to send email with CC.
Something in general like this:
(The subject is just for example)
I have the following function:
def send_email(data):
body = "hello world"
message = MIMEText(body)
message['Subject'] = "File '%s' upload data" % data['filename']
message['From'] = 'noreply#atte-mm.com'
message['To'] = data['send_to']
message['CC'] = 'test#atte-mm.com'
s = smtplib.SMTP(SMTP_HOST)
s.sendmail(EMAIL_SENDER, [data['send_to']], message.as_string())
s.quit()
The function works however it ignores the CC.
The TO receive the email without the CC and of curse the CC receives nothing.
I didn't find anything useful in the python documentation about SMTP.
Is it possible to send a mail with CC? If not how do I make it work with two email address in the TO?
The "c" might have to be lower case like this:
message['Cc'] = 'test#atte-mm.com'

When to use MIMEMultipart() From and To

Most examples of sending email from Python seem to set the From and To attributes of email.mime.multipart.MIMEMultipart similar to the following:
msg = email.mime.multipart.MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
Then, the email is sent using smtplib.SMTP() where the original recipient and the sender values are used, like this:
s = smtplib .SMTP('localhost')
s.sendmail(from_email, to_email, msg.as_string())
So what’s the point of setting msg[‘From’] and msg[‘To’] when it’s not used. And in what situation would you use it?
See this for an example of where both are set: http://www.pythonforbeginners.com/code-snippets-source-code/using-python-to-send-email
You set 'From' at 2 places because one is for the actual sending, and the other is for displaying where the message comes from.
Note that most SMTP clients at this point do not allow you to adapt the From header (except for giving an alias, rather than changing the From email)
Something like
Your Neighbour <my#email.com>
Could be valid as from header, while
Your Neighbour <fake#email.com>
is not allowed (when sending originally from my#email.com).

Send email to multiple addresses (using cc) from different domains

I've made a little Python script that sends emails using smtplib.
For example, I have an email that needs to be sent to n users (via To: field), but I also need to send this email to m other users, via Cc: field.
Obviously those n + m email addresses are from different domains (#mydomain, #gmail, #hotmail, #whatever). The emails are correctly delivered to each address if I put the email addresses in the To: field, but the same thing doesn't happen if I put the emails in the Cc: field....
For example
FROM: me#mydomain.com
TO: alice#mydomain.com, bob#gmail.com, mallory#hotmail.com
CC: john#mydomain.com, robert#yahoo.com, clara#gmail.com
note that the email is sent using a #mydomain.com account. The addresses in the TO: list correctly receive the email, while only john#mydomain.com, from the CC: list, get the email..
It seems that the CC field works only with same-domain-email... Any idea?
Anyway, this is the code:
msg = MIMEText(mailContent)
msg["Subject"] = "This is the subject"
msg["From"] = "me#mydomain.com"
toEmails = ["alice#mydomain.com", "bob#gmail.com", "mallory#hotmail.com"]
ccEmails = ["john#mydomain.com", "robert#yahoo.com", "clara#gmail.com"]
msg["To"] = ",".join(toEmails)
msg["Cc"] = ",".join(ccEmails)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login("me#mydomain.com", "password")
server.sendmail("me#mydomain.com", toEmails, msg.as_string())
server.quit()
Thanks
change this line
server.sendmail("me#mydomain.com", toEmails+ccEmails, msg.as_string())

Mails not being sent to people in CC

I have the following script for sending mails using python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
FROMADDR = "myaddr#server.com"
PASSWORD = 'foo'
TOADDR = ['toaddr1#server.com', 'toaddr2#server.com']
CCADDR = ['ccaddr1#server.com', 'ccaddr2#server.com']
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Test'
msg['From'] = FROMADDR
msg['To'] = ', '.join(TOADDR)
msg['Cc'] = ', '.join(CCADDR)
# Create the body of the message (an HTML version).
text = """Hi this is the body
"""
# Record the MIME types of both parts - text/plain and text/html.
body = MIMEText(text, 'plain')
# Attach parts into message container.
msg.attach(body)
# Send the message via local SMTP server.
s = smtplib.SMTP('server.com', 587)
s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.login(FROMADDR, PASSWORD)
s.sendmail(FROMADDR, TOADDR, msg.as_string())
s.quit()
When I use the script, I see that the mail gets delivered to both toaddr1 and toadd2
However ccaddr1 and ccaddr2 does not receive the mail at all.
Interestingly, when I check the mails received by toaddr1 and toadd2, it shows that
ccaddr1 and ccaddr2 are present in CC.
Is there any error in the script? Initially I thought that this might be an issue with my mail server. I tried it with Gmail and saw the same result. That is, no matter whether its an account in my current mail server or my Gmail account in the CC, the recipient will not receive the mail, even though the people in the 'To' field receive it properly and have the correct addresses mentioned in the CC field
I think that you will need to put the CCADDR with the TOADDR when sending the mail:
s.sendmail(FROMADDR, TOADDR+CCADDR, msg.as_string())
You're correctly adding the addresses to your message, but you will need the cc addresses on the envelope too.
From the docs:
Note The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents.
You specified the CC entries in the message, but not in the envelope. It's your job to make sure that the message is also sent to the CC and BCC entries.
I got below error with TOADDR+CCADDR =>
TypeError: can only concatenate str (not "list") to str
I did below changes and it worked for me.
It sends email with attachment to - "To", "Cc" & "Bcc" successfully.
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "\n !! Hello... !!"
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject
s.sendmail(fromaddr, (toaddr+cc+bcc) , message)

Categories