开发者

Can't send emails with pyramid_mailer and gmail

I 开发者_开发问答am trying to send emails from my pyramid website with my gmail smtp and the pyramid_mailer package. First of all, if anyone has another suggestion for an email solution, please let me know!

I added the following to my dev.ini:

mail.host = smtp.gmail.com
mail.username = user@gmail.com
mail.password = password
mail.port = 465
mail.ssl = True

And then I'm sending the message like so:

config.registry['mailer'] = Mailer.from_settings(settings)

and later...

mailer = request.registry['mailer']
message = Message(subject="hello world",
                      sender="admin@mysite.com",
                      recipients=["someaddress@gmail.com"],
                      body="hello!")
mailer.send(message)

Unfortunately, I get the following exception:

SMTPServerDisconnected: please run connect() first

What am I doing wrong?

Thanks!


The following settings worked for me:

# pyramid_mailer
mail.host = smtp.gmail.com
mail.port = 587
mail.username = my.login@gmail.com
mail.password = mypassword
mail.tls = True

Your mail sending code seems to be the same as mine, so that should work.

I haven't tried SSL, but I'm assuming that all kinds of bugaboos may exist thataway.


The email is not actually sent until the transaction is committed.

You should commit the transaction:

import transaction
transaction.commit()

or use send_immediately:

mailer.send_immediately(message, fail_silently=False)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜