On ec2 instance, the IP is dynamic, and if I want to use third party SMTP gateway such as sendgrid, what is the suggested values of the following fields?


Assume my instance hostname (FQDN) is


And I don't have any more hostname mapped to this machine as it is purely dynamic in nature, however, I can receive email with address at info@example.com

Update: I cannot use Elastic IP since each region has only 5, and I want a solution that work without the use of fixed IP.

  • 5
    You can request more than 5 EIPs. – EEAA Jul 4 '14 at 3:34

On ec2 instance, the IP is dynamic,

This is precisely what EC2 Elastic IPs are for. Nab an EIP, associate it with your server, and then you can point a proper DNS record to that EIP.

  • EIP is limited, not a solution when you need to launch large cluster of machines to send out email, especially when these machines does not need public access – Ryan Jul 4 '14 at 3:00
  • 5
    @Ryan If you have requirements that affect potential answers, you ought to mention those in your answer, perhaps. – EEAA Jul 4 '14 at 3:01

I have a similar set up relaying via mailgun.

Firstly, you can get the public IP of an instance using:

From within the instance, in my case I use orchestration (ie. Chef + OpsWorks) to automatically create a DNS record in Route53 for the instance on boot.

You could probably also use:

To get the instance host name, and then insert it into your postfix config.

I use servername.servers.mydomain.com (which is created automatically on boot) as the hostname and mydomain and mydestination is empty.

I also had to create a transport file in /etc/postfix/transport with the following content:

*                smtp:smtp.mailgun.org

First, most third party SMTP gateway providers support using their service as a authenticated, encrypted relay host:

The key is, you shouldn't have to do access control using IP addresses or hostname. You use encryption (TLS) and authentication, so the SMTP provider should "trust" all mail coming from your local instance of postfix once it's setup to do auth. Most have ways of using sendmail as well.

As for the configuration values:

  • myorigin should definitely be example.com - what you want users to see your mail as coming from.
  • In my opinion on myhostname would be set to either:
    1. leave myhostname as the default, since it should not matter to your SMTP provider
    2. just hard-code myhostname to an arbitrary name e.g. aws-web.example.com for the same reason - it should not matter to your SMTP provider
  • mydestination should be left to be the default - you're not delivering any mail locally.

If you want to get really fancy you can also use the sender_dependent_relayhost_maps option.

Finally, you haven't described what you're using to send email from your EC2 instances. Depending on the language and library (I'm looking at you php mail) your mail submission agent (MSA) might do hairy things to mangle your message, possibly ignoring the values set above. php mail is infamous because on most systems it tries to invoke the sendmail binary, which you have to work around explicitly to prevent the message from being sent as the user running the webserver (e.g. www-data or apache)


sendgrid uses password based authentication (config details for that below). It won't care about your IP, or the hostnames you use (myhostname, myorigin, etc).

The hostname your server presents to sendgrid in the HELO (or EHLO) greeting is likely to appear in mail headers. Some recipients' spam software may check it, so use something that does exist in public DNS, and not associated with a dodgy domain, but few systems will notice this stuff. Good to do it well, but not critical.

I have a configuration where I send mail through sendgrid for multiple domains, so postfix needs to know which sendgrid account to use for each sender domain. (and if it's not a known sender domain it sends via a local relay, without using sendgrid). This is not really what you asked for, but my guess is that the problem you think is about domain names and your shifting IP is actually about authentication with sendgrid.


relayhost = smtp.example.com
smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous


@domain1.example.com       [smtp.sendgrid.net]:submission
@domain2.example.com       [smtp.sendgrid.net]:submission


@domain1.example.com       sendgrid-user-1:Pa$$w0rd1
@domain2.example.com       sendgrid-user-2:Pa$$w0rd2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.