Hack Forums
[Python] Sending an email using Python - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Sending an email using Python (/showthread.php?tid=5889288)



Sending an email using Python - g-ez - Sep 27, 2018

Hello guys! I've just started coding in python and I was wondering if I could send the output of this to an email address or save just this specific output as a .txt or .pdf file in my computer.

Maybe it's very complicated or very easy, I have no clue. I know I have import email libs and modules and smpt servers but guidance would be helpful as I can't seem to find specific answers on the internet about it. Thank you :)

FOR EXAMPLE:

def example():
s_kgs = input()
s_kgs_int = int(s_kgs)

totals1 = ((s_kgs_int 65) / 100)
totals2 = ((s_kgs_int
75) / 100)
totals3 = ((s_kgs_int * 85) / 100)

print("Example")
print("Row 1: "+format(totals1)+" x 6")
print("Row 2: "+format(totals2)+" x 6")
print("Row 3: "+format(totals3)+" x 6\n\n")

example()

This will be the output and what's going to be send over the email or is going to be saved in my files. (values are wrong but general idea):

Row 1: 65.0 x 6
Row 2: 75.0 x 6
Row 3: 85.0 x 6


RE: Sending an email using Python - Infamia - Sep 28, 2018

to save locally (to pc):

Code
with open("filename", "w") as output:
    output.write(result)

to send over email you'd either want to take a look at the gmail/yahoo/outlook/whatever API for python or look at `smtplib` which is a python standard library.


RE: Sending an email using Python - dtbmk - Sep 28, 2018

U should use MIMEMultipart() / MIMEText() for the text and then log to the email and send it.Probably u will have to change settings (gmail has a specific setting for that)


RE: Sending an email using Python - Hackoo - Sep 29, 2018

Take a look at this video :






RE: Sending an email using Python - ethnocentric - Sep 30, 2018

Here's a class from a project of mine. settings is a custom configuration object I use, you could just make a object and assign the properties needed or rework the class. Hope this helps. You'll probably wanna read up on some of the documentation for this here: https://docs.python.org/3/library/smtplib.html

Code
import smtplib

class EmailSender:
    
    def __init__(self, settings):    
        self.userName = settings.UserName
        self.password = settings.Password
        self.mailServer = settings.MailServer
        self.mailServerPort = settings.SMTPPort
        self.sslEnabled = settings.SMTPSSLEnabled

    def SendMail(self, recipient, subject, body):
        print('Recipient:' + str(recipient))
        to = str(recipient)
        if type(recipient) is str:
            to = recipient.split(";")
        print(to)

        # Prevents trying to send bad emails
        if to[0] == '':
            print("Missing Recipient")
            return

        msg = ('From: %s\nTo: %s\nSubject: %s\n\n\n%s\n' %
            (self.userName, to, subject, body))
        server = None
        if self.sslEnabled == 1:
            server = smtplib.SMTP_SSL(self.mailServer, self.mailServerPort)
        else:
            server = smtplib.SMTP(self.mailServer, self.mailServerPort)

        server.ehlo()
        server.login(self.userName, self.password)
        server.sendmail(self.userName, to, msg)
        server.quit()



RE: Sending an email using Python - g-ez - Oct 1, 2018

Thank you so much to all :D You guys are fucking awesome <3