5

Can someone teach me how to configure a centos instance to send mails.

My intention is to send mails from a bash script based on some signals and alerts to a build user. I don’t intend to use it as a mass mailing machine.

4 Answers 4

12

This is a perfect fit for Amazon's Simple Notification Service. The service (also known as SNS) is simple to use from a bash script, free (for the first 100,000 messages, less than 1gb in total), and the notifications are immediate.

The setup for SNS looks like this: enter image description here
From:SNS Documentation

It looks like a lot of trouble to send eMail, however, Amazon's EC2 environment is highly restrictive when it comes to sending eMail. Many have tried to use EC2 as a spam platform, so Amazon has been thorough in blocking SMTP (eMail) traffic, except as prescribed by Amazon. You can't just open up a port on the Amazon security group to bypass Amazon's blocks.

Amazon does have a general eMail facility one can use from within EC2. It is called, Amazon Simple Email Service (SES). That will not work well for you, as it is designed for bulk eMail. So, SES's pricing, exception handling and messaging won't fit well with your application as I imagine it.

SNS, on the other hand works great for this. It includes an initial eMail to the recipients (your build users) asking if they want to receive future messages of the topic; they are given an option to opt out, and must reply to receive further.

The setup process (shown in blocks above) is all easily doable from Amazon's AWS Management Console. (Your question implies you that you already have an AWS EC2 account needed for this.) Once setup, your command from bash would be similar to:

$PROMPT> sns-publish arn:aws:sns:us-east-1:365928882516:Foo --message "Hello, world!"

The example above is done using the Command Line Interface available for SNS. This Command Line Interface is available at: (Amazon Simple Notification Service Command Line Interface Tool), and it's documentation is here: (Amazon Simple Notification Service Command Line Interface Documentation). Set it up first though, as described above.

It'll be gotcha-free in setting up, and solid as a rock in operation.

2
  • Very helpful. Was a bit tricky getting SNS set up initially. I had to include the region explicitly otherwise it couldn't find the resource despite the region being a part of the resource identifier.
    – g .
    Nov 4, 2011 at 12:33
  • I like the idea of using SNS to do this! I'm using an EC2 instance provisioned through Lightsail and the sns-publish errors for me, it's reporting "An error occurred (AuthorizationError) when calling the Publish operation: User: arn:aws:sts::XXXXXXXX:assumed-role/AmazonLightsailInstanceRole/i-YYYYY is not authorized to perform:: SNS:Publish on resource... arn:aws:sns:eu-west-1:ZZZZZZZZ:my-sns-topic-name". I've tried to modify the topic policy to enable "Allow these users to publish messages to this topic" to "everyone", any pointers welcome. Oct 16, 2018 at 22:09
4

Note that in the latest version of the CLI, which now combines AWS services into one executable, the syntax has changed slightly. The above command would now be:

$PROMPT> aws sns publish --topic-arn arn:aws:sns:us-east-1:365928882516:Foo --message "Hello, world!"

Also note that if you haven't set the AWS_DEFAULT_REGION environment variable (this is not set by default on AMAZON Linux AMIs, for example), you'll need to specify the region:

$PROMPT> aws --region us-east-1 sns publish --topic-arn arn:aws:sns:us-east-1:365928882516:Foo --message "Hello, world!"

Source: Amazon's unified CLI documentation.

1

If you use your own mail server (on Centos), remember to remove the EC2 email sending limit!

http://aws.amazon.com/ec2/faqs/#Are_there_any_limitations_in_sending_email_from_EC2_instances

1

I know it's old topic, but today you can use SES as sendmail

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/sendmail.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .