Sending emails on behalf of someone using nodemailer - javascript

I'm trying to send an email on behalf of my user using nodemailer but all my emails are being rejected.
My transporter works for normal use-cases where I'm sending from my own domain:
const transporter = nodemailer.createTransport({
host: 'emailhost.com',
port: 465,
secure: true,
auth: {...},
dkim: {
domainName: <domainname>,
keySelector: <keyselector>,
privateKey: <privatekey>,
},
});
transporter.sendMail({
subject: "Shop subject",
from: `kyle#mydomain.com`,
to: <customerEmail>,
replyTo: <shopEmail>,
html: "<div>my user message</div>",
})
But when I try to send on behalf of my user it doesn't work:
transporter.sendMail({
subject: "Shop subject",
from: `shop#exampleshop.com`,
to: <customerEmail>,
// sender: `kyle#mydomain.com` <-- Also tried adding this field,
replyTo: `shop#exampleshop.com`,
html: "<div>my user message</div>",
})
>> "Can't send mail - all recipients were rejected: 553 <shop#exampleshop.com>: Sender address rejected: not owned by user kyle#mydomain.com"
I figured if I set a DKIM DNS record and an SPF record on exampleshop.com it would mean I could send emails on behalf of the domain but still no luck.
Type: TXT Record
Host: #
Value: v=spf1 include:spf.emailhost.com ~all
// Also tried adding this record in place of the one above
Type: TXT Record
Host: #
Value: v=spf1 include:spf.mydomain.com ~all
Type: TXT Record
Host: default._domainkey
Value: v=DKIM1;k=rsa;p=<DKIM DNS record from namecheap>
It's also been over an hour since I've updated the DNS records, I'm not sure what I'm doing wrong here.

try using the envelope for this. as i understand it, "envelope" values are like whats written on the outside of a physical piece of mail. it tells the post office where to actually send (or return) the message. the "header from", on the other hand, is like when you re-write your address at the top of the piece of paper that comes inside the envelope. it doesn't do anything but it looks nice.
header_from = "it-just-looks-like-its-from-me#asdf.com"
envelope_from = "im-actually-doing-the-sending#asdf.com"
to = 'pick-me#asdf.com'
var mailOptions = {
from: header_from,
to: to,
subject: 'Sending Email using Node.js',
text: 'That was easy!',
envelope: {
from: envelope_from,
to: to,
}
};
https://nodemailer.com/smtp/envelope/

Related

Can’t Attach PDF File to Email in JavaScript Using smtpjs

Summarize the problem
I am unable to attach a PDF file to an email to be sent to an email address. The PDF file is in the same folder as the HTML file, which contains the JavaScript code to send the email using SMTPJS.
Expected result:
An email is sent to the designated email address with the pdf file without any problems.
Actual result:
No email was sent at all.
Describe what you’ve tried
#1: Attempt by putting file name and extension in quotes like this:
PFD_Articles.pdf -> 'PFD_Articles.pdf'
Again, the actual result is produced, not the expected one.
Show some code
Email.send
(
{
Host: "smtp.gmail.com",
Username: "sender#gmail.com",
Password: "password",
To: 'receiver#gmail.com',
From: "sender#gmail.com",
Subject: "<h1> ddd </h1>",
Body: "And this is the body",
Attachments:
[
{
name: 'PFD_Articles.pdf'
}
]
}).then
(
alert("Successful! Please check your email inbox. Otherwise, you may have to enter your email address again.")
);
Email.send({
Host: "smtp.gmail.com",
Username : "abc#gmail.com",
Password : "abc",
To : receiver,
From : "abc#gmail.com",
Subject : emailSubject,
Body : emailBody,
Attachments : [
{
name : list.pdf
data : pdfBase64
}]
}).then(
)
});
This worked ! The created pdf got added to the email as an attachment

How to send images and hyper links via smtpjs?

I have the following code:
const input = document.querySelector('input')
btn.addEventListener('click',() => {
Email.send({
Host: 'smtp.gmail.com',
Username:'',
Password:'',
To:input.value,
Subject:"",
From: '',
Body:''
}).then(msg=>console.log(msg))
And in the body I want to send a photo, on the next row text and then a hyperlink(or even the button) Is it possible or smtpjs does not provide that functuonality? Couldn't find anything
Or maybe there is another source(not smtpjs), where I could write a template of the letter and then send it
To send the files, you can define the attachment.
Email.send({
SecureToken : "<your security token>",
To : '<whom you want to send>',
From : "<Your email id registered on SMTPJS>",
Subject: "<Subject>",
Body: "<Body Content>",
Attachments:
[{
name: "File_Name_with_Extension",
path: "Full Path of the file"
}]
})
.then(function (message) {
//
}
Next, if you want to send the hyperlink, I think you can use the html content on body instead of text.
...
Body: "Click <a href='[full_path of link]'>here</a> <button>Button</button>",
...
But the onClick event or other javascript actions will not work for button.
Thank you

React Native - Open mail client with body

I'm using npm pacakge to open mail client with data:
https://www.npmjs.com/package/react-native-mail-compose
Also, I'm using their example:
import MailCompose from 'react-native-mail-compose';
// later in your code...
async sendMail() {
try {
await MailCompose.send({
toRecipients: ['to1#example.com', 'to2#example.com'],
ccRecipients: ['cc1#example.com'],
bccRecipients: ['bcc1#example.com', 'bcc2#example.com'],
subject: 'This is subject',
text: 'This is body',
html: '<p>This is <b>html</b> body</p>', // Or, use this if you want html body. Note that some Android mail clients / devices don't support this properly.
attachments: [{
filename: 'mytext', // [Optional] If not provided, UUID will be generated.
ext: '.txt',
mimeType: 'text/plain',
text: 'Hello my friend', // Use this if the data is in UTF8 text.
data: '...BASE64_ENCODED_STRING...', // Or, use this if the data is not in plain text.
}],
});
} catch (e) {
// e.code may be 'cannotSendMail' || 'cancelled' || 'saved' || 'failed'
}
}
and call this function on button press. All data is OK, except body, for example here in Subject there is "This is subject", in CC of mail clients, there is "cc1#example.com", but body is always empty, I can't ever see "This is body" in mail client.
I've fixed it with another package react-native-send-intent.
It works like a charm! :)
I see, give this package a try.
https://github.com/anarchicknight/react-native-communications
Its very simple to use

Hyperlink not shown in gmail with email composer

I am trying to send html as a body with email composer. html body contains hyperlink but it is not working in gmail. Gmail display plain text only.
var email = {
to: sendersemail+';',
cc: '',
bcc: '',
attachments: [cordova.file.externalApplicationStorageDirectory+'Report.pdf'],
subject: '',
body: '<p>the brain out your I think you'll find it helpful too.</p>',
isHtml: true
};
$cordovaEmailComposer.open(email).then(null, function () {
// user cancelled email
});
use html instead of body!
var email = {
to: sendersemail+';',
cc: '',
bcc: '',
attachments: [cordova.file.externalApplicationStorageDirectory+'Report.pdf'],
subject: '',
html: '<p>the brain out your I think you'll find it helpful too.</p>'
};
$cordovaEmailComposer.open(email).then(null, function () {
// user cancelled email
});

Creating an envelope from a template returning "UNSPECIFIED_ERROR"

When I try to create an envelope from a template I get a response of:
{ errorCode: 'UNSPECIFIED_ERROR',
message: 'Non-static method requires a target.' }
Here's what I'm doing so far:
First I login, which returns
{ loginAccounts:
[ { name: '*****',
accountId: '*****',
baseUrl: 'https://demo.docusign.net/restapi/v2/accounts/******',
isDefault: 'true',
userName: '***** ********',
userId: '*******-*****-*****-*****-*********',
email: '********#*******.com',
siteDescription: '' } ] }
So then I take the baseUrl out of that response and I attempt to create the envelope. I'm using the hapi framework and async.waterfall of the async library, so for anyone unfamiliar with either of these my use of the async library uses the next callback to call the next function which in this case would be to get the url for the iframe, and with our usage of the hapi framework AppServer.Wreck is roughy equivalent to request:
function prepareEnvelope(baseUrl, next) {
var createEntitlementTemplateId = "99C44F50-2C97-4074-896B-2454969CAEF7";
var getEnvelopeUrl = baseUrl + "/envelopes";
var options = {
headers: {
"X-DocuSign-Authentication": JSON.stringify(authHeader),
"Content-Type": "application/json",
"Accept": "application/json",
"Content-Disposition": "form-data"
},
body : JSON.stringify({
status: "sent",
emailSubject: "Test email subject",
emailBlurb: "My email blurb",
templateId: createEntitlementTemplateId,
templateRoles: [
{
email: "anemailaddress#gmail.com",
name: "Recipient Name",
roleName: "Signer1",
clientUserId: "1099", // TODO: replace with the user's id
tabs : {
textTabs : [
{
tabLabel : "acct_nmbr",
value : "123456"
},
{
tabLabel : "hm_phn_nmbr",
value : "8005882300"
},
{
tabLabel : "nm",
value : "Mr Foo Bar"
}
]
}
}
]
})
};
console.log("--------> options: ", options); // REMOVE THIS ====
AppServer.Wreck.post(getEnvelopeUrl, options, function(err, res, body) {
console.log("Request Envelope Result: \r\n", JSON.parse(body));
next(null, body, baseUrl);
});
}
And what I get back is:
{ errorCode: 'UNSPECIFIED_ERROR',
message: 'Non-static method requires a target.' }
From a little googling it look like 'Non-static method requires a target.' is a C# error and doesn't really give me much indication of what part of my configuration object is wrong.
I've tried a simpler version of this call stripping out all of the tabs and clientUserId and I get the same response.
I created my template on the Docusign website and I haven't ruled out that something is set up incorrectly there. I created a template, confirmed that Docusign noticed the named form fields, and created a 'placeholder' templateRole.
Here's the templateRole placeholder:
Here's one of the named fields that I want to populate and corresponding data label:
As a side note, I was able to get the basic vanilla example working without named fields nor using a template using the docusign node package just fine but I didn't see any way to use tabs with named form fields with the library and decided that I'd rather have more fine-grained control over what I'm doing anyway and so I opted for just hitting the APIs.
Surprisingly when I search SO for the errorCode and message I'm getting I could only find one post without a resolution :/
Of course any help will be greatly appreciated. Please don't hesitate to let me know if you need any additional information.
Once I received feedback from Docusign that my api call had an empty body it didn't take but a couple minutes for me to realize that the issue was my options object containing a body property rather than a payload property, as is done in the hapi framework.

Categories

Resources