> For the complete documentation index, see [llms.txt](https://gyansetu-python.gitbook.io/python-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-python.gitbook.io/python-programming/email-automation.md).

# Email Automation

#### Sending mail via gmail

```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = '''Hello, Mail is sent!!
'''

#The mail addresses and password
sender_address = 'sahil.6906@gmail.com'
sender_pass = ''
receiver_address = ''

#Setup the MIME
message = MIMEMultipart()
message['From'] = 'sahil.6906@gmail.com'
message['To'] = ''
message['Subject'] = 'Test mail'   #The subject line

#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))

#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()

print("mail sent")
```

#### Sending Mail via gmail with attachment

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

mail_content = '''Hello, Mail is sent!!
'''

#The mail addresses and password
sender_address = 'sahil.6906@gmail.com'
sender_pass = ''
receiver_address = ''

#Setup the MIME
message = MIMEMultipart()
message['From'] = 'sahil.6906@gmail.com'
message['To'] = 'agarwalshalki@gmail.com'
message['Subject'] = 'Test mail'   #The subject line

# attach the body with the msg instance 
message.attach(MIMEText(mail_content, 'plain')) 

# open the file to be sent
filename = "type conversion.xlsx"
attachment = open("C:/sahil/type conversion.xlsx", "rb") 

# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream') 

# To change the payload into encoded form
p.set_payload((attachment).read()) 

# encode into base64 
encoders.encode_base64(p)

# Add header as key/value pair to attachment part
p.add_header('Content-Disposition', "attachment; filename= %s" % filename) 
message.attach(p) 

s = smtplib.SMTP('smtp.gmail.com', 587) 
s.starttls() 
s.login(sender_address, sender_pass) 
text = message.as_string() 
s.sendmail(sender_address, receiver_address, text) 
s.quit() 
print("mail sent")
```

#### Sending one mail to multiple email ids in excel

```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import xlrd

loc = ("C:/sahil/student details.xlsx") 
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)

for a in range(1,sheet.nrows):

    mail_content = '''Hello, Mail is sent!!
    '''

    #The mail addresses and password
    sender_address = 'sahil.6906@gmail.com'
    sender_pass = ''
    receiver_address = sheet.cell_value(a, 1)

    #Setup the MIME
    message = MIMEMultipart()
    message['From'] = 'sahil.6906@gmail.com'
    message['To'] = sheet.cell_value(a, 1)
    message['Subject'] = sheet.cell_value(a, 0)   #The subject line

    #The body and the attachments for the mail
    message.attach(MIMEText(mail_content, 'plain'))

    #Create SMTP session for sending the mail
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
    session.starttls() #enable security
    session.login(sender_address, sender_pass) #login with mail_id and password
    text = message.as_string()
    session.sendmail(sender_address, receiver_address, text)
    session.quit()

print("mail sent")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gyansetu-python.gitbook.io/python-programming/email-automation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
