Reputation: 445
Trying to figure out how to get access to googles API so I can create a contact form on my website, I have found this example but am struggling to use / convert it for use in Angular.
https://github.com/googleapis/google-api-nodejs-client/blob/master/samples/gmail/send.js
I have just done npm install googleapis
but am unsure about how to continue. I know this is strictly speaking not a programming question, but I am wondering if anyone can help set up a mailing (contact form) for my portfolio. This is also my first time working with an API and I am doing a fair amount of research to figure out how to correctly do everything.
I am trying to go throught the documentation on the google API but as it is auto generated, the interface is a bit questionable... https://googleapis.dev/nodejs/googleapis/latest/index.html and I can't really find any useful information on it...
What i want to make is a way for people to contact me, so I send an e-mail to my work mail adress with their mail address attached so I can reply, a subject and a body. This is all the information I would need but as the API is very unclear I am having a hard time figuring out hhow to do this in Angular (Node.js)
TL;DR: How to connect Angular 8 (Node.js) to gmail api (or use smtp)
UPDATE: I changed to using nodemailer for the smtp but it isnt working still
import * as mailer from 'nodemailer';
export class Mailing {
constructor() {
this.send();
}
async send() {
const account = await mailer.createTestAccount();
const transporter = mailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: account.user,
pass: account.pass
}
});
const info = await transporter.sendMail({
from: '"Portfolio" <[email protected]>',
to: '[email protected]',
subject: 'Hello',
text: 'shut up',
});
}
}
ERROR in ./node_modules/nodemailer/lib/sendmail-transport/index.js
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\sendmail-transport'
ERROR in ./node_modules/nodemailer/lib/mailer/index.js
Module not found: Error: Can't resolve 'dns' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\mailer'
ERROR in ./node_modules/nodemailer/lib/shared/index.js
Module not found: Error: Can't resolve 'dns' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\shared'
ERROR in ./node_modules/nodemailer/lib/dkim/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\dkim'
ERROR in ./node_modules/nodemailer/lib/mime-node/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\mime-node'
ERROR in ./node_modules/nodemailer/lib/shared/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\shared'
ERROR in ./node_modules/nodemailer/lib/mailer/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\mailer'
ERROR in ./node_modules/nodemailer/lib/shared/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\shared'
ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\smtp-connection'
ERROR in ./node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\smtp-connection'
ERROR in ./node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\smtp-connection'
ERROR in ./node_modules/nodemailer/lib/smtp-connection/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\tassc\Projects\Portfolio\node_modules\nodemailer\lib\smtp-connection'
UPDATE: Interesting article about using the google API in Angular https://blog.angularindepth.com/google-apis-with-angular-214fadb8fbc5
Upvotes: 1
Views: 2799
Reputation: 2598
You can follow the Node.js Gmail API quickstart for setting up a workable sample code. Then, you can use the sending mail tutorial to code a function for sending mails to your account.
In the links provided you can see many examples and detailed steps, but if you have any doubts please ask me for clarifications.
EDIT
On the previous quickstart link we all can see the current recommended npm pakpage to interact with the Gmail API: npm install googleapis@39 --save
. You can read more information about Node.js dependencies for Google Libraries here.
Upvotes: 1