-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect-spam
132 lines (113 loc) · 3.02 KB
/
redirect-spam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/php
<?php
/*
* @author Johannes Cram <[email protected]>
*/
function includeIfExists($file) {
if (file_exists($file)) {
return include_once $file;
}
}
//check if dependencies are set up
if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../autoload.php'))) {
echo PHP_EOL.
"// You have to install the project dependencies via composer!\n\n".
die();
}
use SSilence\ImapClient\ImapClientException;
use SSilence\ImapClient\ImapConnect;
use SSilence\ImapClient\ImapClient as Imap;
/*
* Set variables
*/
$imapHost = 'imap-mail.outlook.com';
$imapPort = 993;
$imapEnc = 'tls';
$smtpHost = 'smtp-mail.outlook.com';
$smtpPort = 587;
$smtpEnc = 'tls';
//show help
if($argc != 4 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
echo PHP_EOL."// CLI Tool to redirect emails from an outlook.com spam folder to another email address.\n";
echo PHP_EOL."// Usage: 'php bin/redirect-spam <authEmail> <authPassword> <sendToEmail>'\n\n";
die();
}
$authEmail = $argv[1];
$authPass = $argv[2];
$sendTo = [ $argv[3] ];
//Construct Imap Client and check login data
try {
if($imapEnc == 'tls') {
$enc = Imap::ENCRYPT_TLS;
}
else {
$enc = null;
}
$imap = new Imap($imapHost,$authEmail,$authPass,$enc);
}
catch(ImapClientException $error) {
echo $error->getInfo().PHP_EOL;
die();
}
// select folder Junk
$imap->selectFolder('Junk');
// count messages in current folder
$intAll = $imap->countMessages();
if($intAll > 0) {
//fetch all messages in the spam folder
$emails = $imap->getMessages(true);
//initialize Swiftmailer
$transport = (new Swift_SmtpTransport($smtpHost,$smtpPort,$smtpEnc))
->setUsername($authEmail)
->setPassword($authPass);
$mailer = new Swift_Mailer($transport);
$arrCounter = 0;
$failures = [];
$successes = 0;
foreach($emails as $id => $email) {
//get data
$fromAddress = $email->header->details->from[0]->mailbox.'@'.$email->header->details->from[0]->host;
$fromName = $email->header->from;
$subject = '[FROM SPAM] '.$email->header->subject;
$isHtml = $email->message->html->structure->subtype == 'HTML';
if($isHtml) {
$htmlBody = $email->message->html->body;
}
$plainBody = $email->message->plain->body;
//prepare message
$message = (new Swift_Message($subject))
->setReplyTo([$fromAddress => $fromName])
->setFrom([ $authEmail => $fromName ])
->setTo($sendTo);
if($isHtml) {
$message->setBody($htmlBody,'text/html')
->addPart($plainBody,'text/plain');
}
else {
$message->setBody($plainBody,'text/plain');
}
//send message
$arrCounter++;
$result = $mailer->send($message,$failure);
if(!$result) {
$failures[] = $failure;
}
else {
$successes += $result;
}
//move message to inbox
$imap->moveMessage($email->header->uid,'Inbox');
}
if($failures) {
echo PHP_EOL."Failures: \n\n";
print_r($failures,true);
echo "\n\n";
}
if($successes) {
echo PHP_EOL."Mails successfully sent: ".$successes."\n\n";
}
}
else {
echo PHP_EOL."Nothing to do.\n\n";
}
?>