Gmail API Python Plain Text is Not Wrapping in Body of Email - python

When I send an email the plain text is not wrapping. I read that you can't have more than 80 characters in a line or Gmail automatically makes a break and it makes the text look horrible on a phone. I put 'html' as second parameter in MIMETEXT(). This wraps the text, but does not include any Python escape characters. I can't figure out how to make line breaks?
Code:
I set MIMEText with 'html' parameter and this seems to wrap text, but in a block with out any of the Python escape characters being used.
def CreateMessageHtml(sender, to, subject, message_text):
msg = MIMEText(message_text,'html')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to
return {'raw': base64.urlsafe_b64encode(msg.as_string())}
Issue is in the message_text, not sure how to create a line break because \n is not working.
def main():
df = pd.read_csv('testdata.csv')
for index,row in df.iterrows():
to = row['Email']
sender = "sender"
subject = "subject"
dedent_text = '''Hello {}, \n
Thank you for attending our last meeting. We would
like to see you again at our next event.'''.format(row['First'])
message_text = textwrap.dedent(dedent_text).strip()
SendMessage(sender, to, subject, message_text)
if __name__ == '__main__':
main()

In the function CreateMessageHtml the MIMEText object takes a subtype, which is 'HTML'. From the documentation: https://docs.python.org/2/library/email.mime.html#email.mime.text.MIMEText
class email.mime.text.MIMEText(_text[, _subtype[, _charset]])
Module: email.mime.text
A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain.
Based on this you need to pass in a HTML formatted string. So I changed the dedent_text in the main function to:
dedent_text='''Hello {},
<p> Thank you for attending our last meeting.</P>
<p>We would like to see you again at our next
event.</p>'''.format(row['First'])
Now the text wraps on a phone with line breaks.

Try using triple quotes """ TEXT """ instead of triple apostrophes. Tried this:
mytext = """I'm going \n down down \n down """
print mytext
and the output was:
I'm going
down down
down

Related

pyramid_mailer `Message` and `Content-Transfer-Encoding`

I'm sending emails with pyramid_mailer and found this weird issue that when I use Office365 as my SMTP server it adds random = characters into my message. I don't get that issue with any other mail server (I tested this with gmail and also with my own postfix server)
I send emails like below:
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Attachment, Message
mailer = Mailer()
mailer.smtp_mailer.hostname = "test.mail.at.office365"
mailer.smtp_mailer.username = "my_user"
mailer.smtp_mailer.password = "secret"
mailer.smtp_mailer.port = 587
mailer.smtp_mailer.tls = True
message = Message(
subject="Test",
sender="my_user#my_domain.com",
recipients="test_user#test_domain.com",
body="very long text, at least 75 characters long so Office 365 will break it and insert annoying '=' into message",
html="very long text, at least 75 characters long so Office 365 will break it and insert annoying '=' into message",
)
mailer.send_immediately(message)
I searched on google and found this has something to do with line breaks and Transfer-Content-Encoding. And indeed, if I add \r\n every ~50 characters I won't see = added. But the problem is that I might want to send a hyperlink that will be longer than that...
Pyramid documentation (https://docs.pylonsproject.org/projects/pyramid_mailer/en/latest/) says I can use Attachment rather than plain string. And indeed as soon as I do that I can set this Transfer-Content-Encoding to something like base64 (as suggested here: https://jeremytunnell.com/2009/01/04/really-hairy-problem-with-seemingly-random-crlf-and-spaces-inserted-in-emails/) but my message then shows as attachment, not as regular message...
There seems to be no way to add this Transfer-Content-Encoding to Message object... I tried to use Message.extra_headers = {'Transfer-Content-Encoding': 'base64'} but this did not help.
I'm totally out of ideas, would appreciate any help...
-- Edit --
Thanks to answer below from #Mess:
from pyramid_mailer.message import Attachment
my_message = "very long text, at least 75 characters long so Office 365 will break it and insert annoying '=' into message"
body_html = Attachment(data=my_message, transfer_encoding="base64", disposition='inline')
body_text = Attachment(data=my_message, transfer_encoding="base64", disposition='inline')
Then pass body_html and body_text to Message constructor.
This is "Content-Disposition" header you need to set to control how the content is available to the recipient.
Set it to "attachment" to let download the file, use "inline" to be able to include the content, for example a logo, directly to your email, etc:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
I hope it will point you to the right direction.
EDIT:
Using pyramid_mailer package it would be something like:
from pyramid_mailer.message import Attachment
attachment = Attachment(data=some_data, transfer_encoding="base64", disposition='inline')

Collect Python output till current execution and assign it to variable for sending email

I am doing text processing using Python in which I am looking for a specific text in a console log and printing every matched line. This is accomplished by a function called:
get_matched_log_lines(url, search_pattern, print_pattern) where url = from which I get my log, search_pattern = my target search pattern, print_pattern = the way I want to print my output(its, %s.%s)
How do I send this entire output of function get_matched_log_lines() via email? Emailing function code is already written by me in Python.
Here is what I think/attempted so far:
email_content = get_matched_log_lines(url, search_pattern, print_pattern)
TO = 'recipient email address'
FROM ='sender email address'
#emailing function - py_mail
py_mail("Test email subject", email_content, TO, FROM)
This provides me an empty email.
Here is my answer based on the suggestions by PyNEwbie:
def get_matched_log_lines(url, search_pattern, print_pattern):
out = open("output_file.txt", "w")
for something in somthings:
test = print_pattern % matched_line
print >>out, test
out.close()
^^ just a general example (syntax maybe incorrect). The idea is to open the file in write mode and then dumping the output in it.
fp = open("output_file.txt", 'r')
# Create a text/plain message
msg = fp.read()
fp.close()
email_content = msg
Then open the same file in read mode and store its output to some var (in my case email_content)
Finally send an email with that email_content,
email_content = get_matched_log_lines(url, search_pattern, print_pattern)
TO = 'recipient email address'
FROM ='sender email address'
#emailing function - py_mail
py_mail("Test email subject", email_content, TO, FROM)

mail is delivering without subject when calling from function in python

I have a sample code like following:
import smtplib
def send_mail(PASS,FAIL):
me = "XXXX"
you = "YYYY"
print "Start of program"
server = smtplib.SMTP('ZZZ', 25)
total_testcase = "15/12"
print total_testcase
message = """From: From Person <XXXX>
To: To Person <YYYY>
Subject: mail testing
%s
""" %total_testcase
print message
server.sendmail(me, you, message)
send_mail(8,9)
when I am sending the email it is delivering without the subject
But if I use the code instead of a function call - then it is delivering fine with subject. Anything I am missing in a function call. Please suggest.
The issue you're having is with the triple-quoted multi-line string. When you put it in your function, you're indenting all of its lines so that they line up with the rest of the code. However, this results in unnecessary (and inappropriate) spaces at the start of each line of the message after the first.
Leading spaces in the headers of an SMTP message indicate that the previous header should be continued. This means that all of your first three lines are combined into the From header.
You can fix this either by leaving out the leading spaces:
def send_mail(PASS,FAIL):
#...
message = """From: From Person <XXXX>
To: To Person <YYYY>
Subject: mail testing
%s
""" % total_testcase
#...
Or by using \n instead of real newlines in your string:
message = "From: From Person <XXXX>\nTo: To Person <YYYY>\nSubject: mail testing\n\n%s" % total_testcase
Or finally, you could keep the current code for the generation of the message, but strip out the leading whitespace afterwards:
def send_mail(PASS,FAIL):
#...
message = """From: From Person <XXXX>
To: To Person <YYYY>
Subject: mail testing
%s
""" % total_testcase
message = "\n".join(line if not line.startswith(" ") else line[4:]
for line in message.splitlines())
#...
This last option is a bit fragile, as it may strip out desired whitespace from lines in your total_testcase string (if it had multiple lines), not only the spaces added due to the multi-line string. It also will break if you're using tabs for indentation, or really anything other than four spaces. I'm not sure I'd actually recommend this approach.
A better version of the last approach is to use the textwrap.dedent function from the the standard library. It removes any indentation that is present at the start of every line in a string (but only the indentation that is common to all lines). This does require a small change to how you were creating message, as you need the first line to have the same leading spaces as all the rest (you'll also need to avoid adding any newlines without indentation in the extra text that comes from total_testcase).
Here's the code:
import textwrap
def send_mail(PASS,FAIL):
#...
# backslash after the quotes on the first line avoids a empty line at the start
message = """\
From: From Person <XXXX>
To: To Person <YYYY>
Subject: mail testing
%s
""" % total_testcase
message = textwrap.dedent(message)
#...

Python mail inserts space every 171 characters

I am trying to write a python script to send an email that uses html formatting and involves a lot of non-breaking spaces. However, when I run it, some of the &nbsp strings are interrupted by spaces that occur every 171 characters, as can be seen by this example:
#!/usr/bin/env python
import smtplib
import socket
from email.mime.text import MIMEText
emails = ["my#email.com"]
sender = "test#{0}".format(socket.gethostname())
message = "<html><head></head><body>"
for i in range(20):
message += " " * 50
message += "<br/>"
message += "</body>"
message = MIMEText(message, "html")
message["Subject"] = "Test"
message["From"] = sender
message["To"] = ", ".join(emails)
mailer = smtplib.SMTP("localhost")
mailer.sendmail(sender, emails, message.as_string())
mailer.quit()
The example should produce a blank email that consists of only spaces, but it ends up looking something like this:
&nbsp ;
&nb sp;
& nbsp;
&nbs p;
&n bsp;
Edit: In case it is important, I am running Ubuntu 15.04 with Postfix for the smtp client, and using python2.6.
I can replicate this in a way but my line breaks come every 999 characters. RFC 821 says maximum length of a line is 1000 characters including the line break so that's probably why.
This post gives a different way to send a html email in python, and i believe the mime type "multipart/alternative" is the correct way.
Sending HTML email using Python
I'm the developer of yagmail, a package that tries to make it easy to send emails.
You can use the following code:
import yagmail
yag = yagmail.SMTP('me#gmail.com', 'mypassword')
for i in range(20):
message += " " * 50
message += "<br/>"
yag.send(contents = message)
Note that by default it will send a HTML message, and that it also adds automatically the alternative part for non HTML browsers.
Also, note that omitting the subject will leave an empty subject, and without a to argument it will send it to self.
Furthermore, note that if you set yagmail up correctly, you can just login using yag.SMTP(), without having to have username & password in the script (while still being secure). Omitting the password will prompt a getpass.
Adding an attachment is as simple as pointing to a local file, e.g.:
yag.send(contents = [message, 'previously a lot of whitespace', '/local/path/file.zip']
Awesome isn't it? Thanks for the allowing me to show a nice use case for yagmail :)
If you have any feature requests, issues or ideas please let me know at github.

Using the Pythons email.message API, how do you remove a specific part of the message

I have email message with an unwanted attachment (a PKCS-7 signature in this particular case). I can detect the signature in the email with this piece of code:
payloads = mail.get_payload()
for index in xrange(len(payloads)):
if payloads[index].get_content_type() == "application/pkcs7-signature":
print("Found PKCS-7 Signature", index)
How would I remove this particular payload from the message? The email.message API seems to only have methods for reading and writing whole payloads: get_payload() and set_payload(). Neither of these allow specifying payload index of what to read or write.
One possible solution:
def remove_signature(mail):
payload = mail.get_payload()
if isinstance(payload, list):
for part in payload:
if part.get_content_type().startswith('application/pkcs7-signature'):
payload.remove(part)
return mail

Categories

Resources