this code can be used for the Mandrill API for sending mails from a website. But the problem is I can't get the HTML input into the JSON to send to the correct email address. Look at the code beneath:
<head>
<script type="text/javascript" src="https://mandrillapp.com/api/docs/js/mandrill.js"></script>
<script type="text/javascript" src="send-email.js"></script>
</head>
<body>
<input type="text" value="e-mail" id="maila"/>
<button onclick="sendTheMail(); return false;">Send the Mail</button>
<pre id="response"></pre>
</body>
So this is the HTML and in the input field you type the mail address to whom you'd like to send.
Then I have the following .js file:
function log(obj) {
$('#response').text(JSON.stringify(obj));
}
var m = new mandrill.Mandrill('API-KEY');
var mailaddress = document.getElementById("maila").value;
var params = {
"message": {
"from_email":"from@email.com",
"to":[{"email":"to@email.com"}],
"subject": "Sending you a mail",
"html": "Text for the body of the mail",
}
};
function sendTheMail() {
m.messages.send(params, function(res) {
log(res);
}, function(err) {
log(err);
});
}
So here I get the var mailaddress which is the mail address to which I would like to send a message and which the user has given in the browser. But now I want the value "to@email.com" to take the variable of var mailaddress. But just entering this variable here doesn't work as I think it's not JSON...
I know the program works, because if I fill in a mail address in the "to":[{"email":"to@email.com"}], manually it will send and arrive. Tried to search the forum and google but could not find a solution for this..
UPDATE I got it, thanks for the help. The getElementById has to be within a function I think, so I moved the code a little bit... It looks like beneath. The code is working now, but I am curious: is this nice coding or do I have to orden things?
function log(obj) {
$('#response').text(JSON.stringify(obj));
}
var m = new mandrill.Mandrill('API-KEY');
function sendTheMail() {
// Send the email!
var mailaddress = document.getElementById("maila").value,
recipient = [{"email": String(mailaddress)}];
var params = {
"message": {
"from_email":"email@gmail.com",
"to": recipient,
"subject": "Sending a text email from the Mandrill API",
"text": "I'm learning the Mandrill API at Codecademy.",
"autotext": true
}
};
m.messages.send(params, function(res) {
log(res);
}, function(err) {
log(err);
});
}
var
mailaddress = document.getElementById("maila").value,
recipients = [],
recipient = { "email": mailaddress };
recipients.push(recipient);
var params = {
"message": {
"from_email": "from@email.com",
"to": recipients,
"subject": "Sending you a mail",
"html": "Text for the body of the mail",
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.