How to send scheduled email in javascript? - javascript

I'm using SMTPJS to send emails by Gmail SMTP. Here my simple work code:
<script src="https://smtpjs.com/v2/smtp.js"></script>
sendEmail(to, subject, body){
Email.send(
"SITENAME noti.sitename#gmail.com", //from
to, //to
subject, //subject
body, //body
"smtp.gmail.com", //smtp host
"noti.sitename#gmail.com", //username account
"Noti-Password", //password account
message=>{
alert("sent");
}
)
}
What I need to do is sending an email that should be sent by date. For example after 2 weeks or after 30 days. So is that possible by adding some lines or doing an other way ?

I think it's not possible with only JavaScript For this you need to open your browser for that specific time it can be manage from the server side but as far my concern from client side it can be done with extension only that must be embedded in client browser. You can you use background or cantent script to send the message at the particular time

You can't do this in the Front End because JavaScript on the browser is only executed while the site is opened.
To do it you need a server to run a code every X time, that's a cron.
That code can be written in languages such as Python, JavaScript (Node.js) o PHP.
If you can host that on a website you probably can also run PHP so I recommend you to use PHP. This is how you would do it:
In your HTML use a Form to send the content of the email via POST to a PHP file.
Upload a PHP file that reads the POST parameters and saves a file (for example a JSON) that contains, for each email: the timestamp in which should be sent and the email content.
Upload a PHP file that reads the "pending emails to send" file and sends the emails that have a past timestamp and removes that mail from the file.
Set up a cron that runs the second PHP file every one day at 8am.

Related

is there a way to send an email with data from input without php or javascript just pure simple html?

I have HTML form with a textbox where user enter his email - let say to register for a newsletter and a button to join.
When clicking the button I'd like to send myself an email with a constant subject like: 'a new user joined your newsletter' and in the body of the email have the text entered by the user.
So with PHP and Javascript code its possible - i'm looking for a pure html code that does the same (in my index.html file)
using <a href: mailto...> or <form action=mailto:... method=post> or <button onclick:mailto...> opens my mail application :(
Is there a way to send that email in the background (without opening the mail application) with the data from textbox in the email body?
if Yes to Q.1 then is there a way to add a fixed subject
No, there is no way you can send email using pure html. Email is sent over SMTP protocol. A browser operates in HTTP protocol. So it's not possible to send email even using pure javascript. Only server can send data using SMTP protocol.
No because you can not send an E-Mail without using an SMTP server. This server must be contacted by PHP or another server-side script (server must receive a form request and process it). You can not contact a SMTP server via pure HTML (that would be really insecure btw).
Regarding mailto: This only works if your clients have installed a mail client on their local machine. This mail client must be properly configured in order to - guess what - contact an SMTP server in order to send the mail.
However it is possible passing a subject via mailto:
https://css-tricks.com/snippets/html/mailto-links/

Access PHP SESSION value in JavaScript

I generate a session value for each visitor on my website. If they submit the form, it sends the data via a jQuery AJAX request to my PHP validator script.
This script performs several checks on the data the user submitted. If everything has been validated, it returns a sha256 hash which is generated with the function hash_hmac('sha256', 'success', $_SESSION['secret_key']). I hash this so users cannot manipulate the response with software such as Charles.
The jQuery request receives the hashed string and I have to hash 'success' with the secret key again to check if they match. However, the secret key is stored in a PHP session and I am not able to figure out how to get access to it through JavaScript.
An AJAX request to a PHP script would not be ideal — an attacker can then edit the response to make it match with their own hashed strings.
I'll simply elaborate on my comments in this answer.
You say
An AJAX request to a PHP script would not be ideal — an attacker can
then edit the response to make it match with their own hashed strings.
They can edit the response, but if it's all done client side, they can still edit it.
You want to send the data hashed, then you want the client to be able to check the hash, so I'm not sure what the point in hashing would be, other than security in transport. I can't tell you what you really need, because I'm not seeing the use case here.
I do know you'll either need to go to the server for something you want to keep secret from the client. There's no security on the client side.
As long as you are using javascript in your php file, something like this will suffice...
<script>
var secret_key = <?php echo json_encode($_SESSION['secret_key']); ?>
<script>

Edit file on server from a webpage

What I want to do is to have a set of editable Excel files on my webpage:
I give links to what for the user represents an Excel file
With a click, the user's default program for editing Excel files (say, MS Excel) should open
After finishing editing, the file should be uploaded to my server transparently for the user, and next time the user visits my page, they should see their edited file and be able to edit it again
What I have considered:
JavaScript Excel-like grid. However, I did not find a JavaScript library with sufficient features, such as easily moving rows (any advice of a good JavaScript Excel component?)
Saving to DropBox / Google Docs /... using their APIs. However, it requires the user to have an account, and it will probably require me to manage user's DropBox passwords (and not all users will want to share passwords with me). Also, I will need to have interfaces to Google Drive, Miscrosoft OneDrive, and who knows how many other services.
Allow the user to download the file and rely on the user to upload it back again. However, this is too complicated for the user, and the users will forget to upload the files, which means losing their edits. Any way or uploading the file automatically upon closing?
A macro in my Excel files that would contact my server before exiting. However, this requires the user to enable macros (security alarm) and may be unreliable if the connection breaks. I did not evaluate whether this is technically possible.
Or what is the best / simplest way to achieve this?
(I know how to generate Excel files and how to open them from the webpage; my problem is how to get the user's edits back to the server transparently for the user.)
I think the easiest way to do this ("get the user's edits back to the server transparently for the user") is to use AJAX (JS) requests to PHP scripts.
AJAX is great for doing things in the background (asynchronously), but it can't edit the server. Just add an event listener in JS (an onchange or onblur, perhaps) and send an AJAX request every time the user edits the file.
PHP is a great server-side scripting language, and you can edit files with it.
EDIT: Example (on request)
Assuming that the Excel file is stored in a string in a <textarea> for simplicity (for now), you can set a listener to get the data from it (in jQuery), and send an AJAX request:
HTML:
<textarea id="excel"></textarea>
JS:
$("#excel").change(function() {
var excelFile = $(this).val();
$.ajax({
url: "updateFile.php",
method: "post",
data: { data: excelFile }
});
});
PHP (updateFile.php):
<?php
$data = $_POST["data"];
$file = fopen("FILENAME.xlsx", "w+");
fwrite($file, $data);
fclose($file);
?>

How to execute php script in background process?

I am developing a mail sending module in php; where I will be fetching
email id from database and will send a message to that email id. I will also be updating database with the information I sent in mail and also will display delivered information.
My Problem:
It takes more time to send mail; so until mail is not sent the database does not get updated and the delivered information is also not displayed; So, I planned to run mailing functionality in background process and I came across this piece of code which was similar; but I am unable to understand it. Would any one help me?
shell_exec("/path/to/php /path/to/send_notifications.php '".$post_id."' 'alert' >> /path/to/alert_log/paging.log &")

Node js server: I want to respond to a browser request with both HTML and JSON in the same response, is this possible?

Basically, my thought process is as follows.
Client/browser requests html file login.html. This file contains a form with a username and password field. The submission button sends a POST request to the server with the given user and password field values. Simple, so far.
The server then checks a folder /users of JSON files of the naming convention username.json. For instance, if the client submits the username "John", it checks to see whether or not the file John.json exists. If it does, it compares the password value submitted in the POST request to that of the JSON file. If that matches, it returns the login-success html file, otherwise redirects back home.
That's where my problem lies. If it succeeds, I also wan't it to 'respond' with the JSON file matched to it, but the request seemingy only allows for the response to be one content-type, or to be read from a single file.
So, I'm just not sure how the client will know which JSON file to read based on the given response. I'm sure there's an easy solution and that I'm being an idiot, but any help would be appreciated.
Thanks in advance!
You can respond an HTML file and when document is load, respond the json file,
for example if you use jquery as your client side framework:
$(document).ready(function(){
$.get(URL_To_JsonFile,Params,function(data){
//do somthing with json file
})
})
In the end, I decided to use the Sockets.io module in order to pass the client my json data.
This enabled me to open up a TCP connection on the back of my HTTP server between the server and the client. When the password submitted matches the password in the json file, the server emits a "login-success" event to that client and with that event passes the json file used in the server. The client side script accepts that response and parses that data for use. Here's a sample:
server.js - Server side script
if (password == json.password) {
io.on("connection", function(socket){
socket.emit("login-success", json);
}
}
app.js - Client side script
var socket = io("http://localhost:8888");
var username;
socket.on("login-success", function(json){
username = JSON.parse(json).username;
}
This also means the data only has to be read in once, the object it's assigned to is just passed to the client via the TCP stream established between it and the server. Hoorah!
Thanks very much for the responses everyone.

Categories