これには必死で悩んでいます。チェックボックス付きのテーブル (一部はチェックされていますが、すべてではありません) から電子メール アドレスの配列を収集し、それらを Mandrill JavaScript コードに渡して送信したいと考えています。
ここには 2 つの問題があります。
- 一般的な複数のメールの送信方法と
- mandrill コードに配列を渡してメール送信を実行する方法。
コードは次のとおりです。
<script>
function log(obj) {
$('#response').text(JSON.stringify(obj));
}
function sendTheMail() {
// Send the email!
alert('scripting');
var emails = [];
var first = [];
var last = [];
$('tr > td:first-child > input:checked').each(function() {
//collect email from checked checkboxes
emails.push($(this).val());
first.push($(this).parent().next().next().text());
last.push($(this).parent().next().next().next().text());
});
var new_emails = JSON.stringify(emails);
alert(new_emails);
//create a new instance of the Mandrill class with your API key
var m = new mandrill.Mandrill('my_key');
// create a variable for the API call parameters
var params = {
"message": {
"from_email": "rich@pachme.com",
"to": [{"email": new_emails}],
"subject": "Sending a text email from the Mandrill API",
"text": "I'm learning the Mandrill API at Codecademy."
}
};
m.messages.send(params, function(res) {
log(res);
},
function(err) {
log(err);
});
}
</script>
電子メール アドレスの配列のアラートは次のとおりです。
["richardwi@gmail.com","richard.illingworth@refined-group.com","mozartfm@gmail.com"]
結果のエラーメッセージは次のとおりです。
[{"email":"[\"richardwi@gmail.com\",\"richard.illingworth@refined-group.com\",\"mozartfm@gmail.com\"]","status":"invalid","_id":"0c063e8703d0408fb48c26c77bb08a87","reject_reason":null}]
より一般的な注意として、以下は 1 つの電子メール アドレスに対してのみ機能します。
"to": [{"email" : "user@gmail.com"}],
しかし
"to": [{"email" : "user@gmail.com", "anotheruser@gmail.com"}],
そうではないので、複数の送信をハードコーディングすることさえできません。
何か案は?すべての助けを感謝して受け取りました。