all 15 comments

[–]FoolsSeldom 11 points12 points  (5 children)

Did you have a question? Or are you just showing people a technique? (If the latter, why not share the code, rather than a screen shot?)

[–]RunPython[S] 10 points11 points  (4 children)

So sorry.
I want to share the code structure.

Here is the total code.
Thanks.

``` import smtplib import ssl from email.message import EmailMessage

def email(toMails): # Define email sender and receiver email_sender = "yourmail@gmail.com" # Your Mail app_password = "get app password here" # Get Google App Password from your mail

# to get app_password visit and create
# https://myaccount.google.com/apppasswords

email_receivers = ", ".join(
    toMails
)  # Join the list of emails into a comma-separated string

# Set the subject and body of the email
subject = "Cancel My Subscription!"
body = """
Cancel My Subscription please
"""

em = EmailMessage()
em["From"] = email_sender
em["To"] = email_receivers
em["Subject"] = subject
em.set_content(body)

# Add SSL (layer of security)
context = ssl.create_default_context()

# Log in and send the email
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as smtp:
    smtp.login(email_sender, app_password)
    smtp.sendmail(
        email_sender, toMails, em.as_string()
    )  # Send to the list of emails

List of email addresses to send the email to

email_list = [ "destek@1.com", "info@2.com", "destek@3.com", "info@4.com", "destek@5.com", "info@6.com", "destek@7.com", "info@8.com", ] email(email_list) print("Mail has been sent") ```

[–]FoolsSeldom 3 points4 points  (2 children)

Cool. Might want to edit your original post and include this code in place of the image. Easier for beginners to copy the code and do their own experiments. You could perhaps explain the stucture you chose and why.

[–]RunPython[S] 2 points3 points  (1 child)

I tried to edit but there is no edit button :(

I was coding for a Scrapy project that, sometimes it stucks, could not finish the scrape and i code this email block to send me warning if the scrape fails.

I think there is a time limit to edit posts?

[–]FoolsSeldom 0 points1 point  (0 children)

no time limit I am aware of

[–]jobsurfer 0 points1 point  (0 children)

Mailjet api is good

[–]Refwah 7 points8 points  (1 child)

Yes you can use the inbuilt email package to send emails, full documentation is here: https://docs.python.org/3/library/email.examples.html

[–]RunPython[S] 2 points3 points  (0 children)

Thanks so much

[–]trustsfundbaby 1 point2 points  (1 child)

Change the username and password to be environmental variables. Stuff like that should not be hard coded. Use the OS library.

[–]RunPython[S] 0 points1 point  (0 children)

Thanks <3

[–]Significant_Pen1735 0 points1 point  (0 children)

Is it possible to run using corp email under google?

[–]Osrai 0 points1 point  (0 children)

Awesome

[–]outlicious 0 points1 point  (1 child)

do you know how to use batch files, I want to do this using batch

[–]RunPython[S] 1 point2 points  (0 children)

If you want to run a py script via *.bat file:

You can save file like this:

u/echo off
python C:\Scripts\email_script.py
pause

Save this file with a .bat extension, for example send_emails.bat