Created
February 24, 2012 08:10
-
-
Save okochang/1899199 to your computer and use it in GitHub Desktop.
awssdk for ruby send email with attaching file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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: [email protected] | |
To: [email protected] | |
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