-
-
Save cinco/1e2efe4d67dc69b2a26704378ef358ae to your computer and use it in GitHub Desktop.
Postal - Clear Held with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POSTAL_LOGIN=your-email | |
POSTAL_PASSWORD=your-pass | |
POSTAL_URL=http://your-postal-domain | |
POSTAL_ORGANIZATION=slug-of-your-organization | |
POSTAL_MAIL_SERVER=slug-of-mail-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
from lxml import html | |
import requests | |
import os | |
# import re | |
# --------------------------------- | |
# ----- Environment Variables ----- | |
# --------------------------------- | |
email_address = os.environ['POSTAL_LOGIN'] | |
password = os.environ['POSTAL_PASSWORD'] | |
postal_url = os.environ['POSTAL_URL'] | |
postal_org = os.environ['POSTAL_ORGANIZATION'] | |
postal_mailserver = os.environ['POSTAL_MAIL_SERVER'] | |
# -------------------------------- | |
# ----- Post Login - Payload ----- | |
# -------------------------------- | |
payload = { | |
"utf8": "\u2713", | |
"return_to": "/", | |
"email_address": email_address, | |
"password": password, | |
"commit ": "Login" | |
} | |
# ---------------------------------------- | |
# ----- Get Login authenticity_token ----- | |
# ---------------------------------------- | |
s = requests.session() | |
login_url = postal_url + "/login" | |
result = s.get(login_url) | |
tree = html.fromstring(result.text) | |
authenticity_token = list(set(tree.xpath("//input[@name='authenticity_token']/@value")))[0] | |
payload.update({"authenticity_token":authenticity_token}) | |
# --------------------------- | |
# ----- Make Post Login ----- | |
# --------------------------- | |
result = s.post( | |
login_url, | |
data = payload) | |
if result.status_code == 200: | |
print ('successfully logged in') | |
else: | |
print ('wrong login or password') | |
mensagens = [] | |
# ----------------------------------------------- | |
# ----- Find, unhold and retry send message ----- | |
# ----------------------------------------------- | |
def unheld_message(): | |
url = postal_url + '/org/' + postal_org + '/servers/' + postal_mailserver + '/messages/held' | |
while url: | |
# ---------------- | |
# ----- Find ----- | |
# ---------------- | |
r = s.get(url) | |
soup = BeautifulSoup(r.text ,"lxml") | |
try: | |
url = soup.find('p', {'class': 'simplePagination__next'}).findAll('a', {'class': 'simplePagination__link'}) | |
except: | |
print("Don't has next page") | |
url = [] | |
content = r.content | |
soup = BeautifulSoup(content, features="lxml") | |
message_id = soup.find_all("a", class_="messageList__link") | |
for a in message_id: | |
# ------------------ | |
# ----- Unhold ----- | |
# ------------------ | |
result = s.get(postal_url + a['href']) | |
tree = html.fromstring(result.text) | |
csrftoken = list(set(tree.xpath("//html/head/meta[@name='csrf-token']/@content")))[0] | |
url1 = (postal_url + a['href'] + "/cancel_hold" ) | |
s.post( | |
url1, | |
headers={ | |
'X-CSRF-Token': csrftoken | |
} | |
) | |
# ------------------------------ | |
# ----- Retry send message ----- | |
# ------------------------------ | |
result = s.get(postal_url + a['href']) | |
tree = html.fromstring(result.text) | |
csrftoken = list(set(tree.xpath("//html/head/meta[@name='csrf-token']/@content")))[0] | |
url2 = (postal_url + a['href'] + "/retry" ) | |
s.post( | |
url2, | |
headers={ | |
'X-CSRF-Token': csrftoken | |
} | |
) | |
import re | |
url_path = a['href'] | |
pattern = '[^\/]+(?=\/$|$)' | |
re = re.findall(pattern, url_path) | |
for msg_id in re: | |
print("unhold and retry message, url: " + postal_url + '/org/' + postal_org + '/servers/' + postal_mailserver + '/messages/' + msg_id) | |
# ----------------------------------------------------------- | |
# ----- run again if held page has next page pagination ----- | |
# ----------------------------------------------------------- | |
if url: | |
url = postal_url + '/org/' + postal_org + '/servers/' + postal_mailserver + '/messages/held' | |
else: | |
break | |
unheld_message() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beautifulsoup4==4.10.0 | |
html==1.13 | |
requests==2.26.0 | |
lxml==4.7.1 |
@devopsmash, your solution is working! Anyway, could you add this tool to remove the email queue? I have a large amount of spam and causing the server to freeze.
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much @cinco for your script, but it seems that python3 and pip3 aren't like the old
html
pip packageSo I made a few changes to support
python3
andpip3
First, remove
html
package from therequirements.txt
Second, replace the
html
onpostal-clear_held.py
:and
now you can run it without any issue