Skip to content

Instantly share code, notes, and snippets.

@okochang
Created February 24, 2012 08:10
Show Gist options
  • Save okochang/1899199 to your computer and use it in GitHub Desktop.
Save okochang/1899199 to your computer and use it in GitHub Desktop.
awssdk for ruby send email with attaching file
# -*- coding: utf-8 -*-
require 'aws-sdk'
# ACCESS_KEYとSECRET_KEYを定義します
ACCESS_KEY = 'YOUR_ACCESS_KEY'
SECRET_KEY = 'YOUR_SECRET_KEY'
# 送信メールの本文を作成します
body = 'Sample email attaching file.'
# ファイルを読み込みbase64フォーマットにエンコードします
file_path = 'path_to_attach_file.png'
filecontent = File.read(file_path)
encodedcontent = [filecontent].pack("m")
# 送信用のメッッセージを作成します
marker = "AWSSESTESTSENDING"
mailtext =<<EOF
From: sender@domain.com
To: receipient@domain.com
Subject: A Sample Email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{body}
--#{marker}
Content-Type: image/png; name=\"#{file_path}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{file_path}"
#{encodedcontent}
--#{marker}--
EOF
# AWSの認証を行い、テストメールを送信します
ses = AWS::SimpleEmailService.new(:access_key_id => ACCESS_KEY,:secret_access_key => SECRET_KEY)
ses.send_raw_email(mailtext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment