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

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

Related

Sending emails on behalf of someone using nodemailer

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/

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
});

Add a template for emails with emailjs

I have the following lines, This is what I tried, and it works fine but I want to load a template for drawing the content inside, how can I attach a file for being displayed as a content of the email? Here comes what I have:
attachment:[{data: req.__({ phrase: 'Please click this link to reset your password: https://website.com/reset/%s', locale: locale}, user.uuid), alternative:true}]
How can I load a path to read the content from something like an HTML file?
I can use a template adding this lines:
var forgotTemplate = fs.readFileSync('../public/mailplates/forgot');
and then:
attachment: [{data: forgotTemplate, alternative:true}]
Anyway, I need to fit the "hash" for change the password, how can i add a script line inside or at least a string?
var email = require("./path/to/emailjs/email");
var server = email.server.connect({
user: "username",
password:"password",
host: "smtp.your-email.com",
ssl: true
});
var message = {
text: "... ",
from: "...",
to: "...",
subject: "testing emailjs",
attachment:
[
{data:"<html>i <i>hope</i> this helps</html>", alternative:true},
{path:"path/to/file.zip", type:"application/zip", name:"renamed.zip"}
]
};
// send the message and get a callback with an error or details of the message that was sent
server.send(message, function(err, message) { console.log(err || message); });
Finally, a solution is with Jade, i add:
var jade = require('jade');
and then:
var forgotTemplate = jade.renderFile('../public/mailplates/forgot.jade', {userRecover: userInfoRecover});
The Jade template must display something like this:
doctype html
html
head
title = forgot password
body
h1 this works
a(href='#{ userRecover }') link

Categories

Resources