Page 1 of 1

Two instances of swift using Swift_Connection_Multi sharing

Posted: Mon Mar 10, 2008 12:30 pm
by Flurrywinde
I just want to check if this is okay:

Code: Select all

$connections_gmail = array();
$connections_def = array();
 
$conn_g =& new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS);
$conn_g->setUsername("me@gmail.com");
$conn_g->setPassword("mypass");
$connections_gmail[] =& $conn_g;
 
$conn_s =& new Swift_Connection_Sendmail();
$connections_gmail[] =& $conn_s;
$connections_def[] =& $conn_s;
 
$conn_n =& new Swift_Connection_NativeMail();
$connections_gmail[] =& $conn_n;
$connections_def[] =& $conn_n;
 
$mailer_gmail =& new Swift(new Swift_Connection_Multi($connections_gmail));
$mailer_def =& new Swift(new Swift_Connection_Multi($connections_def));
Basically this is just to use the Gmail SMTP server in some cases (Ahem, Yahoo due to the annoying deferred e-mail spam protection they do). Can I do it this way or should I make a second instance of Swift_Connection_Sendmail() and Swift_Connection_NativeMail()?

Re: Two instances of swift using Swift_Connection_Multi sharing

Posted: Mon Mar 10, 2008 9:19 pm
by Chris Corbyn
I haven't tested what would happen if you run both versions in the same request, but logically it should work. Thanks for raising this scenario since I should probably add test coverage for it.

Like I say, it *should* work, as to whether it will in reality is another matter since I haven't tested it.

Re: Two instances of swift using Swift_Connection_Multi sharing

Posted: Fri Mar 28, 2008 2:16 pm
by Flurrywinde
Thanks, please let me know if there's a problem. I'll do my own testing, of course, but I'm not really set up for that (i.e. only have access to a limited amount of e-mail addresses). So far, it seems to work if only one instance is used.

ANOTHER RELATED QUESTION: Is there a way to determine which connection is being used? The reason I need to know is that Gmail STMP fails after 100 e-mails, so I plan to make multiple accounts, cycling through them. (I believe this doesn't violate their terms as long as I'm not spamming or using the space for storage.) Thus, if my script exhausts all my Gmail SMTP connections, I want to be able to make the script notify me of this so I can go make a new one.

Re: Two instances of swift using Swift_Connection_Multi sharing

Posted: Sat Mar 29, 2008 12:45 pm
by Chris Corbyn
Gmail is painfully slow. It doesn't really feel like you're using the right tool for the job here.
ANOTHER RELATED QUESTION: Is there a way to determine which connection is being used?

Code: Select all

 
if ($swift->connection === $gmailConnection) {
  //is gmail
}
Or if you just want to look at the host name:

Code: Select all

 
if ($swift->connection->getServer() == 'smtp.gmail.com') {
  //is gmail
}

Re: Two instances of swift using Swift_Connection_Multi sharing

Posted: Sat Mar 29, 2008 2:51 pm
by Flurrywinde
Thanks, just what I needed!

As for Gmail being slow, do you mean slow sending (i.e. my script will be slow) or slow delivering? If slow delivering, well, it doesn't seem to be as bad as getting deferred up to 72 hours and sometimes not getting delivered at all, which is what happens when I use my host's smtp server. If you mean slow sending, well, we only send a few hundred e-mails once per day, so even if it takes all night, that would be fine, especially since only a small percentage of those are to Yahoo addresses. How slow is it anyway?

But you're right, I don't particularly like this Gmail SMTP solution, but it's the only one I can think of besides switching to a dedicated server or paying for our own smtp. Unfortunately, we're just a small non-profit site, so the cheap shared hosting is all we can afford. Let me know if you have any other ideas to get around this terrible Yahoo deferment problem!

Re: Two instances of swift using Swift_Connection_Multi sharing

Posted: Sat Mar 29, 2008 10:07 pm
by Chris Corbyn
Gmail is slow just generally. It's not a bad thing... I use it all the time for my personal email. That's what it's for. They aren't set up to allow *anybody* to just wham mail through their servers. The more people who keep doing it (and there are a lot) the slower their service will become and the tighter their restrictions will be.

But you're right, my only other solution involves spending money... email does cost money at the end of the day. It's a high bandwidth service which is subject to a lot of abuse. 100 emails per day is fine no doubt and fits within gmail's limits (unconfirmed). I got the impression you were sending more than that based on your reasoning of creating multiple gmail accounts to avoid their flood detection.

Does your ISP not provide an SMTP server?

Re: Two instances of swift using Swift_Connection_Multi sharing

Posted: Fri May 09, 2008 8:53 pm
by Flurrywinde
Chris Corbyn wrote:Does your ISP not provide an SMTP server?
They do, but it has a bad reputation with senderscore.org.

Anyway, I tried to use your code:

Code: Select all

if ($swift->connection === $gmailConnection) {
  //is gmail
}
 


and

Code: Select all

if ($swift->connection->getServer() == 'smtp.gmail.com') {
  //is gmail
}
But neither seem to work. Here's my code:

Code: Select all

// Initialize SwiftMailer
$connections_gmail = array();
$connections_def = array();
 
$conn_g =& new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS);
$conn_g->setUsername("email1@gmail.com");
$conn_g->setPassword("mypass");
$connections_gmail[] =& $conn_g;
 
$conn_g2 =& new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS);
$conn_g2->setUsername("email2@gmail.com");
$conn_g2->setPassword("mypass");
$connections_gmail[] =& $conn_g2;
 
// Add another Gmail connection here if needed. Will also have to edit function gmail_conn() if you do so. 
 
$conn_s =& new Swift_Connection_Sendmail();
$connections_gmail[] =& $conn_s;
$connections_def[] =& $conn_s;
 
$conn_n =& new Swift_Connection_NativeMail();
$connections_gmail[] =& $conn_n;
$connections_def[] =& $conn_n;
 
$mailer_gmail =& new Swift(new Swift_Connection_Multi($connections_gmail));
$mailer_def =& new Swift(new Swift_Connection_Multi($connections_def));
 
//1 mails per batch with a 10 second pause between batches (Author suggests 100/batch, 30 sec.
$mailer_gmail->attachPlugin(new Swift_Plugin_AntiFlood(1, 10), "anti-flood");
$mailer_def->attachPlugin(new Swift_Plugin_AntiFlood(1, 10), "anti-flood");
 
function gmail_conn() {
    global $mailer_gmail, $conn_s, $conn_n, $conn_g, $conn_g2; // When adding a new Gmail connection, don't forget to add it here too.
 
    if ($mailer_gmail->connection === $conn_g) {
        return "Gmail #1 (".$mailer_gmail->connection->getUsername().") ({$mailer_gmail->connection})";
    } else if($mailer_gmail->connection === $conn_g2) {
        return "Gmail #2 ($mailer_gmail->connection)";
    // Add new Gmail connections here
    } else {
        if($mailer_gmail->connection === $conn_s) return "Sendmail";
        if($mailer_gmail->connection === $conn_n) return "Native";
        echo $mailer_gmail->connection->getServer();
        echo $mailer_gmail->connection->getUsername();
    }
}
This code reports that $mailer_gmail->connection is none of the connections (though using $mailer_gmail to send stuff works and does use Gmail's SMTP server). $mailer_gmail->connection->getServer() and $mailer_gmail->connection->getUsername() do not work because $mailer_gmail->connection doesn't have the getServer() or getUsername() properties.

AH! While writing this, I tried different code, and it worked! I think because the connection is a Multi, you have to do:

$mailer_gmail->connection->connections[$mailer_gmail->connection->active] instead of just $mailer_gmail->connection.

Thus, to get the Server and Username, it should be:

$mailer_gmail->connection->connections[$mailer_gmail->connection->active]->getServer()
$mailer_gmail->connection->connections[$mailer_gmail->connection->active]->getUsername()

Or you could just use $mailer_gmail->connection->active, and if it's zero, it means the active connection is the first one, etc.