Ir al contenido

Cuando el servidor de correo dice que no ​

Traducción automática

Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.

Tarea: un pedido debe guardarse aunque no se pueda enviar el correo de confirmación — y el visitante debe enterarse, no recibir una página de error.

Por defecto, un q:mail que el servidor no acepta detiene la acción con el motivo del servidor. onerror="continue" deja que la acción siga y pone el resultado en <name>_result: success, y error.message cuando falló.

Esta receta apunta a un servidor de correo que no existe, así que todos los mensajes fallan — como pasa cuando el servidor real está caído:

yaml
# Recipe: an order is saved even when the confirmation e-mail cannot be sent.
# The mail server here is deliberately unreachable (nothing listens on port 9),
# so every q:mail fails — as it does when the real server is down.
paths:
  components: ./components
  migrations: ./migrations

datasources:
  db:
    driver: sqlite
    database: ./data/orders.db

mail:
  host: 127.0.0.1
  port: 9
  tls: false
  timeout: 2
  from: shop@example.com
sql
CREATE TABLE orders (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    email TEXT NOT NULL,
    item TEXT NOT NULL
);
xml
<q:component name="Order">
  <q:action name="order" method="POST">
    <q:param name="email" type="email" required="true" />
    <q:param name="item" required="true" />
    <q:query name="saved" datasource="db">
      INSERT INTO orders (email, item) VALUES (:email, :item)
      <q:param name="email" value="{email}" type="string" />
      <q:param name="item" value="{item}" type="string" />
    </q:query>

    <!-- onerror="continue": a refused message does not undo the order -->
    <q:mail name="confirmation" to="{email}" subject="Your order: {item}"
            type="text" onerror="continue">We received your order for {item}.</q:mail>

    <q:if condition="confirmation_result.success">
      <q:redirect url="/" flash="Ordered {item}. A confirmation is on its way." />
    </q:if>
    <q:redirect url="/" flash="Ordered {item}. We could not send the confirmation e-mail." />
  </q:action>

  <h1>Order</h1>
  <q:if condition="flash"><p class="flash">{flash}</p></q:if>
  <form method="POST" action="/?action=order">
    <input name="email" type="email" />
    <input name="item" />
    <button>Order</button>
  </form>
</q:component>

El pedido se inserta antes del correo, y el mensaje flash dice cómo salió. La prueba verifica las dos cosas — la fila está ahí, y se le avisó al visitante:

xml
<q:test name="the order is saved and the visitor told the e-mail failed" page="/">
  <test:submit action="order" email="ana@example.com" item="Blue mug" />
  <test:expect redirect="/" flash="Ordered Blue mug. We could not send the confirmation e-mail." />
  <test:expect table="orders" count="1" where="email = 'ana@example.com' AND item = 'Blue mug'" />
</q:test>
text
tests/order.test.q
  PASS  the order is saved and the visitor told the e-mail failed
1 passed, 0 failed

En desarrollo, host: log evita el fallo por completo; onerror="continue" es para el día en que el servidor real está caído. Más en Files & Mail (en inglés).

Probado: esta página importa los archivos de examples/cookbook/files-and-mail/mail-server-refuses/, y el resultado de arriba viene de ejecutarlos.

Licencia MIT · Hecho con VitePress