Open local mbox mail archive with imap_open() in PHP - php

I'm attempting to read an mbox email archive exported from another server locally, via file access, but for whatever reason everything I've tried fails. Is there some magical trick to parse a local file and access it with PHP's built-in IMAP functionality?

You should be able to use PHP's built-in IMAP functionality. Have you tried something like this:
function openLocal($file_path) {
$mbox = imap_open("$file_path",'','');
if (!mbox) {
$errorMsg = imap_last_error(); // do something with the error...
return false;
} else {
return true;
}
}
And call this with the respective correct path:
openLocal('/home/email/temp/mailbox')

I didn't find the accepted answer sufficient, though it did point me in the right direction.
PHP's IMAP library can be used to parse local .mbox files, such as the ones from Gmail exports.
Importantly, the path must be absolute, it cannot be relative to the current folder.
$imap = imap_open($path_to_mbox, '', '');
You can then get retrieve information with imap functions, such as the subject:
$headers = imap_headerinfo($imap, 0); // Second parameter is the message number
$subject = $headers->subject;

Related

PHP mail successful with empty to parameter

I am learning PHP and from what I understand the mail function has a to parameter that needs to comply with a certain string format - php doc. I have read that if I parse an empty string then the mail function will return false (not 1). However, when I try this the mail function never fails. Is there something I have missed?
Code:
<?php
if (mail('', 'mySubject', 'myMessage')) {
echo "Success!";
} else {
echo 'Failure!';
}
?>
The output is "Success!"
Only by removing the argument entirely gets the else statement to execute. Does the to parameter need to be of a certain string format like the documentation states and if not, then how can I get this function to fail? Thanks
The mail() function does not validate the input. It more or less just takes the data and hands it over to the system mailer daemon.
If that handoff was successful, the method returns true.
It it likely that your local mailer daemon will log an error that it couldn't process the email
php mail() function is just a middle-man between your mail daemon and your code.
You need to manually validate email addresses, typically using something like
if( filter_var( $email_address ,FILTER_VALIDATE_EMAIL ) )
{
mail(parameters);
}
else {
// handle error
}
Like #skaveRat said, you'll most probably find something in your email daemon log that this mail was not sent.

What is the modern way to check if user is requesting site using Tor? (php)

I've tried a lot of ways but all of them are not working for me, I guess they are outdated and things changed. Maybe someone can advise me the direction to dig out?
I am the author of a PHP library called TorUtils that provides a number of classes related to Tor and relays.
One of the classes it provides is TorDNSEL example which gives you a simple interface to query the Tor DNS exit list to see if the remote IP is a Tor relay accessing your site.
The usage is simple (example updated 2022/02/04):
<?php
use Dapphp\TorUtils\TorDNSEL;
require_once 'src/TorDNSEL.php';
// Practical usage on a web server:
try {
if (TorDNSEL::isTor($_SERVER['SERVER_ADDR'])) {
// do something special for Tor users
} else {
// not using Tor, educate them! :-D
}
} catch (\Exception $ex) {
error_log("Tor DNSEL query failed: " . $ex->getMessage());
}
The TorDNSEL script doesn't require any of the other classes and can be downloaded and used as a standalone script. It makes a DNS request, but builds it directly so you also don't need any DNS functions or libraries available to PHP.
Alternatively, I use the ControlClient class to maintain very fresh exit lists. You can build your own lists using that (you'll need a Tor relay of your own to connect to, or you could use the Directory Authorities to build lists as well). Even easier, just periodically download and check the exit lists I export here. The list is updated every 10 minutes so please try not to download it more than once every 10 minutes or so.
Then save it to your site somewhere and check the client IP against the ones in the list.
You can download the source from GitHub or composer require dapphp/torutils to install to an existing project.
Hope that helps, let me know if I can answer anything further.
Tor browser or not checking in PHP code I am using currently:
<?php
namespace neoistone;
class TOR
{
public static function isTorRequest()
{
$ipServeur = $_SERVER['SERVER_ADDR'];
$ipUser = $_SERVER['REMOTE_ADDR'];
if(is_file(TMP.'torips.db') == false){
self::tor_ips_update();
}
if (self::match_ips($ipServeur)) {
return true;
} elseif (self::match_ips($ipUser)) {
return true;
} else {
return false;
}
}
public static function tor_ips_update()
{
file_put_contents(TMP.'torips.db',file_get_contents('https://check.torproject.org/torbulkexitlist'));
}
public static function match_ips($ip)
{
$lines = file(TMP.'torips.db', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
return in_array($ip, $lines);
}
}
?>
Live URL: https://reseller.neoistone.com/api/
The way check.torproject.org uses is
For every user that hits the website, we check if their IP matches a known exit node's IP, and that its exit policy would allow accessing this website on port 443(HTTPS). If it is, then we can make a good assumption that the user has successfully connected via the Tor network.
You can find its source at https://git.torproject.org/check.git. It is under MIT licence. You can use Exits.IsTor in datastore.go to determine whether the request comes from tor. You need to change
var DefaultTarget = AddressPort{"38.229.72.22", 443}
to your server's IP address and port. You will also need some interface written in Go if you are using PHP if you want to use their code.
If you use Cloudflare, it uses the 2-letter code "T1" for Tor, and you can have custom rules for Tor.
Note that it is hard to distinguish a request directly from a Tor exit node (not via Tor) from a request from Tor. Most places assume they are all from Tor.

Why is_dir() function is not working for Network paths in PHP?

I am checking in my code, if directory exists or not with is_dir().
It works for local drives, but not for network paths.
Can anyone help me out?
Here Is My Sample Code:
public function setXMLFilePath($filePath) {
if(is_dir($filePath)) {
$this->XMLFilePath = $filePath;
$retVal = true;
} else {
$ratVal = false;
}
return $retVal;
}//setXMLFilePath
And My Network Path is Like this:
$filePath = '\\Nas-heidi\heidi\FAS\Polish GameRobot\Export_Raffle\';
The file-related functions wrap over a few protocols, the Windows networking schema is not one of them.
Also notable is the fact that when you are accessing one of these (external) protocols you should not be using shorthands such as \\\network_computer\network_folder\ you should be using full protocol specifications such as ftp:///http:///ssh:// and if the protocol for Windows networking existed it would probably have such an identifier.
This is probably because the account that run PHP script (maybe the count that start the Apache service) has no permissions for such directories.

pipe mail to PHP script : cant find the pip.php file

hi i'm trying to pipe incoming emails to php script
i did step by step from my manual but when i send an email i get this error from my delivery system :
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es)
failed:
pipe to |/home2/mimjobco/public_html/pipe.php
i chosse pipe.php address from cpanel so it cant be wrong
here is the picture !
here is my pipe.php code :
#!/usr/bin/php –q
<?php
require_once('class/support.php');
require_once('class/db.php');
$title = 'email_request';
$text = 'email_req_text';
$sup_id = 1 ;
$sup = new support;
$sup->title = $title;
$sup->part_id = $sup_id ;
$sup->text = $text;
$sup->email = 'email';
$sup->type = 1;
$sup->set_ticket();
mail('xxxx#gmail.com','new message recived','new message recived ');
i also set it's permission to 755
am i missing something ?
is there another thing that i should have done? someone had mention something about crone job ?!
The error message says "/home2/mimjobco/pipe.php", but the screenshot shows the file is in your "public_html" directory (presumably, "/home2/mimjobco/public_html/pipe.php").
Note that "pipe.php" should only be in "public_html" if you want it to be accessible on the website. Otherwise, it should go elsewhere in your home directory hierarchy (e.g. ~/bin/).
It looks like you are using bluehost, here is someone else who was able to fix the problem: http://www.bluehostforum.com/showthread.php?5786-Reading-email-with-php&p=26554#post26554
Setting up filter in cPanel, problem that delivery error coming back. Many solutions not available on a shared server.
Finally, this seems to work to avoid the error in delivery emails coming back - note the -q:
|/usr/bin/php -q /home/myacct/public_html/mydir/myfile.php
Solution came from http://forums.exocrew.com/index.php?showtopic=1838. (DEAD LINK)
The code for parsing emails being piped to the php file is at:
http://evolt.org/node/27914/
This gives you the message parsed as variables for subject, from, body etc.
Hope this helps!

error on using #fopen

I'm using #fopen to open a file in "rb" mode. the file that im opening here is running with no errors but if i open that file using #fopen then it is giving error.
code is something like this---
$file = #fopen("xyz.com","rb") or $flag=1;
if($flag==1)
{
mail($to, $subject, $message, $from);
die();
}
sometimes it opens up without sending any error mail but sometimes it starts giving so many error mails.
what is the solution to open this url without having any error mail? plz help!!
If you're trying to open a URL (presuming from the 'xyz.com' you included), then you need to include the schema declaration before it. E.g. http://xyz.com, otherwise PHP will attempt to open a local file. If you're referring to a local file, make sure to escape any back-slashes if you're on Windows.
However, there's nothing else inherently wrong with the rest of your code sample that should cause a problem. # simply suppresses error outputs, so it won't be causing any odd behaviour in and of itself.
Though, that said, a better way to handle it might be to do this:
$file = #fopen("xyz.com","rb");
if(!$file)
{
mail($to, $subject, $message, $from);
die();
}
Try to use the
file_get_contents();
function instead of fopen().
by the way, you are setting $flag = 1 when there is an error. but what if there was an error last time and this time there is no error? (then $flag is still 1 from the previous time).
remove the '#' charactor from the start of the fopen method, (ther presence of the # symbol supresses any php driven error message) this will give you the explanation of why php thinks you cant open that file - i would hazard a guess either the path to the file or the permissions of the file are invalid.
What is error message? We can just guess about the problem without it.
Is url fopen always allowed in your ini? Maybe this value overrides somewhere with ini_set()?
Are you sure, that url is correct and host is alive?
Finally, I recommend to use fsockopen instead. It provides more flexible remote connections, error handling for them and possibility to set the connection timeout.
The # symbols suppresses errors so $flag will never be set

Categories