facelessuser wrote:Yes, I think I heard that code runs better if it is prettier.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</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 local SMTP server.
s = smtplib.SMTP('localhost:25')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()agibsonsw wrote:I grabbed the following code from the Python docs and ran it (Windows 7) and it sent an email for me![]()
The SMTP settings can be added to the package settings and send() might be replaced with display() or show().
But perhaps it's not so straight-forward on non-Windows OSes(?).
- Code: Select all
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</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 local SMTP server.
s = smtplib.SMTP('localhost:25')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
import platform
def send_mail_windows(receiver, subject, body, sender, server):
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(body)
msg['to'] = receiver
msg['from'] = sender
msg['subject'] = subject
smtpObj = smtplib.SMTP(server)
smtpObj.sendmail(sender, [receiver], msg.as_string())
smtpObj.quit()
def send_mail_mac(receiver, subject, body):
import subprocess
cmd = """echo "%s" | mailx -s "%s" %s""" % (body, subject, receiver)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, errors = p.communicate()
if errors != None:
print errors
if output:
print output
system = platform.system()
if system == "Darwin":
send_mail_mac(
'me@gmail.com',
'Mail Time',
"Look mom, I am sending an email!"
)
elif system == "Windows":
send_mail_windows(
'me@gmail.com',
'Mail Time',
"Look mom, I am sending an email!",
'noreply@CaptainLaserFace.com',
'localhost'
)
else:
# Do some linux stuff, probably use mailx
pass
facelessuser wrote:Yes, I think I heard that code runs better if it is prettier.
Grant wrote:As I have found here at my office, our proxy blocks outbound SMTP calls directly. I'd found a way to make calls to send mail through Exchange via a VB script, but that probably won't help much here.
My point about sending via email was just an example. There are plenty of times where I want to paste code snippets into Word documents SharePoint (yuck) pages, or even (more frequently for me) Evernote where I can save off commonly used snippets of code for searching later.
If we want to email our happily formatted code, is it possible to make a new plugin that piggy-backs off of this one?
Return to Plugin Announcements
Users browsing this forum: No registered users and 3 guests