Giter Club home page Giter Club logo

gmailpy's People

Contributors

idutchy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

crrapi

gmailpy's Issues

Fixed the "Less Secure App Error"

I edited the code a bit to fix it.

import smtplib
import ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class Error(Exception):
    pass

class Client:
    def __init__(self, mail, password):
        self.mail = mail
        self.password = password
        
        
    async def send(self, receiver, body, subject=None, bcc=None, attachment_bytes=None, attachment_name=None):
        sender_email = self.mail
        password = self.password

        message = MIMEMultipart()
        message["From"] = sender_email
        message["To"] = receiver
        message["Subject"] = subject if subject else "No subject"
        message["Bcc"] = bcc

        message.attach(MIMEText(body, "plain"))
        
        if attachment_name is None and attachment_bytes is not None:
            raise Error('You did not provide an attachment name for your attachment!')
            
        if attachment_name is not None and attachment_bytes is None:
            raise Error('Can not set an attachment name without an attachment!')
        
        if attachment_bytes is not None and attachment_name is not None:
            part = MIMEBase("application", "octet-stream")
            part.set_payload(attachment_bytes)

            encoders.encode_base64(part)

            part.add_header(
                "Content-Disposition",
                f"attachment; filename= {attachment_name}",
            )

            message.attach(part)

        text = message.as_string()

        server = smtplib.SMTP(host="smtp.gmail.com", port=587)
        server.starttls()
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver, text)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.