メール送信予約(一斉メール配信)
サンプルプログラム Python
# -*- coding: utf-8 -*-
import urllib2
import StringIO
import os
import sys
##############################
# 接続先URLを用意
##############################
# ドメイン(●の部分) https://●●●●●/■■■■■/api/index.php?ac=CreateNewMail
domain = '●●●●●'
# ログインURL(■の部分) https://●●●●●/■■■■■/api/index.php?ac=CreateNewMail
login_url = '■■■■■'
##############################
# POSTパラメータを用意
##############################
# 接続用パスワード
transport_passwd = '◆◆◆◆◆'
# 文字コード
charset = '◆'
# 取得形式
return_format = '◆◆◆'
# CSVファイル
csvfile = '◆◆◆◆◆'
# リスト名
list_name = '◆◆◆◆◆'
# Fromのメールアドレス
from_address = '◆◆◆◆◆'
# Fromの差出人名
from_name = '◆◆◆◆◆'
# 件名
subject = '◆◆◆◆◆'
# 本文(テキストパート)
text_part = '◆◆◆◆◆'
# 本文(HTMLパート)
html_part = '◆◆◆◆◆'
# 予約種別
schedule_type = '◆'
# レポートオプション
report_option = '◆'
##############################
# HTTPリクエスト準備
##############################
response = {}
url = 'https://' + domain + '/' + login_url + '/api/index.php?ac=CreateNewMail'
boundary = '---attached'
s = StringIO.StringIO()
##############################
# 送信データを作成
##############################
try:
if transport_passwd:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="transport_password"\r\n\r\n%s\r\n' % transport_passwd)
except NameError:
pass
try:
if charset:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="charset"\r\n\r\n%s\r\n' % charset)
except NameError:
pass
try:
if return_format:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="return_format"\r\n\r\n%s\r\n' % return_format)
except NameError:
pass
try:
if csvfile:
if os.path.exists(csvfile):
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="csvfile"; filename="%s"\r\n' % csvfile)
s.write('Content-Type: application/octet-stream\r\n\r\n')
f = open(csvfile, 'rb').read()
s.write(f)
s.write('\r\n')
else:
pass
except NameError:
pass
try:
if list_name:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="list_name"\r\n\r\n%s\r\n' % list_name)
except NameError:
pass
try:
if from_address:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="from_address"\r\n\r\n%s\r\n' % from_address)
except NameError:
pass
try:
if from_name:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="from_name"\r\n\r\n%s\r\n' % from_name)
except NameError:
pass
try:
if subject:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="subject"\r\n\r\n%s\r\n' % subject)
except NameError:
pass
try:
if text_part:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="text_part"\r\n\r\n%s\r\n' % text_part)
except NameError:
pass
try:
if html_part:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="html_part"\r\n\r\n%s\r\n' % html_part)
except NameError:
pass
try:
if schedule_type:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="schedule_type"\r\n\r\n%s\r\n' % schedule_type)
except NameError:
pass
try:
if schedule_date:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="schedule_date"\r\n\r\n%s\r\n' % schedule_date)
except NameError:
pass
try:
if report_option:
s.write('--%s\r\n' % boundary)
s.write('Content-Disposition: form-data; name="report_option"\r\n\r\n%s\r\n' % report_option)
except NameError:
pass
s.write('--%s--\r\n' % boundary)
##############################
# POST送信+レスポンス取得
##############################
try:
req = urllib2.Request(url, s.getvalue())
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_header('Connection', 'close')
res = urllib2.urlopen(req)
response["body"] = res.read()
response["headers"] = res.info()
except urllib2.URLError, e:
print e
sys.exit(1)
if charset == '1':
charsetStr = 'UTF-8'
elif charset == '2':
charsetStr = 'shift-JIS'
elif charset == '3':
charsetStr = 'EUC-JP'
else:
charsetStr = 'UTF-8'
print response["headers"]
print response["body"].decode(charsetStr).encode('UTF-8')