Skip to content

Instantly share code, notes, and snippets.

@mapio
Created November 8, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mapio/4039338 to your computer and use it in GitHub Desktop.
Save mapio/4039338 to your computer and use it in GitHub Desktop.
Mail merge in Python per mandare username e password
PASSWORD user@foo.bar
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import SMTP
from time import sleep
text = open( 'message.txt', 'r' ).read()
html = open( 'message.html', 'r' ).read()
smtp = SMTP( 'smtp.di.unimi.it' )
for n, code_you in enumerate( open( 'codici.txt', 'r' ), start = 1 ):
code, you = code_you.split( '\t' )
you = you.strip()
msg = MIMEMultipart( 'alternative' )
msg[ 'Subject' ] = 'Codice per la registrazione a Campus Party'
msg[ 'From' ] = 'Massimo Santini <santini@di.unimi.it>'
me = msg[ 'Reply-To' ] = 'santini@di.unimi.it'
msg[ 'To' ] = you
msg.attach( MIMEText( text.format( code, you ), 'plain' ) )
msg.attach( MIMEText( html.format( code, you ), 'html', 'iso-8859-1' ) )
smtp.sendmail( me, you, msg.as_string() )
print '{0}\t{1}'.format( n, you )
sleep( 1 )
smtp.quit()
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8559-1">
<title>Codice Campus Party</title>
<style type="text/css">body {{font-family: Arial, Helvetica, sans-serif;}} p {{ padding-bottom: 1em }}</style>
</head>
<body>
<p>Caro studente, <br/>
ti anticipo il codice per registrati sul sito di Campus Party Ë <strong>{0}</strong>
(va usato assieme all'indirizzo email <strong>{1}</strong> a cui hai ricevuto questa comunicazione).</p>
<p>Massimo Santini</p>
<p>PS<br/>
A breve dovresti ricevere anche una mail dall'organizzazione dell'evento.</p>
</body>
</html>
Caro studente, ti anticipo il codice per registrati sul sito di Campus Party è {0}
(va usato assieme all'indirizzo email {1} a cui ricevi questa comunicazione).
Massimo Santini
PS
A breve dovresti ricevere anche una mail dall'organizzazione dell'evento.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment