Outlook stops working after running python email sending script? - python

I wrote a script to send an email using python. The script worked and the recipient received an email. However, I kept myself in CC and I didn't get an email and moreover, my outlook stopped working after that.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def success_email():
sender = 'abhishek_talwar#xyz.com'
recipients = 'GANA_PANGO#xyz.com'
CC = 'abhishek_talwar#xyz.com'
subject = "Load Balance Request Completed"
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipients
msg['CC'] = CC
# Create the body of the message (a plain-text and an HTML version).
text = 'Hi this is a test mail'
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text.encode('utf-8'), 'html')
# Attach parts into message container.
msg.attach(part1)
s = smtplib.SMTP('mail1.xyz.com')
x =s.sendmail(sender, recipients, msg.as_string())
print x
action = 'Success email sent for'
print action
success_email()

You're not sending the email to the CC list. You're adding the address in the head of the message, but not on the envelop.
x =s.sendmail(sender, [recipients, CC], msg.as_string())

Related

Python sending email with either plain text or html, but not both

I am experimenting with sending emails using Python. I have run into a problem where I cannot send plain text and html in the body, only one or the other. If I attach both parts, only the HTML shows up, and if I comment out the HTML part, then the plain text shows up.
I'm not sure why the email can't contain both. The code looks like this:
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
username = 'email_address'
password = 'password'
def send_mail(text, subject, from_email, to_emails):
assert isinstance(to_emails, list)
msg = MIMEMultipart('alternative')
msg['From'] = from_email
msg['To'] = ', '.join(to_emails)
msg['Subject'] = subject
txt_part = MIMEText(text, 'plain')
msg.attach(txt_part)
html_part = MIMEText("<h1>This is working</h1>", 'html')
msg.attach(html_part)
msg_str = msg.as_string()
with smtplib.SMTP(host='smtp.gmail.com', port=587) as server:
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_email, to_emails, msg_str)
server.quit()
I actually believe according to 7.2 The Multipart Content-Type what you coded is correct and the email client chooses whichever it believes is "best" according to its capabilities, which generally is the HTML version . Using 'mixed' causes both versions to be displayed serially (assuming the capability exists). I have observed in Microsoft Outlook that the text version becomes an attachment.
To see both serially:
Instead of:
msg = MIMEMultipart('alternative')
use:
msg = MIMEMultipart('mixed')
The server.ehlo() command is superfluous.

How to send emails programmatically and hide the sender address?

I would like to send mail notifications to my customers via python. The problem is the sender mail account needs to be hidden. Just to be clear - this is not for phishing or spamming, only personal use!
I used smtplib and setup a new 'noreply' account in gmail, but even when providing an alias to the message, the 'mail from:' header contains my actual mail.
import smtplib
from email.mime.text import MIMEText
from email.utils import *
email_sender = 'noreply%%#gmail.com'
email_receiver = 'example%%%#gmail.com'
subject = 'Python!'
msg = MIMEText('This is the body of the message.')
msg['To'] = formataddr(('Recipient', 'example%%%#gmail.com'))
msg['From'] = formataddr(('Author', 'author#example.com'))
msg['Subject'] = 'Simple test message'
connection = smtplib.SMTP('smtp.gmail.com', 587)
connection.starttls()
connection.login(email_sender, 'password')
connection.sendmail(msg['From'], email_receiver, msg.as_string())
connection.quit()
I get the mail in to my inbox as expected but when clicking 'more details' the original sender address appears.
The first argument to sendmail is the envelope sender, and should be just the email terminus, not a formatted address; so passing in msg['From'] there is doubly wrong (one, because you don't want to show it; and two, because you are passing in the entire From: header with display name and all).

Email using SMTP python from gmail account with html body

I am trying to send email using gmail account from python script using SMTP library. It is working fine with normal message body. But when i try to send it using HTML body. It does not allow me to send.
# Import smtplib to provide email functions
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define email addresses to use
addr_to = 'xxxx#localdomain.com'
addr_from = "xxxxx#gmail.com"
# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user = 'xxxxxx#gmail.com'
smtp_pass = 'xxxxxxx'
# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = *emphasized text*addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html>
<head></head>
<body>
<p>This is a test message.</p>
<p>Text and HTML</p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server,587)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
Add these two lines before attempting to login, it won't gave you the authentication error.
server.ehlo()
server.starttls()
So your code should look like this:
# Import smtplib to provide email functions
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define email addresses to use
addr_to = 'xxxx#localdomain.com'
addr_from = "xxxxx#gmail.com"
# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user = 'xxxxxx#gmail.com'
smtp_pass = 'xxxxxxx'
# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = *emphasized text*addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
(your html code)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server,587)
s.ehlo()
s.starttls()
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
The issue could be that google isn't letting using that service anymore as far as i know

Python 2.7 - I am Using smtplib to send mails to multiple people, but the subject line in email sent is not reflecting in every mail

I am using smtplib to send mails to multiple users, I am reading email ids from mail.txt , and names of receivers from names.txt file and subject from subject.txt file,
Somehow the subject is not reflecting in all emails, only last mail that is sent has subject and others don't. But when I print the subject in each iteration it prints, I am not sure why this is happening. I tried to change number of receipts and checked, but the issue doesn't solve. Also I tried to keep the import statements outside the loop as well.
Can you please help me with this code.
Code :
ob1 = open("names.txt","r")
fname = ob1.readlines()
ob1.close()
ob2 = open("mail.txt","r")
email = ob2.readlines()
ob2.close()
ob3 = open("subject.txt","r")
aname = ob3.readlines()
ob3.close()
for i in range(len(fname)):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'my_email_id'
msg['To'] = email[i]
msg['Subject'] = aname[i]
message = 'some mail text'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
mailserver.login('my_email_id', '******')
mailserver.sendmail('my_email_id',email[i],msg.as_string())
mailserver.quit()

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