cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
lala
Level IX

How to write Python or JSL to automatically log in to an email account to send and receive emails?

Thanks Experts!

Directly read the current JMP table:

Row 1 contains the sender email information

Row 2 contains the receiver email information

After receiving the email, directly write the received content into row 2 of the “Content” column in the current table.

dt = New Table(
    "EmailConfig",
    Add Rows(2),
    New Column("Email", Character, Set Values({
        "A@gmail.com",
        "B@gmail.com"
    })),
    New Column("Password", Character, Set Values({
        "AA",
        "BB"
    })),
    New Column("Content", Character, Set Values({
        "Test",
        ""
    }))
);
3 REPLIES 3
jthi
Super User

Re: How to write Python or JSL to automatically log in to an email account to send and receive emails?

For Python, this isn't correct place to ask. 

For JSL, what type of service do you have? Do you have access to some API to send and receive the emails which JMP can access?

-Jarmo
lala
Level IX

Re: How to write Python or JSL to automatically log in to an email account to send and receive emails?

Thanks Experts!
AI's
it's not work.

dt = Current Data Table();

sender = Char(dt:Email[1]);
pwd_sender = Char(dt:Password[1]);
content = Char(dt:Content[1]);
receiver = Char(dt:Email[2]);
pwd_receiver = Char(dt:Password[2]);

proxy = "--proxy \!"http://127.0.0.1:7897\!"";

message_file = "C:\temp\jmp_message.txt";
Save Text File(message_file, "Subject: Test Email\n\n" || content);

send_options = {"smtp://smtp.gmail.com:587", "--mail-from", sender, "--mail-rcpt", receiver, "--user", sender || ":" || pwd_sender, "-T", message_file, "--ssl-reqd", proxy};

rp_send = Run Program(
	Executable("curl.exe"),
	Options(send_options),
	ReadFunction(Function({this}, send_out = this << Read; Show(send_out); ))
);

Wait(10);

search_options = {"--url", "imaps://imap.gmail.com/INBOX", "--user", receiver || ":" || pwd_receiver, "-X", "\!"SEARCH ALL\!"", proxy};

rp_search = Run Program(
	Executable("curl.exe"),
	Options(search_options),
	ReadFunction(Function({this}, search_out = this << Read; ))
);

match_pos = Contains(search_out, "* SEARCH");
if(match_pos > 0,
	ids_start = match_pos + Length("* SEARCH ");
	ids_end = Contains(search_out, "\r\n", ids_start);
	ids = Substr(search_out, ids_start, ids_end - ids_start);
,
	ids = "";
);

id_list = Words(ids, " ");
if(N Items(id_list) > 0,
	last_id = id_list[N Items(id_list)];
,
	last_id = "";
	dt:Content[2] = "Receive Failed";
	throw();
);

fetch_options = {"--url", "imaps://imap.gmail.com/INBOX", "--user", receiver || ":" || pwd_receiver, "-X", "\!"FETCH " || last_id || " BODY.PEEK[TEXT]\!"", proxy};

rp_fetch = Run Program(
	Executable("curl.exe"),
	Options(fetch_options),
	ReadFunction(Function({this}, fetch_out = this << Read; ))
);

body_start = Contains(fetch_out, "{");
body_end = Contains(fetch_out, ")", body_start);
body_length = Num(Substr(fetch_out, body_start + 1, body_end - body_start - 1));
content_start = body_end + 2;
received_content = Substr(fetch_out, content_start, body_length);

cleaned = Trim(Regex(received_content, "[\\r\\n]+", " ", "GLOBALREPLACE"));

dt:Content[2] = cleaned;


 

jthi
Super User

Re: How to write Python or JSL to automatically log in to an email account to send and receive emails?

I cannot directly help you, but you can use VScript to send mail with JMP, use Mail() command, powershell with Send-MailMessage or other options.

-Jarmo

Recommended Articles