Open Mailbox with PHP imap_open, DomainFactory mail - php

I tried to open my mail server with imap_open(). I always get this error:
Warning: imap_open(): Couldn't open stream {sslin.df.eu:993/novalidate-cert}
I tried this code:
{sslin.df.eu:993/novalidate-cert}INBOX
{sslin.df.eu:993}INBOX
{sslin.df.eu:993}
{sslin.df.eu:993/imap/ssl/novalidate-cert}
{sslin.df.eu:993/imap/ssl/novalidate-cert}INBOX
{sslin.df.eu:993/imap/novalidate-cert}INBOX
{sslin.df.eu:993/novalidate-cert}INBOX
my current code:
//The location of the mailbox.
$mailbox = '{sslin.df.eu:993/novalidate-cert}INBOX.';
//The username / email address that we want to login to.
$username = 'example#example.com';
//The password for this email address.
$password = 'xxx';
//Attempt to connect using the imap_open function.
$imapResource = imap_open($mailbox, $username, $password);
//If the imap_open function returns a boolean FALSE value,
//then we failed to connect.
if($imapResource === false){
//If it failed, throw an exception that contains
//the last imap error.
throw new Exception(imap_last_error());
}
//If we get to this point, it means that we have successfully
//connected to our mailbox via IMAP.
//Lets get all emails that were received since a given date.
$search = 'SINCE "' . date("j F Y", strtotime("-7 days")) . '"';
$emails = imap_search($imapResource, $search);
//If the $emails variable is not a boolean FALSE value or
//an empty array.
if(!empty($emails)){
//Loop through the emails.
foreach($emails as $email){
//Fetch an overview of the email.
$overview = imap_fetch_overview($imapResource, $email);
$overview = $overview[0];
//Print out the subject of the email.
echo '<b>' . htmlentities($overview->subject) . '</b><br>';
//Print out the sender's email address / from email address.
echo 'From: ' . $overview->from . '<br><br>';
//Get the body of the email.
$message = imap_fetchbody($imapResource, $email, 1, FT_PEEK);
}
}

Related

how can i check how much i have downloaded from gmail using imap

I'm new to using imap with php to get message from gmail.
In gmail docs they said
Important: To avoid temporarily locking yourself out of your account, make sure you don't exceed 2500 MB per day for IMAP downloads and 500 MB per day for IMAP uploads. If you're setting up a single IMAP account on multiple computers, try taking a break between each setup.
Here's the link
I'm using this code to get mail from INBOX folder
// Connect to gmail
$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'exampleusername';
$password = 'examplepassword';
// try to connect
$inbox = imap_open($imapPath, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
// search and get unseen emails, function will return email ids
$emails = imap_search($inbox, 'UNSEEN');
$output = [];
foreach ($emails as $mail) {
$headerInfo = imap_headerinfo($inbox, $mail);
$output[] = $headerInfo->subject . '<br/>';
$output[] = $headerInfo->toaddress . '<br/>';
$output[] = $headerInfo->date . '<br/>';
$output[] = $headerInfo->fromaddress . '<br/>';
$output[] = $headerInfo->reply_toaddress . '<br/>';
$emailStructure = imap_fetchstructure($inbox, $mail);
if (!isset($emailStructure->parts)) {
$output[] = imap_body($inbox, $mail, FT_PEEK);
} else {
//
}
print_r($output);
$output = [];
}
// colse the connection
imap_expunge($inbox);
imap_close($inbox);
My question is how i can check for how much memory i had using for IMAP dowloads and for IMAP uploads?

How to exclude IMAP-Folder in INBOX from reading with imap_open (PHP)

I´m working on a script in PHP that reads all mails in INBOX, detects special mails via Header-Info and moves processed mails into a folder. I want to exclude this folder from further reading, at the moment my script also finds mails that are already in the processed folder. I´m using the imap_open function like this:
$host = "{" . HOST . ":" . PORT . "/imap/ssl/novalidate-cert}INBOX";
$email = EMAIL;
$password = PASSWORD;
$imap = imap_open($host, $email, $password) or die("Cannot Connect " . imap_last_error());
$emailIds = imap_search($imap, 'FROM ' . SCAN_EMAIL_FOR_OFFERS, SE_UID);
if (!empty($emailIds)) {
foreach ($emailIds as $emailId) {
$email = new readMail();
$header = $email->get_headerinfo($imap, $emailId);
if (strpos($header->subject, "Angebot Nr.") != false) {
...Doing stuff...
$email->move_mail($imap, $emailId, MOVE_PROCESSED_TO_FOLDER);
}
}
} else {
echo "No new offers.";
}
imap_close($imap);
Regards
Edit: This is the move-func of readMail-Class...
function move_mail($imap, $uid, $inboxFolder) {
$msgNo = imap_msgno($imap, $uid);
$movingMail = imap_mail_move($imap, $msgNo, 'INBOX/'. $inboxFolder);
if($movingMail == false) {die(imap_last_error());}
}

PHP IMAP : How to get the correct body?

I am connecting to my inbox via PHP IMAP plugin. Below are my steps
Connection
//The location of the mailbox.
$hostname = '{outlook.office365.com:993/imap/ssl/novalidate-cert}';
//The username / email address that we want to login to.
$username = 'username';
//The password for this email address.
$password = 'password';
I am further opening the inbox connection and looking at anything that is UNSEEN in the inbox
//Attempt to connect using the imap_open function.
$inbox = imap_open($hostname,$username,$password);
$mailboxes = imap_list($inbox, $hostname, '*');
imap_reopen($inbox, $hostname.'INBOX');
$emails = imap_search($inbox, 'UNSEEN');
Further, I am iterating over all the emails and its working well with headers. The issue I am having is with body.
To get the body of the email I am using
//get message body
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 2.1));
if ($message == '') {
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 1));
}
So running a test on 40 emails, 28 emails fetch the body correct and store into the database.
12 emails fetch a body which looks like
PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiIHhtbG5zOm89InVy
bjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9mZmljZSIgeG1sbnM6dz0idXJuOnNjaGVt
YXMtbWljcm9zb2Z0LWNvbTpvZmZpY2U6d29yZCIgeG1sbnM6bT0iaHR0cDovL3NjaGVtYXMubWlj
cm9zb2Z0LmNvbS9vZmZpY2UvMjAwNC8xMi9vbW1sIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv
VFIvUkVDLWh0bWw0MCI+DQo8aGVhZD4NCjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIg
Y29udGVudD0idGV4dC9odG1sOyBLWZhbWlseToiQ2FsaWJyaSIsc2Fucy1zZXJp
ZjsNCglmb250LXdlaWdodDpib2xkO30NCmE6bGluaywgc3Bhbi5Nc29IeXBlcmxpbmsNCgl7bXNv
LXN0eWxlLXByaW9
How should I be reading the body? So that I can have my test pass all the emails correctly?
Thank you
Also I found a way to get rid of those characters.
I looked at the encoding and I am parsing based on encoding numbers as follow
$encoding = $structure->encoding;
if($encoding == 3) {
$finalmsg = imap_base64($message);
} else if($encoding == 1) {
$finalmsg = imap_8bit($message);
} else {
$finalmsg = imap_qprint($message);
}
I am still having issues when encoding type = 0. The signature is causing major problem

Imap count all mails of email account

I'm trying to count all emails from and email but my script only count mails from inbox,
anyone know how to count all emails from the mail account including sent,spam,deleted, etc
$mailcnf = "mail.office365.com:993/imap/ssl/novalidate-cert";
$conn_str = "{".$mailcnf."}INBOX";
$username = 'test3#sjnewman.co.uk';
$password = 'Woju6532';
$imap = imap_open($conn_str,$username,$password) or die('Cannot connect to Server: ' . imap_last_error());
echo $message_count = imap_num_msg($imap);
first use imap_list to list all available folders.
then $conn_str = "{".$mailcnf."}$mailbox"instead of mailbox
imap_num_msg should return the number of emails in the current mailbox
You can loop through each folder and use imap_status() to count the number of emails in each folder. Here's an example:
<?php
$username = 'mail#example.com';
$password = 'password123';
// Define the connection string:
$server = '{server.example.net:993/ssl}';
// Connect to the server:
$connection = imap_open($server, $username, $password);
// List the mailboxes:
$mailboxes = imap_list($connection, $server, '*');
// Loop through the mailboxes:
foreach($mailboxes as $mailbox) {
$status = imap_status($connection, "$mailbox", SA_ALL);
if ($status) {
echo "Mailbox: $mailbox\t\tMessages: " . $status->messages . "\n";
} else {
echo "imap_status failed: " . imap_last_error() . "\n";
}
}
// Close the connection:
imap_close($connection);
?>

Wordpress PHP Server Script to send email

I am trying to get a server script to use wp_mail() and send an email to a user. I am doing this as part of a password reset routine, and calling the script with Ajax from the user's side.
I am getting the following error from my server script:
Fatal error: Call to undefined function wp_mail() in /var/www/abc-php/pwd.php on line 57
I cannot seem to find an answer to my question, so maybe I am asking incorrectly. Is what I am doing possible?
My server script (pwd.php) contains:
<?php
$setFromId = "info#abc.com.au";
$setFromName = "Info # abc";
$replyToId = "info#abc.com.au";
$replyToName = "Info # abc";
$sendToEmail = base64_decode($_GET["ste"]); //the URL in AJAX call contains base64 encoded data
$resetPwd = base64_decode($_GET["rp"]);
$resetSalt = base64_decode($_GET["rs"]);
$param = $_GET["var"];
$username = "xxxxxxxxx";
$password = "xxxxxxxx";
$host = "localhost";
$database = "xxxxxxxxx";
$con = mysqli_connect($host, $username, $password, $database);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
switch ($param) {
case "PWDRES": // Query 1: Current Logged In Partner Details
$mysqli = new mysqli($host, $username, $password, $database);
if (mysqli_connect_errno()) {
echo '<p>Unable to connect to the server at this time to update and send password. Please contact SMA for help or try again.</p>';
} else {
$myquery = "UPDATE abc_users
SET `password` = SHA2('" . $resetPwd . $resetSalt . "', 512),
`salt_value` = SHA2('" . $resetSalt . "', 512)
WHERE email_user_id = '" . $sendToEmail . "'";
mysqli_query($mysqli, $myquery);
if($mysqli->affected_rows >= 1) {
$headers = 'Reply-To: <' . $replyToName . '> ' . $replyToId;
$subject = "Password Reset - Confidential";
$body = "Subject: " . $subject . "<br/><br/>Your new password is: <br/><strong>" . $resetPwd . "</strong><br/><br/>Please ensure you reset this password next time you log in.";
$mail = wp_mail($sendToEmail, $subject, $body, $headers);
echo "Password was successfully reset and sent to " . $sendToEmail;
} else if($mysqli->affected_rows == 0) {
echo "No user id row was updated in the process of resetting password. Please try again.";
} else if($mysqli->affected_rows < 0) {
echo "There was an SQL error updating the user id password table. Please contact SMA for help.";
};
};
break;
case "OTHER1"
etc, etc, etc...
default;
};
};
unset($con);
unset($myquery);
unset($mysqli);
?>
Any help is appreciated.
You need to include wp-load.php in your file....
Just put this in top of your file.
require_once( dirname(__FILE__) . '/wp-load.php' );
The problem is you are not including Wordpress, so you are not using it at all (and it isn't including its libraries).
Plus, this is unrelated to the question, but you have a weakness in your code :
Any users can define the recipient of the email.

Categories