Send mail documented
This commit is contained in:
parent
4812a8d14f
commit
5966869ebc
|
@ -14,5 +14,96 @@ Siempre guarda las contraseñas de la forma más segura posible.
|
||||||
Enviar un correo electrónico.
|
Enviar un correo electrónico.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from conf import PASSWORD
|
||||||
|
|
||||||
|
SERVER = dict(
|
||||||
|
server = 'mail.correo.net' ,
|
||||||
|
port = 405,
|
||||||
|
ssl = True,
|
||||||
|
user = 'no-responder@pruebas.mx',
|
||||||
|
password = PASSWORD,
|
||||||
|
)
|
||||||
|
|
||||||
|
body = "Hola Ingrid\n\n¿Como estas?\n\nSaludos cordiales"
|
||||||
|
|
||||||
|
message = dict(
|
||||||
|
to = 'ingrid@bergman.mx',
|
||||||
|
subject = 'Te amo...',
|
||||||
|
body = body,
|
||||||
|
)
|
||||||
|
|
||||||
|
app.email.send(SERVER, message)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Puede usar los campos `cc` (Con Copia) y `bcc` (Con Copia Oculta), así como separar por comas las direcciones destino para enviar a más de un correo.
|
||||||
|
|
||||||
|
```python
|
||||||
|
to = 'mail1@correo.com,mail2@correo.com,mail3@correo.com'
|
||||||
|
cc = 'otro@correo.com'
|
||||||
|
bcc = 'oculto@correo.com'
|
||||||
|
```
|
||||||
|
|
||||||
|
Podemos enviar más de un mensaje.
|
||||||
|
|
||||||
|
```python
|
||||||
|
mensaje1 = dict(
|
||||||
|
to = 'ingrid@correo.net',
|
||||||
|
subject = 'Te amo...',
|
||||||
|
body = "Hola Ingrid\n\n¿Como estas?\n\nSaludos cordiales",
|
||||||
|
)
|
||||||
|
mensaje2 = dict(
|
||||||
|
to = 'sophia@correo.net',
|
||||||
|
subject = 'A ti también...',
|
||||||
|
body = "Hola Sophia\n\n¿Como estas?\n\nSaludos cordiales",
|
||||||
|
)
|
||||||
|
mensajes = (mensaje1, mensaje2)
|
||||||
|
|
||||||
|
app.email.send(SERVER, mensajes)
|
||||||
|
```
|
||||||
|
```
|
||||||
|
16/08/2022 13:01:49 - DEBUG - Connect to: mail.server.net
|
||||||
|
16/08/2022 13:01:50 - DEBUG - Email sent...
|
||||||
|
16/08/2022 13:01:51 - DEBUG - Email sent...
|
||||||
|
16/08/2022 13:01:51 - DEBUG - Close connection...
|
||||||
|
```
|
||||||
|
|
||||||
|
Enviar un archivo adjunto.
|
||||||
|
|
||||||
|
```python
|
||||||
|
archivo = '/home/mau/temp.txt'
|
||||||
|
mensaje = dict(
|
||||||
|
to = 'ingrid@bergman.mx',
|
||||||
|
subject = 'Te amo...',
|
||||||
|
body = "Hola Ingrid\n\n¿Como estas?\n\nSaludos cordiales",
|
||||||
|
files = archivo,
|
||||||
|
)
|
||||||
|
|
||||||
|
app.email.send(SERVER, mensaje)
|
||||||
|
```
|
||||||
|
|
||||||
|
O varios.
|
||||||
|
|
||||||
|
```python
|
||||||
|
archivos = (
|
||||||
|
'/home/mau/temp.txt',
|
||||||
|
'/home/mau/datos.csv',
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Si tu cliente de correo usa el formato `mbox`, puede guardar los correos enviados en una carpeta dentro de la configuración de su cliente de correo.
|
||||||
|
|
||||||
|
```python
|
||||||
|
ruta = '/home/mau/.thunderbird/7iznrbyw.default/Mail/Local Folders/LibreOffice'
|
||||||
|
mensaje = dict(
|
||||||
|
to = 'ingrid@bergman.mx',
|
||||||
|
subject = 'Te amo...',
|
||||||
|
body = "Hola Ingrid\n\n¿Como estas?\n\nSaludos cordiales",
|
||||||
|
path = ruta,
|
||||||
|
)
|
||||||
|
|
||||||
|
app.email.send(SERVER, mensaje)
|
||||||
|
```
|
||||||
|
|
||||||
|
{{% notice tip %}}
|
||||||
|
Todos los correos se envían siempre en otro hilo de ejecución.
|
||||||
|
{{% /notice %}}
|
||||||
|
|
|
@ -31,8 +31,7 @@ import shlex
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
|
|
||||||
import tempfile
|
|
||||||
import threading
|
|
||||||
|
|
||||||
from string import Template
|
from string import Template
|
||||||
|
|
||||||
|
@ -41,35 +40,18 @@ from urllib import parse
|
||||||
from urllib.request import Request, urlopen
|
from urllib.request import Request, urlopen
|
||||||
from urllib.error import URLError, HTTPError
|
from urllib.error import URLError, HTTPError
|
||||||
|
|
||||||
import mailbox
|
|
||||||
import smtplib
|
|
||||||
from smtplib import SMTPException, SMTPAuthenticationError
|
|
||||||
from email.mime.multipart import MIMEMultipart
|
|
||||||
from email.mime.base import MIMEBase
|
|
||||||
from email.mime.text import MIMEText
|
|
||||||
from email.utils import formatdate
|
|
||||||
from email import encoders
|
|
||||||
|
|
||||||
|
|
||||||
from com.sun.star.awt import Rectangle, Size, Point
|
from com.sun.star.awt import Rectangle, Size, Point
|
||||||
from com.sun.star.awt import Key, KeyEvent, KeyModifier
|
from com.sun.star.awt import Key, KeyEvent, KeyModifier
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from com.sun.star.datatransfer import XTransferable, DataFlavor
|
from com.sun.star.datatransfer import XTransferable, DataFlavor
|
||||||
from com.sun.star.io import IOException, XOutputStream
|
from com.sun.star.io import IOException, XOutputStream
|
||||||
|
|
||||||
|
|
||||||
from com.sun.star.sheet import XRangeSelectionListener
|
from com.sun.star.sheet import XRangeSelectionListener
|
||||||
|
|
||||||
|
|
||||||
from com.sun.star.container import NoSuchElementException
|
from com.sun.star.container import NoSuchElementException
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TIMEOUT = 10
|
|
||||||
SALT = b'00a1bfb05353bb3fd8e7aa7fe5efdccc'
|
SALT = b'00a1bfb05353bb3fd8e7aa7fe5efdccc'
|
||||||
|
|
||||||
_EVENTS = {}
|
_EVENTS = {}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
@ -57,6 +58,7 @@ __all__ = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
TIMEOUT = 10
|
||||||
PYTHON = 'python'
|
PYTHON = 'python'
|
||||||
if IS_WIN:
|
if IS_WIN:
|
||||||
PYTHON = 'python.exe'
|
PYTHON = 'python.exe'
|
||||||
|
@ -1336,12 +1338,13 @@ class Email():
|
||||||
if isinstance(paths, str):
|
if isinstance(paths, str):
|
||||||
paths = (paths,)
|
paths = (paths,)
|
||||||
for path in paths:
|
for path in paths:
|
||||||
fn = _P(path).file_name
|
fn = Paths(path).file_name
|
||||||
# ~ print('NAME', fn)
|
# ~ print('NAME', fn)
|
||||||
part = MIMEBase('application', 'octet-stream')
|
part = MIMEBase('application', 'octet-stream')
|
||||||
part.set_payload(_P.read_bin(path))
|
part.set_payload(Paths.read_bin(path))
|
||||||
encoders.encode_base64(part)
|
encoders.encode_base64(part)
|
||||||
part.add_header('Content-Disposition', f'attachment; filename="{fn}"')
|
part.add_header(
|
||||||
|
'Content-Disposition', f'attachment; filename="{fn}"')
|
||||||
email.attach(part)
|
email.attach(part)
|
||||||
|
|
||||||
receivers = (
|
receivers = (
|
||||||
|
|
Loading…
Reference in New Issue