SVN Notifications via GMAIL SMTP - python

I have got SVN running on Ubuntu 11.04 32bit and now want notifications using my GMAIL account for every commit.
I've commited a few things but don't actually recieve the commit emails for them. There aren't any errors that are displayed and I have looked through the logs but haven't found much useful information as of yet.
I've read quite alot of posts regarding this and editing the following files below including what they contain now. I've tried using sendmail and postfix but have had no luck with them hence which is why I am using Google's mail server. It would be grateful if someone could point me in the right direction or an alternative approach.
The links I found and have used.
http://sadomovalex.blogspot.com/2009/12/use-gmail-smtp-server-for-post-commit.html
http://iffee.wordpress.com/2009/04/08/svn-commit-to-google-apps-email-notification/
post-commit.tmpl
REPOS="$1"
REV="$2"
/home/megaz/svn/repos/ya/hooks/mailer.py commit "$REPOS" \
"$REV" /home/megaz/svn/repos/ya/hooks/mailer.conf
mailer.conf
[general]
smtp_hostname = smtp.gmail.com:587
smtp_username = #mygmailaddress
smtp_password = #mygmailpassword
smtp_use_ssl = true
smtp_use_tls = 1
[defaults]
diff = /usr/bin/diff -u -L %(label_from)s -L %(label_to)s %(from)s %(to)s
commit_subject_prefix = [SVN-Commit]
propchange_subject_prefix =
lock_subject_prefix =
unlock_subject_prefix =
from_addr = #my from address
to_addr = #my to address
reply_to = #my replyto address
generate_diffs = none
show_nonmatching_paths = yes
[maps]
mailer.py
class SMTPOutput(MailedOutput):
def start(self, group, params):
MailedOutput.start(self, group, params)
self.buffer = StringIO()
self.write = self.buffer.write
self.write(self.mail_headers(group, params))
def finish(self):
server = smtplib.SMTP(self.cfg.general.smtp_hostname)
# 2009-12-13 asadomov: add ssl configuration (e.g. for gmail smtp server)
if self.cfg.is_set('general.smtp_use_ssl') and self.cfg.general.smtp_use_ssl.lower() == "true":
server.ehlo()
server.starttls()
server.ehlo()
if self.cfg.is_set('general.smtp_username'):
server.login(self.cfg.general.smtp_username,
self.cfg.general.smtp_password)
server.sendmail(self.from_addr, self.to_addrs, self.buffer.getvalue())
server.quit()

I see, you have not really read the instructions. The code you have copy/pasted needs to replace a snippet in a larger file which you haven't downloaded. Also, the file name of the post-commit script should not have the .templ suffix; that's what they use for inactive example / template files in the distribution.
Perhaps this explains why you couldn't get Sendmail to work, either. At this point I'd recommend to go back to that, as it's simpler.

rename your "post-commit.templ" to "post-commit"
make sure you give exec rights (such as 755) to "post-commit"

Related

Sendmail not working for local GAE local development server

I've enabled sendmail using the --enable_sendmail=yes option as described in the GAE docs and am still not getting any emails (though the emails do appear in the logging messages). Simplified code looks like:
from google.appengine.api import mail
SENDER_EMAIL_ADDRESS = "admin#APPID.appspot.com"
msg = "Test message"
subject = "Test subject"
recipient = "{} <{}>".format('username', 'username#gmail.com')
mail.send_mail(sender=SENDER_EMAIL_ADDRESS,
to=recipient,
subject=subject,
body=msg)
What am I doing wrong?
If you specify a From address it must correspond with your local machine. The sendmail handling function in the Mail Stub that google provides for its development server looks like:
...
try:
child.stdin.write(mime_message.as_string())
child.stdin.close()
...
The problem is that the From address passed to the send_mail method is in mime_message headers. Adding the following line as the first line in the try block removes that header from the mime message. This allows sendmail to use a default From address:
mime_message._headers = [x for x in mime_message._headers if x[0] != 'From']
The file with this function can be found at /path/to/google_cloud_sdk/platform/google_appengine/google/appengine/api/mail_stub.py

Python aiosmtpd - what is missing for an Mail-Transfer-Agent (MTA)?

I want to write my own small mailserver application in python with aiosmtpd
a) for educational purpose to better understand mailservers
b) to realize my own features
So my question is, what is missing (besides aiosmtpd) for an Mail-Transfer-Agent, that can send and receive emails to/from other full MTAs (gmail.com, yahoo.com ...)?
I'm guessing:
1.) Of course a domain and static ip
2.) Valid certificate for this domain
...should be doable with Lets Encrypt
3.) Encryption
...should be doable with SSL/Context/Starttls... with aiosmtpd itself
4.) Resolving MX DNS entries for outgoing emails!?
...should be doable with python library dnspython
5.) Error handling for SMTP communication errors, error replies from other MTAs, bouncing!?
6.) Queue for handling inbound and pending outbund emails!?
Are there any other "essential" features missing?
Of course i know, there are a lot more "advanced" features for a mailserver like spam checking, malware checking, certificate validation, blacklisting, rules, mailboxes and more...
Thanks for all hints!
EDIT:
Let me clarify what is in my mind:
I want to write a mailserver for a club. Its main purpose will be a mailing-list-server. There will be different lists for different groups of the club.
Lets say my domain is myclub.org then there will be for example youth#myclub.org, trainer#myclub.org and so on.
Only members will be allowed to use this mailserver and only the members will receive emails from this mailserver. No one else will be allowed to send emails to this mailserver nor will receive emails from it. The members email-addresses and their group(s) are stored in a database.
In the future i want to integrate some other useful features, for example:
Auto-reminders
A chatbot, where members can control services and request informations by email
What i don't need:
User Mailboxes
POP/IMAP access
Webinterface
Open relay issue:
I want to reject any [FROM] email address that is not in the members database during SMTP negotiation.
I want to check the sending mailservers for a valid certificate.
The number of emails/member/day will be limited.
I'm not sure, if i really need spam detection for the incoming emails?
Losing emails issue:
I think i will need a "lightweight" retry mechanism. However if an outgoing email can't be delivered after some retries, it will be dropped and only the administrator will be notified, not the sender. The members should not be bothered by email delivery issues. Is there any Python Library that can generate RFC3464 compliant error reply emails?
Reboot issue:
I'm not sure if i really need persistent storage for emails, that are not yet sent? In my use case, all the outgoing emails should be delivered usually within a few seconds (if no delivery problem occurs). Before a (planned) reboot i can check for an empty send queue.
aiosmtpd is an excellent tool for writing custom routing and header rewriting rules for email. However, aiosmtpd is not an MTA, since it does not do message queuing or DSN generation. One popular choice of MTA is postfix, and since postfix can be configured to relay all emails for a domain to another local SMTP server (such as aiosmtpd), a natural choice is to use postfix as the internet-facing frontend and aiosmtpd as the business-logic backend.
Advantages of using postfix as the middle-man instead of letting aiosmtpd face the public internet:
No need to handle DNS MX lookups in aiosmtpd -- just relay through postfix (localhost:25)
No worry about non-compliant SMTP clients in aiosmtpd
No worry about STARTTLS in aiosmtpd -- configure this in postfix instead (simpler and more battle-hardened)
No worry about retrying failed email deliveries and sending delivery status notifications
aiosmtpd can be configured to respond with "transient failure" (SMTP 4xx code) upon programming errors, so no email is lost as long as the programming error is fixed within 4 days
Here's how you might configure postfix to work with a local SMTP server powered by e.g. aiosmtpd.
We're going to run postfix on port 25 and aiosmtpd on port 20381.
To specify that postfix should relay emails for example.com to an SMTP server running on port 20381, add the following to /etc/postfix/main.cf:
transport_maps = hash:/etc/postfix/smtp_transport
relay_domains = example.com
And create /etc/postfix/smtp_transport with the contents:
# Table of special transport method for domains in
# virtual_mailbox_domains. See postmap(5), virtual(5) and
# transport(5).
#
# Remember to run
# postmap /etc/postfix/smtp_transport
# and update relay_domains in main.cf after changing this file!
example.com smtp:127.0.0.1:20381
Run postmap /etc/postfix/smtp_transport after creating that file (and every time you modify it).
On the aiosmtpd side, there are a few things to consider.
The most important is how you handle bounce emails. The short story is that you should set the envelope sender to an email address you control that is dedicated to receiving bounces, e.g. bounce#example.com. When email arrives at this address, it should be stored somewhere so you can process bounces, e.g. by removing member email addresses from your database.
Another important thing to consider is how you tell your members' email providers that you are doing mailing list forwarding. You might want to add the following headers when forwarding emails to GROUP#example.com:
Sender: bounce#example.com
List-Name: GROUP
List-Id: GROUP.example.com
List-Unsubscribe: <mailto:postmaster#example.com?subject=unsubscribe%20GROUP>
List-Help: <mailto:postmaster#example.com?subject=list-help>
List-Subscribe: <mailto:postmaster#example.com?subject=subscribe%20GROUP>
Precedence: bulk
X-Auto-Response-Suppress: OOF
Here, I used postmaster#example.com as the recipient for list unsubscribe requests. This should be an address that forwards to the email administrator (that is, you).
Below is a skeleton (untested) that does the above. It stores bounce emails in a directory named bounces and forwards emails with a valid From:-header (one that appears in MEMBERS) according to the list of groups (in GROUPS).
import os
import email
import email.utils
import mailbox
import smtplib
import aiosmtpd.controller
LISTEN_HOST = '127.0.0.1'
LISTEN_PORT = 20381
DOMAIN = 'example.com'
BOUNCE_ADDRESS = 'bounce'
POSTMASTER = 'postmaster'
BOUNCE_DIRECTORY = os.path.join(
os.path.dirname(__file__), 'bounces')
def get_extra_headers(list_name, is_group=True, skip=()):
list_id = '%s.%s' % (list_name, DOMAIN)
bounce = '%s#%s' % (BOUNCE_ADDRESS, DOMAIN)
postmaster = '%s#%s' % (POSTMASTER, DOMAIN)
unsub = '<mailto:%s?subject=unsubscribe%%20%s>' % (postmaster, list_name)
help = '<mailto:%s?subject=list-help>' % (postmaster,)
sub = '<mailto:%s?subject=subscribe%%20%s>' % (postmaster, list_name)
headers = [
('Sender', bounce),
('List-Name', list_name),
('List-Id', list_id),
('List-Unsubscribe', unsub),
('List-Help', help),
('List-Subscribe', sub),
]
if is_group:
headers.extend([
('Precedence', 'bulk'),
('X-Auto-Response-Suppress', 'OOF'),
])
headers = [(k, v) for k, v in headers if k.lower() not in skip]
return headers
def store_bounce_message(message):
mbox = mailbox.Maildir(BOUNCE_DIRECTORY)
mbox.add(message)
MEMBERS = ['foo#example.net', 'bar#example.org',
'clubadmin#example.org']
GROUPS = {
'group1': ['foo#example.net', 'bar#example.org'],
POSTMASTER: ['clubadmin#example.org'],
}
class ClubHandler:
def validate_sender(self, message):
from_ = message.get('From')
if not from_:
return False
realname, address = email.utils.parseaddr(from_)
if address not in MEMBERS:
return False
return True
def translate_recipient(self, local_part):
try:
return GROUPS[local_part]
except KeyError:
return None
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
local, domain = address.split('#')
if domain.lower() != DOMAIN:
return '550 wrong domain'
if local.lower() == BOUNCE:
envelope.is_bounce = True
return '250 OK'
translated = self.translate_recipient(local.lower())
if translated is None:
return '550 no such user'
envelope.rcpt_tos.extend(translated)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
if getattr(envelope, 'is_bounce', False):
if len(envelope.rcpt_tos) > 0:
return '500 Cannot send bounce message to multiple recipients'
store_bounce_message(envelope.original_content)
return '250 OK'
message = email.message_from_bytes(envelope.original_content)
if not self.validate_sender(message):
return '500 I do not know you'
for header_key, header_value in get_extra_headers('club'):
message[header_key] = header_value
bounce = '%s#%s' % (BOUNCE_ADDRESS, DOMAIN)
with smtplib.SMTP('localhost', 25) as smtp:
smtp.sendmail(bounce, envelope.rcpt_tos, message.as_bytes())
return '250 OK'
if __name__ == '__main__':
controller = aiosmtpd.controller.Controller(ClubHandler, hostname=LISTEN_HOST, port=LISTEN_PORT)
controller.start()
print("Controller started")
try:
while True:
input()
except (EOFError, KeyboardInterrupt):
controller.stop()
The most important thing about running your own SMTP server is that you must not be an open relay. That means you must not accept messages from strangers and relay them to any destination on the internet, since that would enable spammers to send spam through your SMTP server -- which would quickly get you blocked.
Thus, your server should
relay from authenticated users/senders to remote destinations, or
relay from strangers to your own domains.
Since your question talks about resolving MX records for outgoing email, I'm assuming you want your server to accept emails from authenticated users. Thus you need to consider how your users will authenticate themselves to the server. aiosmtpd currently has an open pull request providing a basic SMTP AUTH implementation; you may use that, or you may implement your own (by subclassing aiosmtpd.smtp.SMTP and implementing the smtp_AUTH() method).
The second-most important thing about running your own SMTP server is that you must not lose emails without notifying the sender. When you accept an email from an authenticated user to be relayed to an external destination, you should let the user know (by sending an RFC 3464 Delivery Status Notification via email) if the message is delayed or if it is not delivered at all.
You should not drop the email immediately if the remote destination fails to receive it; you should try again later and repeatedly try until you deem that you have tried for long enough. Postfix, for instance, waits 10 minutes before trying to deliver the email after the first delivery attempt fails, and then it waits 20 minutes if the second attempt fails, and so on until the message has been attempted delivered for a couple days.
You should also take care to allow the host running your mail server to be rebooted, meaning you should store queued messages on disk. For this you might be able to use the mailbox module.
Of course, I haven't covered every little detail, but I think the above two points are the most important, and you didn't seem to cover them in your question.
You may consider the following features:
Message threading
Support for Delivery status
Support for POP and IMAP protocols
Supports for protocols such as RFC 2821 SMTP and RFC 2033 LMTP email message transport
Support Multiple message tagging
Support for PGP/MIME (RFC2015)
Support list-reply
Lets each user manage their own mail lists Supports
Control of message headers during composition
Support for address groups
Prevention of mailing list loops
Junk mail control

IMAP with python to download attachments

I have a instrument at work that emails me a file containing raw data, I can go into my email and download them easily enough but when I have multiple files (which it sends as multiple emails) it gets a bit tedious.
I'm looking at using python and imaplib to login to my email account, search for emails from a known email address within the past day or so and then download any attachments to a directory. So I thought a script might help here.
I've setup a gmail account and altered the settings so that I can connect using imap from a shell, however I'm lost as to where to go from here.
Could someone point me in the right direction as to what I need to do to make this happen.
Here is a repository that is forked off imaplib (made compatible with Python3.6, did not test other versions)
https://github.com/christianwengert/mail
The following snippet checks all unseen messages, then returns their attachments:
server = IMAPClient(imap, use_uid=True, ssl=993)
server.login(username, password)
server.select_folder('INBOX')
message_ids = server.search([b'NOT', b'SEEN']) # UNSEEN
messages = server.fetch(message_ids, data=['ENVELOPE', 'BODYSTRUCTURE', 'RFC822.SIZE'])
for mid, content in messages.items():
bodystructure = content[b'BODYSTRUCTURE']
text, attachments = walk_parts(bodystructure, msgid=mid, server=server)
HTH

Hide login credentials in Python script

I am using Python 2.7.3 and successfully passing my login credentials to an SMTP server to send me a text or email when an event trigger takes place. However, now I would like to store the SMTP server login and password in a separate file so that multiple scripts could use that information or so that I can share my script without having to remove my credentials each time.
I am using the great tutorial from Alex Le on sending an SMS via Python. But now I want to take the following segment and put it into another file that can be called by multiple scripts. This could be either just the username/password pair or the whole section.
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '<gmail_address>', '<gmail_password>' )
I would consider myself a pretty basic Python programmer. I don't mind doing some research, but I think I need help on what terms I should be looking for.
Get all the critical variables from .yml file:
import yaml
conf = yaml.load(open('conf/application.yml'))
email = conf['user']['email']
pwd = conf['user']['password']
server = smtplib.SMTP( "smtp.gmail.com", 587 ) # add these 2 to .yml as well
server.starttls()
server.login(email, pwd)
The application.yml will look similar to this:
user:
email: example#mail.com
password: yourpassword
This way, you will never see the actual credentials in the script.
In production environments what we usually do is make a seperate file and save it somewhere outside the project. Now do chmod 600, i.e. allow only root to access the file. Now in your code run read the file by running in the the superuser mode. Or you could also create a different user which can access the file and run the code using that user.
OR You could use environment variables in your system. You can set one by doing the following in bash shell
export KEY=some_value
And then in your Python code
os.environ.get('KEY')
Use a separate configuration file settings.py containing:
EMAIL = 'gmail address'
PASSWORD = 'gmail password'
As the configuration file is a Python file, you can import it from your actual code:
from . import settings
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login(settings.EMAIL, settings.PASSWORD)
This is similar to what projects such as Django use, which you can see here.
You would need to keep the settings.py file secret, so you would not add it to your revision control software and wouldn't make it publicly readable.

getting started with davlib.py

I want to write a script to connect and access a webdav server. I found out that there is a
/usr/local/lib/python2.7/dist-packages/davlib.py
But how to use it? There is no tutorial, no documentation and nothing to google. Anybody able to write a small hello world for it?
the test webdav server is on localhost:80/webdav and there is a davtest.txt file with the word dav inside.
$ cd <path_to_webdav>
$ ls
davtest.txt
$ cat davtest.txt
dav
I can read python, so if I could just connect and read that there is a file called davtest.txt or maybe even it's content I think I could get started working with the source. Not knowing how webDAV works and not knowing davlib.py both together is quite tough, though.
With webdav-lib I could solve that problem:
url = "davs://localhost:80/webdav/davtest.txt"
r = ResourceStorer(url)
result = r.downloadContent().read()
To do a simple get request using davlib
import davlib
import base64
host = 'myhost'
protocol = 'myprotocol'
username = 'myusername'
password = 'mypassword'
url = '{}://{}/{}'.format(protocol, host, some_file_path)
d = davlib.DAV(protocol=myprotocol, host=myhost)
auth_token = base64.encodestring('%s:%s' %(username, password)).strip()
header = {"Authenication": "Basic %s' %auth_token}
d.get(url,header)
Basic authentication is usable only on https. For more (dry) details on webDAV, see the RFC.
The most mature webDAV client library I've found is python-webdav-library

Categories