Search code examples
pythonipsmtplib

From what IP address does SMTP lib send an email from?


Does anybody know what is the IP address of the sender when using Python smtplib to send emails? The sender's email address will be Gmail. Does the email originate from the IP of the computer in which the Python script is executed? Or is it an IP from the Gmail servers?

I want to make an email marketing program such as Mailchimp for my personal use, but much smaller and with far fewer features than Mailchimp. One key aspect of marketing software is IP. The emails need to be sent from an IP with a good reputation. So, when you send emails with Mailchimp, they are sent from the Mailchimp (well reputable) IPs. I'd like to know what IP will Python send emails from.


Solution

  • Does the email originate from the IP of the computer in which the Python script is executed? Or is it an IP from the Gmail servers?

    Neither. As shown in the example in the docs, it will be the IP address of the server you're connecting to, to send the email. So you'll probably want to connect to the SMTP server of your domain host.

    If you run an SMTP server locally, it will be IP addr of that machine. And the connection is likely to be rejected by the recipient's smtp server.

    However note that in any case, the SMTP server may add the originating IP address to the email headers.

    To not have your emails outright rejected by the server, take a look at Sender Policy Framework (SPF). Doesn't ensure your email won't get marked as Spam though.