# Flask Mail

***

**1. Simple Email Sending**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_email')
def send_mail():
    msg = Message('Hello', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email.'
    mail.send(msg)
    return 'Email sent!'
```

**2. HTML Email Sending**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_html_email')
def send_html_email():
    msg = Message('HTML Email', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.html = '<p>This is an HTML email.</p>'
    mail.send(msg)
    return 'HTML Email sent!'
```

**3. Attachment Email Sending**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_attachment_email')
def send_attachment_email():
    msg = Message('Attachment Email', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.attach('document.pdf')
    mail.send(msg)
    return 'Attachment Email sent!'
```

**4. Email with Attachments and HTML**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_complex_email')
def send_complex_email():
    msg = Message('Complex Email', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.html = '<p>This is an HTML email with an attachment.</p>'
    msg.attach('document.pdf')
    mail.send(msg)
    return 'Complex Email sent!'
```

**5. Email With Multiple Recipients**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_multi_recipient_email')
def send_multi_recipient_email():
    msg = Message('Message to Multiple Recipients', sender='sender@example.com')
    msg.recipients = ['recipient1@example.com', 'recipient2@example.com', 'recipient3@example.com']
    msg.body = 'This is a message sent to multiple recipients.'
    mail.send(msg)
    return 'Email sent to multiple recipients!'
```

**6. Email With Custom Headers**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_custom_header_email')
def send_custom_header_email():
    msg = Message('Email with Custom Headers', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This email has custom headers.'
    msg.headers['X-Custom-Header'] = 'custom-header-value'
    mail.send(msg)
    return 'Email sent with custom headers!'
```

**7. Email With Attachments and Custom Headers**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_complex_custom_header_email')
def send_complex_custom_header_email():
    msg = Message('Complex Email with Custom Headers', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.html = '<p>This is an HTML email with an attachment and custom headers.</p>'
    msg.attach('document.pdf')
    msg.headers['X-Custom-Header'] = 'custom-header-value'
    mail.send(msg)
    return 'Complex Email with custom headers sent!'
```

**8. Email with BCC Recipients**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_bcc_email')
def send_bcc_email():
    msg = Message('Email with BCC Recipients', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email with BCC recipients.'
    msg.bcc = ['bcc1@example.com', 'bcc2@example.com']
    mail.send(msg)
    return 'Email sent with BCC recipients!'
```

**9. Email with CC Recipients**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_cc_email')
def send_cc_email():
    msg = Message('Email with CC Recipients', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email with CC recipients.'
    msg.cc = ['cc1@example.com', 'cc2@example.com']
    mail.send(msg)
    return 'Email sent with CC recipients!'
```

**10. Email with Reply-To Address**

```python
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_reply_to_email')
def send_reply_to_email():
    msg = Message('Email with Reply-To Address', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email with a reply-to address.'
    msg.reply_to = 'reply-to@example.com'
    mail.send(msg)
    return 'Email sent with reply-to address!'
```

**11. Email with Custom SMTP Server**

```python
from flask_mail import Mail, Message

mail = Mail(app, smtp_server='smtp.example.com', smtp_port=587, smtp_use_tls=True, smtp_username='username', smtp_password='password')

@app.route('/send_smtp_email')
def send_smtp_email():
    msg = Message('Email with Custom SMTP Server', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email sent using a custom SMTP server.'
    mail.send(msg)
    return 'Email sent with custom SMTP server!'
```

**12. Email with TLS Encryption**

```python
from flask_mail import Mail, Message

mail = Mail(app, smtp_use_tls=True)

@app.route('/send_tls_email')
def send_tls_email():
    msg = Message('Email with TLS Encryption', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email sent using TLS encryption.'
    mail.send(msg)
    return 'Email sent with TLS encryption!'
```

**13. Email with SSL Encryption**

```python
from flask_mail import Mail, Message

mail = Mail(app, smtp_use_ssl=True)

@app.route('/send_ssl_email')
def send_ssl_email():
    msg = Message('Email with SSL Encryption', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email sent using SSL encryption.'
    mail.send(msg)
    return 'Email sent with SSL encryption!'
```

**14. Email with SMTP Authentication**

```python
from flask_mail import Mail, Message

mail = Mail(app, smtp_user='username', smtp_password='password')

@app.route('/send_authenticated_email')
def send_authenticated_email():
    msg = Message('Email with SMTP Authentication', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.body = 'This is an email sent using SMTP authentication.'
    mail.send(msg)
    return 'Email sent with SMTP authentication!'
```

**15. Email with HTML Template**

```python
from flask import render_template, request
from flask_mail import Mail, Message

mail = Mail(app)

@app.route('/send_html_template_email')
def send_html_template_email():
    msg = Message('Email with HTML Template', sender='sender@example.com', recipients=['recipient@example.com'])
    msg.html = render_template('email.html')
    mail.send(msg)
    return 'Email sent with HTML template!'
```

**16. Email with Context Processor**

```python
from flask import g

```
