I am working on a project in which I have to fetch data from gmail inbox and save it into mysql database using php. I tried different code but it shows different errors like:
imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX
and also saw a notice that is
Too many login failures (errflg=2) in Unknown on line 0
I am working on Windows 8 and using php version 5.4.12.
Below is my code:
<?php
$email = "xxx#gmail.com";//or my gmail account
$password = "my gmail paasoward";
$imap_host = "{imap.gmail.com:993/imap/ssl}";
$imap_folder = "INBOX"; //it's what is called label in Gmail
$mailbox = imap_open($imap_host . $imap_folder,$email,$password) or die('Failed to open connection with Gmail: ' . imap_last_error());
$emails = imap_search( $mailbox, 'ALL');
if ($emails) {
foreach($emails as $email_id) {
$email_info = imap_fetch_overview($mailbox,$email_id,0);
$message = imap_fetchbody($mailbox,$email_id,2);
echo "Subject: " . $email_info[0]->subject . "\n";
echo "Message: " . $message . "\n";
}
}
?>
it seems like that you have to enable the IMAP setup in the php.ini file, if you are using local xampp then go to the php.ini file where you have to remove the; option in the line of extension= php_imap.dll
First go to the php.ini file, and here you find extenstion with relate to IMAP extenstion.
it starts with semicolon ; you need to remove semicolon. after that a new file automatic created in folder php/ext you find a new file with name php_imap.dll, copy this and paste into windows/system32 folder.
Related
My script packs some files with ZIP then uploads this ZIP to another server. After the upload it checks size of the ZIP on the FTP and locally. If sizes are the same- the local ZIP is deleted.
The problem is ftp_size() returns -1. But only in the real script. In my test script it works fine.
Test script works like this:
$f = ftp_connect(HOST);
$res = ftp_login($f, USER, PASS);
$fname = 'archive_2018-09-18_13-39';
$fsize = ftp_size($f, "$fname-img.zip").'';
$fsize2 = filesize("backup/$fname-img.zip").'';
echo $fsize . '<br>' . $fsize2;
and it returns:
22907946995
22907946995
The real script works like this:
$f = ftp_connect(HOST);
$res = ftp_login($f, USER, PASS);
$fname = 'archive_' . date('Y-m-d_H-i');
exec("zip -r -0 backup/$fname-img.zip \"website\" 2>&1");
exec('curl -T "' . "backup/$fname-img.zip" . '" ftp://' . HOST . ' --user ' . USER . ':' . PASS . ' 2>&1');
$fsize = ftp_size($f, "$fname-img.zip").'';
$fsize2 = filesize("backup/$fname-img.zip").'';
echo $fsize . '<br>' . $fsize2;
and it shows:
-1
22907946995
Real script uploads file just fine. It just doesn't show correct size on the FTP server.
So it's not a problem with size of the file and not a problem with FTP connection.
Try moving ftp_connect only after the call to curl. There is possibly some caching involved that prevents the FTP server from returning correct size immediately, if the file is uploaded using a different connection.
Though I'd strongly suggest you to use PHP functions to upload the file.
I am using the following code in elastic beanstalk php to generate log file but I am getting this error.But the same code is working on another server. How can I make it work ?
Error:
Error opening file in write mode!
Code :
$path="/var/www/html/aws";
$fileName =$path.'/logs/data'.date('Y-m-d_H-i-s'). '_' . uniqid() . '.txt';
$file = fopen($fileName,'w') or die('Could not create report file: ' . $fileName);
foreach($_POST as $key => $value) {
$reportLine = $key." = ".$value."\n";
fwrite($file, $reportLine) or die ('Could not write to report file ' . $reportLine);
}
fputs($file,"log aws");
fclose($file);
Make sure your php user www-data have permission to write in $path.'/logs/ folder or just chmod that logs folder to 0777 permisson
I have a testcode.php file need to encode:
<?php
$hello = "Hello World!";
?>
And I created file encode.php to encrypt and test that file:
<?php
/* read the PHP source code */
$source_code = file_get_contents("testcode.php");
/* create the encrypted version */
$redistributable_key = blenc_encrypt($source_code, "encrypt.php");
/* read which is the key_file */
$key_file = ini_get('blenc.key_file');
/* save the redistributable key */
file_put_contents($key_file, $redistributable_key, FILE_APPEND);
include 'encrypt.php';
echo $hello;
?>
but I recevied these errors when I ran encode.php:
Warning: blenc_compile: Validation of script 'encrypt.php' failed.
MD5_FILE: 910e6a45f806ba3dc42830839971cb53
MD5_CALC: c38a6b2f389267a272ea656073a463ed in
C:\xampp\htdocs\PHPEncode\encode.php on line 14
and
Fatal error: blenc_compile: Validation of script 'encrypt.php' failed,
cannot execute. in C:\xampp\htdocs\PHPEncode\encode.php on line 14
Help me fix it, thank you! :)
BLENC has issues when there is more than one redistributable key in blenc.key_file. See PHP bug #68490 that I've reported.
Also when you run your script multiple times, redistributable keys will get corrupted in blenc.key_file. This is because you are appending to the file, but all keys are saved on the same line (the same broken example is on php manual page). You should change it to:
file_put_contents($key_file, $redistributable_key."\n", FILE_APPEND);
The second Fatal error you got was probably because of corrupted blenc.key_file.
;) just delete "<?php ?>" in your page *.php
compiled this not with "<?php and ?>"
just
$hello = "Hello World!";
and is ok :) !
<?php
$file_name = basename($file);
$source_code = file_get_contents($file);
//This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
$html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
$html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
//file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
$redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
$html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
/**
* Server key
* key_file.blenc
*/
file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
$html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
?>
https://github.com/codex-corp/ncryptd/blob/master/app/controllers/MagicalController.php#L479
You need to specify the full location of the blenc.key_file in php.ini variable called blenc.key_file or via .htaccess , setting at runtime with ini_set() is not possible (at this moment the key file has already been read).
.htaccess example:
php_value blenc.key_file /path/path/path/key_file.blenc
Every time you encrypt a file a new $redistributable_key will be generated!
You have to include all keys in the key#
Or use a fixed (private) encryption key for all your encryption:
$private_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxVCHANGEME";
$redistributable_key = blenc_encrypt($source_code, "encrypt.php", $private_key);
I cannot figure out what I am doing wrong here. The permissions for the directory I have for the file being created have write permissions all across the board. I keep getting "directory does not exist" Thanks for the help!
<?
//creates variables and calls the information from the server
$Name = $_POST['name'];
$desc = $_POST['desc'];
$website =$_POST['web'];
$email =$_POST['email'];
$cname =$_POST['cname'];
echo "your registered name is: ". $Name . ".<br/>";
echo "your registered description is: " . $desc . ".<br/>";
echo "your website address is: " . $website . ".<br/>";
echo "your Confirmation email has been sent to: " . $email . ".<br/>";
echo "your information has been stored, thank you! ";
$cname = trim($cname);
$filename = "data/clubinfo/$cname.txt";
$fp = fopen($filename,'a');
fwrite($fp,$Name);
fwrite($fp,"\n");
fwrite($fp,$email);
fwrite($fp,"\n");
fwrite($fp,$desc);
fwrite($fp,"\n");
fwrite($fp, $website);
fwrite($fp, "\n");
fwrite($fp,"__");
fwrite($fp, "\n");
fclose($fp);
?>
Most likely the script is assuming a different working directory to what you're presuming since you're using a relative path.
You'd be better off specifying the path absolutely or at least in relation to $_SERVER['DOCUMENT_ROOT'] even if you do:
$filename = $_SERVER['DOCUMENT_ROOT'] . "../data/clubinfo/$cname.txt";
The advantage of that is that it's outside your document root so it won't be served directly by your Web server. It will also work no matter the location of your script and will work no matter under what directory you install your Webapp, which can be an issue with dev vs prod deployments.
The data/clubinfo folder does not exist in the current directory.
You need to create it first. (By hand or in PHP)
Alternatively, the current directory might not be what you think it is.
Try using file_put_contents() like this:
file_put_contents("data/clubinfo/$cname.txt", implode("\n", $_POST));
If you want to this value by value you should also use the FILE_APPEND flag.
looks like you have not given the path off the root and the server is looking from the current location. try giving the path off the root.
I'm writing a photo gallery webapp for a friend's wedding and they want a photo gallery for guests to submit the digital photos they take on the day.
After evaluating all the options, I've decided the easiest thing for users would be to let them use a familiar interface (their email) and just have them send in the pictures as attachments.
I've created an mailbox but now I need to connect and retrieve these attachments for automated processing for adding to the gallery system. But how? Are there any tutorials or prefab classes you've seen for doing this?
I used to do a lot of this before, but I can't find the code, here's a scaled down version I found. It should put you on the correct path. I used to run this type of script from a cronjob. Sorry I can't find the final version. ;(
// Open pop mailbox
if (!$mbox = imap_open ("{localhost:110/pop3/notls}INBOX", "user", "tester")) {
die ('Cannot connect/check pop mail! Exiting');
}
if ($hdr = imap_check($mbox)) {
$msgCount = $hdr->Nmsgs;
} else {
echo "Failed to get mail";
exit;
}
$MN=$msgCount;
$overview=imap_fetch_overview($mbox,"1:$MN",0);
for ($X = 1; $X <= $MN; $X++) {
$file = imap_fetchbody($mbox, $X, 1);
imap_delete($mbox, $X);
}
imap_expunge($mbox);
imap_close($mbox);
Good luck!
Have you considered using Google's Picasa Web Albums?
You can set up an email address to send photos to and share them online.
You can then get an RSS feed of these photos, which most programmers are
more familiar with than MTAs.
If you're creating a dedicated mailbox for this purpose, using a filtering mechanism is almost definitely not what you want. Instead, you want to have the mailbox be a pipe to the application, and have the application simply read in the message from stdin, parse out the body, and MIME parse the body to get the attachments.
Having a mailbox be a pipe is supported by all the popular unix-based MTAs that I know of, such as sendmail, postfix, and qmail. Generally you define it in your aliases file, like so:
# sendmail or postfix syntax
msgsubmit: "| /usr/bin/php ~path/to/example.php"
Then mails to msgsubmit# get routed to a php program for delivery.
This has the advantage of not relying on an IMAP server or any other server beyond the MTA being alive, and it works fine as long as you have control over the MTA of the destination host. Filtering is what you'd want if you wanted all messages on a system to be inspected by the script, which I'm guessing is not the case.
If you want a copy kept in a mailbox somewhere (not a bad idea) simply define the alias to go to multiple addresses, like so:
msgsubmit: "| /usr/bin/php ~path/to/example.php", msgsubmit-box
Or postfix virtual format:
msgsubmit
"| /usr/bin/php ~path/to/example.php"
msgsubmit-box
What MTA are you using? If you use postfix + maildrop you can create a filtering rule that pipes certain messages through a PHP script that then handles the incoming mails. (google for maildrop and xfilter).
I think you want a MIME message parser.
I've used this one before and it seems to work fine, although I haven't tested it on really big attachments (i.e. 2-3MB files you might get from digital cameras).
Have you already got a system for reading POP3 / IMAP mailboxes? There is another class on the same site which also works on POP3 (I believe there is also an IMAP one) - however if you will be downloading a fair volume maybe you'll want to investigate a few C-based solutions as I believe that one is pure PHP.
Majordomo, could be an alternative to handle emails, but there are some limitations on file attachment handling.
<?php
//make sure that submit button name is 'Submit'
if(isset($_POST['Submit'])){
$name = $_POST['visitorname'];
$email = $_POST['visitoremail'];
$message = $_POST['visitormessage'];
$to="youremail#yourdomain.com";
$subject="From ".$name;
$from = $email;
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
foreach($_FILES as $userfile)
{
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name))
{
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name))
{
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$ok = #mail($to, $subject, $message , $headers);
if ($ok) {
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
}
}
}
else
{
}
echo "<span class='red'>E-mail has been sent successfully from $mail_name to $to</span>"; }
else{
echo "<span class='red'>Failed to send the E-mail from $from to $to</span>";
}
}
?>
p/s:I used this code.hope its work and assist you.just copy and paste.make sure your textfield name is same as in this page.its work for all types of files.for further questions,just email me at shah#mc-oren.com.anyway,i also in learning process.=).thanks.