def __init__(self, email, fliename): # parse an email object and record interesting fields from_field = email.get('Received-From') if from_field is None: from_field = email.get('From') from_field = Email.gimme_unicode(from_field) self.from_name, self.from_addr = Email.parse_email_field(from_field) to_field = email.get('Delivered-To') if to_field is None: to_field = email.get('To') to_field = Email.gimme_unicode(to_field) self.to_name, self.to_addr = Email.parse_email_field(to_field) self.has_dkim = self.dkim_present(email) if self.has_dkim: self.dkim_verified = self.dkim_verify(email) try: self.msg_id = int(filename.split('.')[0].split('/')[1]) except Exception as e: print e, filename self.save()
def __parse_error__(info, email): data = {"errors": [info]} subject = email.get('Subject') try: date = dateparse(email.get('Date'), fuzzy=True).timestamp() finally: date = None if subject is not None: data["origin"] = {'subject': subject} if date is not None: data["date"] = {'date': date} return dumps(data)
def read(self, email): if email is not None: data = list() data.append(email.get('date_shipment', 'None')) data.append(email.get('cement_weight', 'None')) data.append(email.get('cement_grade', 'None')) data.append(email.get('driver', 'None')) data.append(email.get('address', 'None')) append_rowdata(self.spreadsheetId, self.service, data, self.google_sheets_creadential_json) set_cellformat(self.spreadsheetId, self.google_sheets_creadential_json) else: print("Mail is not found")
def get_date(message, email): try: date = email.get('date') if date: message.add_date(date) return message except Exception as error: # logger.error('error retrieving email date: {}'.format(error)) print('error retrieving email date: {}'.format(error))
def get_sender(message, email): try: sender = email.get('from') if sender: message.add_sender(sender) return message except Exception as error: # logger.error('error retrieving email sender: {}'.format(error)) print('error retrieving email sender: {}'.format(error))
def checkName(): subject = email.get('Subject') if subject == 'FW: Device Status Change' or subject == 'Fw: Device Status Change': #checks subject of Asset Status Email print 'The script works' getIMEI() else: print "Not the Asset Change email email" print ''
def get_subject(message, email): try: subject = email.get('subject') if subject: message.add_subject(subject) return message except Exception as error: # logger.error('error retrieving email subject: {}'.format(error)) print('error retrieving email subject: {}'.format(error))
def checkName(): subject = email.get('Subject') if subject == 'FW: Order Notification' or subject == 'Fw: Order Notification': print 'The script works' getName() else: print "Not the Notification email" print '' print "End of checkName and getName script"
def SendMail(self, mail_list, email): replay_to = True from_addr = email.get("From") sub = email.get("Subject") m = re.search("send: (.+) subject: (.+)", sub) if m and any([x in from_addr for x in mail_list]): sub = m.group(2) mail_list = [m.group(1)] logging.info(from_addr + " sent an email") print(from_addr + " sent an email") replay_to = False ''' for header in email._headers: email._headers.remove(header) ''' if replay_to: email.add_header("reply-to", from_addr) del email["Subject"] del email["To"] del email["From"] email.add_header("From", self.cred[0]) email.add_header("To", ", ".join(mail_list)) email.add_header("Subject", str(sub + " from: " + from_addr)) email["Subject"] = str(sub + " from: " + from_addr) try: response = self.connection.send_message(email) except: self.Connect() response = self.connection.send_message(email) if response != {}: logging.warning( "response of the email(" + str(sub) + ") sending was " + str(response) ) logging.debug( "Sent mail " + email["subject"] + " to addr " + str(mail_list) + " with response " + str(response) )
def get_emails_details_not_from_label(label, notEmails, app_token, size): """ Returns id, date (and time), from, and subject of the most recent n emails sent to the address. """ idList = [] # idList is IDs of emails you already will train with if notEmails: for email in notEmails: idList.append(email.get("id")) connections = retrieve_accounts(app_token) detailsList = [] # Step 1: Init multiprocessing.Pool() pool = mp.Pool(mp.cpu_count()) # Step 2: `pool.map` the `get_email_details_from_account()` detailsList = pool.map( get_emails_not_from_label, [(connection, label, idList, size) for connection in connections], ) # Returns a list of lists # Flatten the list of lists detailsList = [ent for sublist in detailsList for ent in sublist] # Step 3: Don't forget to close pool.close() diff = size - len(idList) if len(detailsList) > diff: detailsList = detailsList[0:diff] if notEmails: for email in notEmails: detailsList.append(email) return detailsList
def sender(self, email): return self.predicate.match(email.get('From'))
def _test_encoding(self, subject_header, parsed_subject): email = self._create_email(subject_header) name, _ = clean_subject(email.get('Subject')) self.assertEqual(name, parsed_subject)
def getDate(self, email): return parser.parse(email.get('date'))
def getDate(email): return parser.parse(email.get('date'))
def extract_date(email): date = email.get('Date') return parsedate(date)
def obtenerFecha(email): return parser.parse(email.get('date'))
def send(self, auto_commit=False, raise_exception=False): """ Sends the selected emails immediately, ignoring their current state (mails that have already been sent should not be passed unless they should actually be re-sent). Emails successfully delivered are marked as 'sent', and those that fail to be deliver are marked as 'exception', and the corresponding error mail is output in the server logs. :param bool auto_commit: whether to force a commit of the mail status after sending each mail (meant only for scheduler processing); should never be True during normal transactions (default: False) :param bool raise_exception: whether to raise an exception if the email sending process has failed :return: True """ IrMailServer = self.env['ir.mail_server'] for mail in self: try: # TDE note: remove me when model_id field is present on mail.message - done here to avoid doing it multiple times in the sub method if mail.model: model = self.env['ir.model'].sudo().search([ ('model', '=', mail.model) ])[0] else: model = None if model: mail = mail.with_context(model_name=model.name) # load attachment binary data with a separate read(), as prefetching all # `datas` (binary field) could bloat the browse cache, triggerring # soft/hard mem limits with temporary data. attachments = [(a['datas_fname'], base64.b64decode(a['datas'])) for a in mail.attachment_ids.sudo().read( ['datas_fname', 'datas'])] # specific behavior to customize the send email for notified partners email_list = [] if mail.email_to: email_list.append(mail.send_get_email_dict()) for partner in mail.recipient_ids: email_list.append( mail.send_get_email_dict(partner=partner)) # headers headers = {} bounce_alias = self.env['ir.config_parameter'].get_param( "mail.bounce.alias") catchall_domain = self.env['ir.config_parameter'].get_param( "mail.catchall.domain") if bounce_alias and catchall_domain: if mail.model and mail.res_id: headers['Return-Path'] = '%s+%d-%s-%d@%s' % ( bounce_alias, mail.id, mail.model, mail.res_id, catchall_domain) else: headers['Return-Path'] = '%s+%d@%s' % ( bounce_alias, mail.id, catchall_domain) if mail.headers: try: headers.update(safe_eval(mail.headers)) except Exception: pass # Writing on the mail object may fail (e.g. lock on user) which # would trigger a rollback *after* actually sending the email. # To avoid sending twice the same email, provoke the failure earlier mail.write({ 'state': 'exception', 'failure_reason': _('Error without exception. Probably due do sending an email without computed recipients.' ), }) mail_sent = False # build an RFC2822 email.message.Message object and send it without queuing res = None for email in email_list: msg = IrMailServer.build_email( email_from=mail.email_from, email_to=email.get('email_to'), subject=mail.subject, body=email.get('body'), body_alternative=email.get('body_alternative'), email_cc=tools.email_split(mail.email_cc), email_bcc=tools.email_split(mail.email_bcc), reply_to=mail.reply_to, attachments=attachments, message_id=mail.message_id, references=mail.references, object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)), subtype='html', subtype_alternative='plain', headers=headers) try: res = IrMailServer.send_email( msg, mail_server_id=mail.mail_server_id.id) except AssertionError as error: if error.message == IrMailServer.NO_VALID_RECIPIENT: # No valid recipient found for this particular # mail item -> ignore error to avoid blocking # delivery to next recipients, if any. If this is # the only recipient, the mail will show as failed. _logger.info( "Ignoring invalid recipients for mail.mail %s: %s", mail.message_id, email.get('email_to')) else: raise if res: mail.write({ 'state': 'sent', 'message_id': res, 'failure_reason': False }) mail_sent = True # /!\ can't use mail.state here, as mail.refresh() will cause an error # see revid:[email protected] in 6.1 if mail_sent: _logger.info( 'Mail with ID %r and Message-Id %r successfully sent', mail.id, mail.message_id) mail._postprocess_sent_message(mail_sent=mail_sent) except MemoryError: # prevent catching transient MemoryErrors, bubble up to notify user or abort cron job # instead of marking the mail as failed _logger.exception( 'MemoryError while processing mail with ID %r and Msg-Id %r. Consider raising the --limit-memory-hard startup option', mail.id, mail.message_id) raise except psycopg2.Error: # If an error with the database occurs, chances are that the cursor is unusable. # This will lead to an `psycopg2.InternalError` being raised when trying to write # `state`, shadowing the original exception and forbid a retry on concurrent # update. Let's bubble it. raise except Exception as e: failure_reason = tools.ustr(e) _logger.exception('failed sending mail (id: %s) due to %s', mail.id, failure_reason) mail.write({ 'state': 'exception', 'failure_reason': failure_reason }) mail._postprocess_sent_message(mail_sent=False) if raise_exception: if isinstance(e, AssertionError): # get the args of the original error, wrap into a value and throw a MailDeliveryException # that is an except_orm, with name and value as arguments value = '. '.join(e.args) raise MailDeliveryException(_("Mail Delivery Failed"), value) raise if auto_commit is True: self._cr.commit() return True
raw_email = data[0][1] email_message = email.message_from_string(raw_email) print email_message html = email_message soup = BeautifulSoup(str(html)) print soup.prettify() parser = Parser() email = parser.parsestr(raw_email) print "From: ", email.get('From') print "To: ", email.get('To') print "Subject: ", email.get('Subject') if email.is_multipart(): for payloads in email.get_payload(): # if payload.is_multipart(): ... print "Body: ", payloads.get_payload() else: print "Body: ",email.get_payload() k = id v = raw_email
def login_success(): mail.login(email.get(),password.get()) print ("login success")
if rv == 'OK': print "Mailboxes:" print mailboxes mail.list() #Lists the folders in the gmail mail.select("inbox") # connect to inbox. result, data = mail.uid('search', None, "ALL") # search and return uids instead latest_email_uid = data[0].split()[-1] result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = data[0][1] from email.parser import Parser parser = Parser() emailText = raw_email email = parser.parsestr(emailText) print email.get('From') print email.get('To') print email.get('Subject') '''if email.is_multipart(): for part in email.get_payload(): print part.get_payload() else: print email.get_payload() ''' exml =lxml.html.fromstring(text) exml.test_content() email.logout
print mailboxes mail.list() #Lists the folders in the gmail mail.select("inbox") # connect to inbox. result, data = mail.uid('search', None, "ALL") # search and return uids instead latest_email_uid = data[0].split()[-1] result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = data[0][1] from email.parser import Parser parser = Parser() emailText = raw_email email = parser.parsestr(emailText) print email.get('From') print email.get('To') print email.get('Subject') '''if email.is_multipart(): for part in email.get_payload(): print part.get_payload() else: print email.get_payload() ''' exml = lxml.html.fromstring(text) exmil.text_cintent() email.logout
#!/usr/bin/python from boto.s3.connection import S3Connection import email from identity import * emails = [] aws_connection = S3Connection(AWS_KEY, AWS_SECRET) bucket = aws_connection.get_bucket(BUCKET) for file_key in bucket.list(): print file_key.name emails.append(email.message_from_string(file_key.get_contents_as_string())) print "ID\tFrom\t\t\tTo\t\t\tSubject" index = 0 for email in emails: print "%04d\t%.20s\t%.20s\t%.40s\n" % ( index, email.get('From'), email.get('To'), email.get('Subject')) index += 1
mail.select("INBOX") # connect to inbox. result, data = mail.uid('search', None, "ALL") # search and return uids uids = data[0].split() for uid in uids: result, data = mail.uid('fetch', uid, '(RFC822)') raw_email = data[0][1] parser = Parser() emailText = raw_email email = parser.parsestr(emailText) sender_realname, sender = parseaddr(email.get('From')) if sender not in contacts.keys(): contacts[sender] = {} contacts[sender]['email'] = sender contacts[sender]['realname'] = sender_realname contacts[sender]['gravatar'] = "https://www.gravatar.com/avatar/" + md5(sender.strip().lower()).hexdigest() contacts[sender]['mails'] = [] contacts[sender]['mails'].append((email.get('Subject'), email.get('Date'))) ##### Web Frontend app = Flask(__name__) @app.route("/")
def getSubj(which): # return subject of message with id 'which' msg = "\n".join(M.top(which, 1)[1]) email = parser.parsestr(msg) return email.get("Subject")
def send(self, cr, uid, ids, auto_commit=False, raise_exception=False, context=None): """ Sends the selected emails immediately, ignoring their current state (mails that have already been sent should not be passed unless they should actually be re-sent). Emails successfully delivered are marked as 'sent', and those that fail to be deliver are marked as 'exception', and the corresponding error mail is output in the server logs. :param bool auto_commit: whether to force a commit of the mail status after sending each mail (meant only for scheduler processing); should never be True during normal transactions (default: False) :param bool raise_exception: whether to raise an exception if the email sending process has failed :return: True """ context = dict(context or {}) context['model_name'] = 'crm.helpdesk' if context.get('default_model', False) == 'crm.helpdesk' and 'default_res_id' in context: ir_mail_server = self.pool.get('ir.mail_server') ir_attachment = self.pool['ir.attachment'] for mail in self.browse(cr, SUPERUSER_ID, ids, context=context): try: # TDE note: remove me when model_id field is present on mail.message - done here to avoid doing it multiple times in the sub method if mail.model: model_id = self.pool['ir.model'].search(cr, SUPERUSER_ID, [('model', '=', mail.model)], context=context)[0] model = self.pool['ir.model'].browse(cr, SUPERUSER_ID, model_id, context=context) else: model = None if model: context['model_name'] = model.name # load attachment binary data with a separate read(), as prefetching all # `datas` (binary field) could bloat the browse cache, triggerring # soft/hard mem limits with temporary data. attachment_ids = [a.id for a in mail.attachment_ids] attachments = [(a['datas_fname'], base64.b64decode(a['datas'])) for a in ir_attachment.read(cr, SUPERUSER_ID, attachment_ids, ['datas_fname', 'datas'])] # specific behavior to customize the send email for notified partners email_list = [] if mail.email_to: email_list.append(self.send_get_email_dict(cr, uid, mail, context=context)) for partner in mail.recipient_ids: email_list.append(self.send_get_email_dict(cr, uid, mail, partner=partner, context=context)) # headers headers = {} bounce_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.bounce.alias", context=context) catchall_domain = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.catchall.domain", context=context) if bounce_alias and catchall_domain: if mail.model and mail.res_id: headers['Return-Path'] = '%s-%d-%s-%d@%s' % (bounce_alias, mail.id, mail.model, mail.res_id, catchall_domain) else: headers['Return-Path'] = '%s-%d@%s' % (bounce_alias, mail.id, catchall_domain) if mail.headers: try: headers.update(eval(mail.headers)) except Exception: pass # Writing on the mail object may fail (e.g. lock on user) which # would trigger a rollback *after* actually sending the email. # To avoid sending twice the same email, provoke the failure earlier mail.write({'state': 'exception'}) mail_sent = False # build an RFC2822 email.message.Message object and send it without queuing res = None for email in email_list: email_sub = email.get('subject') body = '' if mail.mail_message_id.model == 'crm.helpdesk': message_pool = self.pool.get('mail.message') message_ids = message_pool.search(cr, SUPERUSER_ID, [ ('model', '=', mail.mail_message_id.model), ('res_id', '=', context.get('default_res_id')), ], context=context) for message_id in message_pool.browse(cr, uid, message_ids[1:], context=context): body += "<div style='margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex'><br><br>On %s " % message_id.date body += message_id.body + "</div>" email.update({'body' : email.get('body') + ustr(body)}) # start custom code for send mail from 'Email Sent From' field helpdesk_obj = self.pool.get('crm.helpdesk').browse(cr, uid, context.get('default_res_id'), context=context) if context.get('default_res_id', False): email_sub = ('['+'Case'+ ' ' + str(context.get('default_res_id'))+']') + ' '+ (helpdesk_obj.name) email_from1 = '' reply_to1 = '' crm_helpdesk_mails = self.pool.get('crm.helpdesk.emails').search(cr, uid, []) if crm_helpdesk_mails: crm_helpdesk_browse = self.pool.get('crm.helpdesk.emails').browse(cr, uid, crm_helpdesk_mails[0]) email_from1 = crm_helpdesk_browse.sent_from or '' if crm_helpdesk_browse.reply_to: reply_to1 = crm_helpdesk_browse.reply_to else: reply_to1 = crm_helpdesk_browse.sent_from else: email_from1 = mail.email_from reply_to1 = mail.reply_to #################TO CHANGE SUBJECT FOR HELPDESK ################ msg = ir_mail_server.build_email( email_from=email_from1, email_to=email.get('email_to'), subject= email_sub,#email.get('subject'), body= email.get('body'), body_alternative=email.get('body_alternative'), email_cc=tools.email_split(mail.email_cc), reply_to=reply_to1, attachments=attachments, message_id=mail.message_id, references=mail.references, object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)), subtype='html', subtype_alternative='plain', headers=headers) try: #check for ir mail server if is it configure or not if not then take default ################### ADDED TO SPLIT MAIL ID FROM THE STRING ############################# if email_from1.partition('<')[2].partition('>')[0].strip(): mail_frm = email_from1.partition('<')[2].partition('>')[0].strip() else: mail_frm = email_from1 ################################################################## server_id = ir_mail_server.search(cr, uid, [('smtp_user','=', mail_frm)]) if server_id: res = ir_mail_server.send_email(cr, uid, msg, mail_server_id=server_id, context=context) else: res = ir_mail_server.send_email(cr, uid, msg, mail_server_id=mail.mail_server_id.id, context=context) except AssertionError as error: if error.message == ir_mail_server.NO_VALID_RECIPIENT: # No valid recipient found for this particular # mail item -> ignore error to avoid blocking # delivery to next recipients, if any. If this is # the only recipient, the mail will show as failed. _logger.warning("Ignoring invalid recipients for mail.mail %s: %s", mail.message_id, email.get('email_to')) else: raise if res: mail.write({'state': 'sent', 'message_id': res}) mail_sent = True # /!\ can't use mail.state here, as mail.refresh() will cause an error # see revid:[email protected] in 6.1 if mail_sent: _logger.info('Mail with ID %r and Message-Id %r successfully sent', mail.id, mail.message_id) self._postprocess_sent_message(cr, uid, mail, context=context, mail_sent=mail_sent) except MemoryError: # prevent catching transient MemoryErrors, bubble up to notify user or abort cron job # instead of marking the mail as failed _logger.exception('MemoryError while processing mail with ID %r and Msg-Id %r. '\ 'Consider raising the --limit-memory-hard startup option', mail.id, mail.message_id) raise except Exception as e: _logger.exception('failed sending mail.mail %s', mail.id) mail.write({'state': 'exception'}) self._postprocess_sent_message(cr, uid, mail, context=context, mail_sent=False) if raise_exception: if isinstance(e, AssertionError): # get the args of the original error, wrap into a value and throw a MailDeliveryException # that is an except_orm, with name and value as arguments value = '. '.join(e.args) raise MailDeliveryException(_("Mail Delivery Failed"), value) raise if auto_commit is True: cr.commit() return True return super(mail_mail, self).send(cr, uid, ids, auto_commit, raise_exception, context=context)
def dkim_present(self, email): # check for dkim header if email.get('DKIM-Signature') is not None: return True
#!/usr/bin/python from boto.s3.connection import S3Connection import email from identity import * emails = [] aws_connection = S3Connection(AWS_KEY, AWS_SECRET) bucket = aws_connection.get_bucket(BUCKET) for file_key in bucket.list(): print file_key.name emails.append(email.message_from_string(file_key.get_contents_as_string())) print "ID\tFrom\t\t\tTo\t\t\tSubject" index = 0 for email in emails: print "%04d\t%.20s\t%.20s\t%.40s\n" % (index, email.get("From"), email.get("To"), email.get("Subject")) index += 1
def cc(self, email): return self.predicate.match(email.get('Cc'))
def get_date(email): return parser.parse(email.get('date'))
def replyTo(self, email): return self.predicate.match(email.get('Reply-To'))
def subject(self, email): return self.predicate.match(email.get('Subject'))
mail.select("INBOX") # connect to inbox. result, data = mail.uid('search', None, "ALL") # search and return uids uids = data[0].split() for uid in uids: result, data = mail.uid('fetch', uid, '(RFC822)') raw_email = data[0][1] parser = Parser() emailText = raw_email email = parser.parsestr(emailText) sender_realname, sender = parseaddr(email.get('From')) if sender not in contacts.keys(): contacts[sender] = {} contacts[sender]['email'] = sender contacts[sender]['realname'] = sender_realname contacts[sender][ 'gravatar'] = "https://www.gravatar.com/avatar/" + md5( sender.strip().lower()).hexdigest() contacts[sender]['mails'] = [] contacts[sender]['tel'] = [] contacts[sender]['mails'].append((email.get('Subject'), email.get('Date'))) if email.is_multipart(): for part in email.get_payload():
def to(self, email): return self.predicate.match(email.get('To'))
def checkName(): subject = email.get('Subject') print subject
def xSpamFlag(self, email): return self.predicate.match(email.get('X-Spam-Flag'))
def getDate(self, email): return parser.parse(email.get("date"))
def get_sender(self, email): sender, enc = decode_header(email.get('From'))[0] return sender