-4

how can i stop smtplib from send with a command in discord py ?

Hello beautiful people , so i am coding a discord bot that can send emails on given smtp server and letter,maillist but lets say i have a list of 20 emails and i started sending , and i realized the email has something , a mistake , and i want to stop the process from w command without quiting the bot entirely

and i know that is server.quit()

i tried to put it in every single place even created a new command for it and i still cannot figure it out :3

This is my code over here! :

@bot.command()
async def smtp(ctx, stat=None):
    
smtp_server = await eee(ctx, f"{ctx.author.mention} Smtp server ?", timeout=60.0)
port = await eee(ctx, f"{ctx.author.mention} Smtp port ?", timeout=60.0)
user = await eee(ctx, f"{ctx.author.mention} Smtp user ?", timeout=60.0)
password = await eee(ctx, f"{ctx.author.mention} Smtp password ?", timeout=60.0)
    
    
def generate_messages(recipients):
  with open(letter_path, 'r') as myfile:
         data = myfile.read()

         for recipient in recipients:
            message = EmailMessage()
            message['Subject'] = letter_subject
            message['From'] = Address(letter_From, *user.split("@"))
            message['To'] = recipient
            message.set_content(data, 'html')                
            yield message   
    messages = generate_messages(mails)            
            
    if port == '587':
        
        with smtplib.SMTP(smtp_server, port) as server:
                 
                    try:
                 
                     server.login(user, password)
                     embed = discord.Embed(title=f"SMTP CONNECTION DONE",
                                    description=f"", color=3447003)
                                    
                     for message in messages:
                         now = datetime.now()
                         
                         server.send_message(message)
                         time.sleep(10)
                
                
                         
                         embed.add_field(name=" [+] ",
                         value=f" {message['To']}  SENT  {time.strftime('%X')}", inline=False)
                     await ctx.send(embed=embed)      
                     
                 
                    
                    except smtplib.SMTPException:
                        await ctx.send('DMTP DEAD')
    elif port == '465':
                    with smtplib.SMTP_SSL(smtp_server, port) as server:
                 
                     try:
                 
                        server.login(user, password)
                        embed = discord.Embed(title=f"SMTP CONNECTION DONE",
                                    description=f"", color=3447003)
                        await ctx.send(embed=embed)
                        for message in messages:
                         now = datetime.now()
                         current_time = now.strftime("%H:%M:%S")
                         
                         server.send_message(message)
                         time.sleep(10)
                
                         
                         embed2 = discord.Embed(title=" [+] SENT! [+] ", description=f" {message['To']}  SENT  {time.strftime('%X')}"
                         , color=3447003)
                          
                         await ctx.send(embed=embed2)   
                    
                         
                
                     except smtplib.SMTPException or stat == 'stop':
                        await ctx.send('SMTP DEAD OR stopped')
                 
    elif port == '465' or '587':
                await ctx.send('PORT NOT SUPPORTED')
                quit()
furas
  • 134,197
  • 12
  • 106
  • 148
yxxhixx
  • 29
  • 6
  • you may use some global variable `running = True` which you will check before sending every mail. And if you change `running = False` then you may stop/skip it. – furas Apr 02 '22 at 03:14
  • you create `server` as local variable - you would have to create it as `global` variable - and then you could access it from other functions. – furas Apr 02 '22 at 03:27

1 Answers1

0

Problem fixed with global variables @furas was the one with the idea :3

so we gotta desfine server_enabled = True than we define global server_enabled under every command that includes smtp server ,

Example :

    server_enabled = True
    @bot.command()
    async def start(ctx):
        global server_enabled
        server_enabled = True
    @bot.command()
    async def stop(ctx):
        global server_enabled
        server_enabled = False
    @bot.command()
    async def start(ctx):
        global server_enabled
        if server_enabled :
............ code
        else:
......... server.quit()

And thats it ♥

yxxhixx
  • 29
  • 6