swift

[smtp] 421 too many messages in this connection

Depois de bastante tempo com meu sistema de newsletter redondo.. tive hoje o seguinte erro:


Expected response code 250 but got code 421, with message 421 too many messages in this connection
 

Não sei qual foi o motivo disso acontecer assim de um dia para o outro.. Dei uma lida na documentação do Swift (http://swiftmailer.org/docs/antiflood-plugin-howto) e passei a utilizar o antiflood-plugin.

Foi bem simples. Utilizei da seguinte forma:



$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(200));

 

Com esse plugin a cada 200 emails é feito uma nova conexão. Isso é o que estava acusando no erro (421 too many messages in this connection).

valeu

swift

Comments (0)

Permalink

Utilizando Swift numa task (symfony 1.3/1.4)

Ao utilizar esse código na task:

$message = Swift_Message::newInstance();

Tive o seguinte erro:

PHP Fatal error: Class ‘Swift_Message’ not found in […]

Solução:

$message = $this->getMailer()->compose("from@mail","to@mail","subject","body");

cli
php
snippet
swift

Comments (0)

Permalink

symfony 1.0 e Swift 4

Ao utilizar o Swift 4 com o symfony 1.0, tive o seguinte erro:

Fatal error: Cannot instantiate abstract class Swift in /[…]/apps/frontend/modules/[…]/actions/actions.class.php on line 40

Não sei porque o autoload do symfony não funcionou. Só consigo utilizar a classe incluindo o swift_required.php

require_once(dirname(FILE).‘/../lib/swift/lib/swift_required.php’);

Se estiver tendo problemas de tela branca com o Swift, utilize:


error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);
 

Aqui vai um exemplo completo:


// swift 4.0.6
require_once(dirname(FILE).‘/../lib/swift/lib/swift_required.php’);

$transport = Swift_SmtpTransport::newInstance(‘smtp.servidor.com.br’, 25)
  ->setUsername(‘usuario’)
  ->setPassword(‘senha’)
  ->setTimeout(60);

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Titulo")
  ->setFrom("from@email.com")
  ->setTo("para@email.com")
  ->setBody("Mensagem", ‘text/html’);

$mailer->send($message);
 

Aqui fala um pouco sobre esse assunto: http://forum.symfony-project.org/index.php/m/77609/

php
swift

Comments (0)

Permalink

Enviando uma newsletter com Swift e Symfony

Olá! Nesse post vou mostrar uma maneira que utilizei para disparar uma newsletter com o Symfony utilizando o Swift . Atualmente esse processo funciona comigo para uma listagem de aprox. 10.000 emails.

Primeiramente, é preciso ter o Swift instalado. Eu utilizei o plugin sfSwiftPlugin

symfony plugin-install http://plugins.symfony-project.com/sfSwiftPlugin

Acesse a página do plugin para fazer os ajustes necessários

Com base num schema.yml:


propel:
  newsletter:
    _attributes:  { phpName: Newsletter }
    id:
    nome:         varchar(200)
    email:        varchar(200)
    ativo:        boolean
    created_at:
 

Geramos os modelos:

$ symfony propel-build-all

Vamos criar um batch que irá enviar nossa newsletter. Esse comando gera o arquivo `batch/newsletter.php`

$ symfony init-batch default newsletter frontend

Vamos editar o `batch/newsletter.php`


<?php

define(‘SF_ROOT_DIR’,    realpath(dirname(file).‘/..’));
define(‘SF_APP’,         ‘frontend’);
define(‘SF_ENVIRONMENT’, ‘prod’); // eu uso em prod.. por default vem dev
define(‘SF_DEBUG’,       1);

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.‘apps’.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.‘config’.DIRECTORY_SEPARATOR.‘config.php’);

// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();

// batch process here

$body = "Conteúdo do email…";

$smtp = new Swift_Connection_SMTP("mail.servidor.com", 25);
$smtp->setUsername("email@servidor.com");
$smtp->setpassword("senha");
$smtp->setTimeout(60);
$swift = new Swift($smtp);

$message = new Swift_Message("Título da Newsletter", $body , "text/html" , "utf-8");        
$message->setReturnPath("bounces@servidor.com"); // o return-path é bom para pegar os bounces

// criamos a lista de emails que receberão a newsletter
$recipients = new Swift_RecipientList();

// selecionando todos os assinantes ativos
$c = new Criteria();
$c->add(NewsletterPeer::ATIVO , true);
$assinantes = NewsletterPeer::doSelect($c);

// preenchendo a lista de recipientes
foreach($assinantes as $assinante) {
    $recipients->addTo($assinante->getEmail());
}

// esse plugin do Swift é muito util
$swift->attachPlugin(new Swift_Plugin_AntiFlood(200), "anti-flood");

$batch = new Swift_BatchMailer($swift);
$batch->setMaxTries(2); // nmro max de tentativas
$batch->setMaxSuccessiveFailures(2); // nmro max de falhas

$batch->send($message, $recipients, new Swift_Address("email_from@servidor.com" , "Título Newsletter"));  

$swift->disconnect();

Para fazer o envio execute o arquivo dentro da pasta `batch/`

$ php newsletter.php

Se você for fazer um disparo que leve muito tempo, é aconselhável colocar o script para ser executado em background com batch ou at

$ at -f php newsletter.php now

Em um post futuro eu mostro como fazer tudo isso sem utilizar linha de comando, ou seja executar uma action via web para executar o script e/ou colocar em background at.

php
swift
symfony

Comments (0)

Permalink