I am working on sending emails with python and have a decent grasp on that however I am struggling with using info from a CSV File.
I have a piece of code:
s.login('example#gmail.com', 'expassword')
that when used with a preset login and password works, it sends the email to my recipient, which is later set in my code, however as soon as I use the code:(chooses a random email and password from a CSV to send from(for a spam email demo))
with open ('C:/Filepath/randomsender.csv') as file:
reader = csv.reader(file)
sender = random.choice(list(reader))
followed by
s.login(sender)
I get an auth error because it cannot read the chosen line properly even though the result of sender is formatted the same as:
'example#gmail.com', 'expassword'
If you want to pass list elements as position param, you need to unpack it.
this might help
e.g.
s.login(*sender)
Related
I'm trying to build a python mailserver that rewrites certain URLs (given in a text file).
Plaintext messages work without problems, but multipart (HTML) E-Mails are a bit of concern to me. I can't find a proper way to rebuild the message after modifying the body and replace functions do not exist for email in python.
What I have currently (for plaintext messages, currently without replacement):
if(not msg.is_multipart()):
for part in msg.walk():
parttype = part.get_content_type()
partmaintype = parttype.split("/")[0]
partsubtype = parttype.split("/")[1]
parttype=partmaintype + "/" + partsubtype
bodypart = part.get_body(('plain','html'))
#bodypart = bodypart.get_content().encode()
bodypart = bodypart.get_content()
print(bodypart)
newmsg.set_content(bodypart)
#print(newmsg)
newdata = bytes(newmsg)
The _structure(msg) function is in knowledge to me, but I cannot find a way to preserve this message structure (to rebuild the email with a new message object). I also know about the msg.walk() function, but when used with multipart message contents, the subcontents occur twice.
Thanks a lot in advance!
An email comes in with the following header from an #outlook.com address
Message-ID:
<DM5PR1101MB2074A897F300D11D32C71B75EC049#DM5PR1101MB2074.namprd11.prod.outlook.com>
To respond with Python I put that message identifier in the In-Reply-To header
from email.message import EmailMessage
msg = EmailMessage()
msg['In-Reply-To'] = '<DM5PR1101MB2074A897F300D11D32C71B75EC049#DM5PR1101MB2074.namprd11.prod.outlook.com>'
However, when I print this message the header appears as follows:
In-Reply-To: =?utf-8?q?=3CDM5PR1101MB2074A897F300D11D32C71B75EC049=40DM5PR11?=
=?utf-8?q?01MB2074=2Enamprd11=2Eprod=2Eoutlook=2Ecom=3E?=
And when it arrives in Outlook it's not put in the same conversation thread as the original message.
Does anyone know why this may be happening and/or know of any work-around?
I suspect that this is happening because Python is trying to split the header across multiple lines since this header is above the RFC-specified line length and when it splits across multiple lines it starts each line with '?utf-8?q and converts the < character to ?=3C, etc... Whereas Outlook is expecting the header to be of the format
In-Reply-To':
<DM5PR1101MB2074A897F300D11D32C71B75EC049#DM5PR1101MB2074.namprd11.prod.outlook.com>'
Perhaps if someone knows how to force the header to be in this format that would suffice as a work-around.
Any help would be greatly appreciated :)
I am trying to code something very simple. The idea is to read a .txt where a html code is written. Specifically it is a table with some css and style modifiers like:
<td style=""background-color:red"" >-49,2%</td>
I can sucessfully read the content of the file and send the email. However, when the email is sent, no background style is applied. The rest of the table is created perfectly.
My code is something like:
fp = open(r"filepath", 'r',encoding='utf-8-sig')
# Create a text/plain message
msg = MIMEText(fp.read()) #Here I also tried with the argument 'html'
fp.close()
s.sendmail("email1", "email2",msg.as_string())
Is there any way I can also get the style applied? The email provider is gmail and I know it can work because if I try to use the 'msg' variable as a literal string, like:
msg= r"<table...> <td style=""background-color:red"" >-49,2%</td> </table>"
it sends the background format correctly. The issue must be of other sort.
Thank you very much!
Solved.
I hope it helps,
The modifier.as_string() was the issue. Removing it on:
s.sendmail("email1", "email2",msg.as_string())
to
s.sendmail("email1", "email2",msg)
made it work.
I'm writing an email parser for python 2.7 that will be invoked via sendmail using an alias, parsed using the email module and then processed and stored into an oracle database:
From /etc/aliases:
myalias: | /my/python/script.py
I'm having trouble "slurping" all of stdin into a string object that I can use with the email module:
import email
# Slurp stdin and store into message
message =
msg = email.message_from_string(message)
# Do something with it
print msg['Subject']
What would be the most efficient way to do this? I've tried stdin.readlines() but it ends up as a list.
Thx for any help. (Sorry if this is noobish... I'm a perl convert and been forced to standardize my tools using python and this is only my second script. Well not really "forced", I've been wanting to do this for some time but not under the gun of a deadline like now)
sys.stdin.readlines() returns a list of lines. If what you want is one long (multi-line) string, use sys.stdin.read().
I want to make postfix send all emails to a python script that will scan the emails.
However, how do I pipe the output from postfix to python ?
What is the stdin for Python ?
Can you give a code example ?
Rather than calling sys.stdin.readlines() then looping and passing the lines to email.FeedParser.FeedParser().feed() as suggested by Michael, you should instead pass the file object directly to the email parser.
The standard library provides a conveinience function, email.message_from_file(fp), for this purpose. Thus your code becomes much simpler:
import email
msg = email.message_from_file(sys.stdin)
To push mail from postfix to a python script, add a line like this to your postfix alias file:
# send to emailname#example.com
emailname: "|/path/to/script.py"
The python email.FeedParser module can construct an object representing a MIME email message from stdin, by doing something like this:
# Read from STDIN into array of lines.
email_input = sys.stdin.readlines()
# email.FeedParser.feed() expects to receive lines one at a time
# msg holds the complete email Message object
parser = email.FeedParser.FeedParser()
msg = None
for msg_line in email_input:
parser.feed(msg_line)
msg = parser.close()
From here, you need to iterate over the MIME parts of msg and act on them accordingly. Refer to the documentation on email.Message objects for the methods you'll need. For example email.Message.get("Header") returns the header value of Header.