Opening e-mail with PHP IMAP - php

A user over at w3schools forums assisted me with some code on using IMAP functions to check my mail inbox on a private server and do what i like with it, i created my own set of functions for posting the e-mail content to a MySQL table.
Could somebody help me find a solution to how can i open the e-mail inbox, check for e-mails in the inbox (There will be only one there because previous emails will be automatically deleted. Define the open e-mail message as $open_email_msg
Allow me to initiate my set of commands for posting the e-mail to a MySQL table, then delete the e-mail and close the inbox?
This is the code the person assisted me with:
<?php
$now = time(); // current time
$mailbox = '{192.168.150.11:143/imap/novalidate-cert}'; // see http://www.php.net/manual/en/function.imap-open.php
$mbox = imap_open($mailbox, 'username', 'password'); // log in to mail server
if (!$mbox)
echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true)); // remove the print_r for production use
else
{
$box = imap_check($mbox); // get the inbox
for ($imap_idx = 1; $imap_idx <= $box->Nmsgs; $imap_idx++) // loop through the messages
{
$headers = imap_headerinfo($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-headerinfo.php
$raw_headers = imap_fetchheader($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-fetchheader.php
$selected_headers = '';
$text_part = '';
$html_part = '';
$original_message = imap_body($mbox, $imap_idx); // save the copy of the entire thing, attachments and all
// build selected headers string
for ($ii = 0; $ii < count($headers->from); $ii++)
$selected_headers .= 'From: ' . $headers->from[$ii]->mailbox . '#' . $headers->from[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->to); $ii++)
$selected_headers .= 'To: ' . $headers->to[$ii]->mailbox . '#' . $headers->to[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->cc); $ii++)
$selected_headers .= 'Cc: ' . $headers->cc[$ii]->mailbox . '#' . $headers->cc[$ii]->host . "\n";
for ($ii = 0; $ii < count($headers->bcc); $ii++)
$selected_headers .= 'Bcc: ' . $headers->bcc[$ii]->mailbox . '#' . $headers->bcc[$ii]->host . "\n";
if (!empty($headers->date))
$selected_headers .= 'Date: ' . $headers->date . "\n";
if (!empty($headers->subject))
$selected_headers .= 'Subject: ' . $headers->subject . "\n";
// see below; getMsg uses global variables
getMsg($mbox, $imap_idx);
$text_part = $plainmsg; // text portion of the email
$html_part = $htmlmsg; // html portion of the email
// check for text portion first
$msg_text = trim(strip_tags($plainmsg

Try this code to read emails.
$username="yourusername#yourmailhost.com";
$password="yourPassword123!";
$hostname="{imap.hostinger.com:993/imap/ssl}INBOX";
$imap=imap_open($hostname,$username,$password) or die('Cannot connect: '.imap_last_error());
$message_count = imap_num_msg($imap);
echo "<b>$message_count messages</b><br>";
for ($i = 1; $i <= $message_count; ++$i){
$header = imap_header($imap, $i);
$body = imap_fetchbody($imap, $i, '2');
$prettydate = date("jS F Y", $header->udate);
if(isset($header->from[0]->personal)){
$personal = $header->from[0]->personal;
}else{
$personal = $header->from[0]->mailbox;
}
$subject=$header->Subject;
$email = "$personal <{$header->from[0]->mailbox}#{$header->from[0]->host}>";
echo "On $prettydate, $email said \"$body\".\n";
echo '<br><br>';
}
print_r(imap_errors());
imap_close($imap);

I believe this link will help you as I used this myself and this is properly working for me.
There you can register and download the code, using it is simple.
Or what you can do if you want to get the information of header only is:
$mbox = imap_open("{xyz#abc.com:995/pop3/ssl/novalidate-cert}INBOX", 'abc#xyz.com', 'pass')
or die("can't connect: " . imap_last_error());
$MC = imap_check($mbox);
$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
{$overview->subject}\n";
echo "<br>";
}

Related

how to make timer inside socket php

this is the issue, i write a php socket, it work as a daemon, every time, and i have a client side, the client connect but every 30 seconds client automaticaly disconnect, it is happening because socket do not send himself a line (ka();) every 30 seconds, the (ka();) line is readed by the client and keep a constant conection, i have tried making a function inside the socket, so every 30 seconds write (ka();), but it does not work, it work wrong, it show the (ka();) if the client send something to the socket, if client side do not send anything the socket do not sent the anything.
the socket work has a daemon CLI windows and the client side is just javascript, when the socket is running, can be accesed directly by navigator and read what client send, if the client do not send anything, the socket need write the (ka();) line, so it is readed by client side and keep conection and so indefinitely.
this is the socket script
error_reporting(1);
ini_set('display_errors', '1');
define('CON_IP', '127.0.0.1');
define('CON_PORT', 1000);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$max_clients = MAX_CLIENTS;
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, CON_IP, CON_PORT);
socket_listen($socket, $max_clients);
$clients = array(
'0' => array(
'socket' => $socket
)
);
echo "Server running....\n\n";
echo "Server Started on : " . date('Y-m-d H:i:s') . "\n";
echo "Listening on : " . CON_IP . " port " . CON_PORT . "\n";
echo ".... awaiting connections ...\n\n";
$session = (TRUE);
while ($session === TRUE) {
$read[0] = $socket;
for ($i = 1; $i < count($clients) + 1; ++$i) {
if ($clients[$i] != NULL) {
$read[$i + 1] = $clients[$i]['socket'];
}
}
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
if (in_array($socket, $read)) {
for ($i = 1; $i < $max_clients + 1; ++$i) {
if (!isset($clients[$i])) {
$clients[$i]['socket'] = socket_accept($socket);
socket_getpeername($clients[$i]['socket'], $RemoteAddr, $RemotePort);
$clients[$i]['ipadd'] = $RemoteAddr . ':' . $RemotePort;
echo 'Incoming connection from ' . $clients[$i]['ipadd'] . "]\r\n";
break;
} elseif ($i == $max_clients - 1) {
echo 'Clients Overload!' . "\r\n";
}
if ($ready < 1) {
continue;
}
}
}
for ($i = 1; $i < $max_clients + 1; ++$i) {
if (in_array($clients[$i]['socket'], $read)) {
if (!$client[$i]['iframe']) {
$data = #socket_read($clients[$i]['socket'], 1024, PHP_BINARY_READ);
}
if (substr($data, 0, 3) == 'GET' || substr($data, 0, 4) == 'POST' || substr($data, 0, 4) == 'OPTI') {
$clients[$i]['iframe'] = true;
echo 'Incoming connection from browser [' . $clients[$i]['ipadd'] . "]\r\n";
socket_write($clients[$i]['socket'], "HTTP/1.1 200 OK\r\n");
socket_write($clients[$i]['socket'], "Content-Type: text/html; charset=utf-8;\r\n");
socket_write($clients[$i]['socket'], "Cache-Control: private\r\n\r\n");
for ($z = 0; $z < 4096; $z++)
$Brk = ("\n");
$html = ('<!DOCTYPE html>' . $Brk);
$html .= ('<html>' . $Brk);
$html .= ('<head>' . $Brk);
$html .= ('</head>' . $Brk);
$html .= ('<body>' . $Brk);
$html .= ('<script>' . $Brk);
$html .= ('function recv(packets){}' . $Brk);
$html .= ('</script>' . $Brk);
$html .= ('</body>' . $Brk);
socket_write($clients[$i]['socket'], $html);
} else {
if (trim($data) != '' && strlen(str_replace(' ', '', trim($data))) > 1) {
$msgs = ($data);
}
}
if ($data === FALSE) {
echo 'Incoming disconnection of ' . $clients[$i]['ipadd'] . "]\r\n";
unset($clients[$i]);
continue;
}
$data = trim($data);
if (!empty($data)) {
//LOGS
if ($msgs) {
$ClientDumpLog = fopen('log/InputToSocket.txt', "a");
fwrite($ClientDumpLog, "$msgs\n");
fclose($ClientDumpLog);
}
//
for ($j = 1; $j < $max_clients + 1; ++$j) {
if (isset($clients[$j]['socket']) and ($msgs !== NULL)) {
echo ('[' . $clients[$i]['ipadd'] . '] OK!' . "\r\n");
socket_write($clients[$j]['socket'], '<script>' . $msgs . "</script>\r\n");
}
}
break;
}
}
}
}
EDIT:
this solution, i think it was, but no, this code block, to test, writed after this line:
for ($j = 1; $j < $max_clients + 1; ++$j) {
$last_time = time();
while(true) {
if(time()-$last_time > 10) {
echo ('[' . $clients[$i]['ipadd'] . '] KA!' . "\r\n");
socket_write($clients[$j]['socket'], "ka();\r\n");
$last_time = time();
}
}
when i put this code, when the socket start, it show every (chosed time) in cli (KA!), when i access by navigator it show the (ka();) line too, but when the client side send something it never show, is like the (ka) line or what the client send, but no both at the same time.
somebody know some function or solution to make it work?
You have to override the maximum execution time setting of php.ini. You should not change it within php.ini - you rather should change it on top of your code:
<?php
set_time_limit(0);
//... your fancy code here...

How to use templates or dynamic content for php email sending

PHPMailer is a good option to send email, and mail() function too, but the thing is that generating dynamic content for the email body, and subject are not the best.
for example I've created a php file with the body templates or a class with the same, but are so difficult to maintain.
What do you recomend for organizing that code?
Is there a Way to create email templates? (like twig).
How do you organize folders and files?
Is there any doc recommendation for that?
thanks for your help
It's no different than what you're already doing in PHP to generate dynamic HTML. Except that instead of sending the generated HTML output via your web server to a client UA, you're sending it to an email UA via an MTA.
20 years ago someone thought to invent a good templating engine to generate dynamic content (it was called PHP). It turns out it's still incredibly useful today.
Let's say you have a template file that looks like this for your email.
<table>
<?php foreach($rows as $row) { ?>
<tr>
<?php foreach($row as $column) { ?>
<td><?=$column?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Let's say have some templating system that renders these templates with perhaps something like this.
class Template {
protected $templateFile = "";
protected $templateVars = [];
public function __construct($templateFile, Array $templateVars= []) {
$this->templateVars = $templateVars;
$this->templateFile = $templateFile;
}
public function __toString() {
export($this->templateVars, EXTR_SKIP);
ob_start();
include $this->templateFile;
return ob_get_clean();
}
}
Now, you could expand upon this very simple abstraction of templating a bit further to include things like your email subject line, sender email address, etc...
class SendEmail {
public function __construct($to, $subject, $template, Array $data) {
$template = new Template($file, $data); // create the email template
$this->emailBody = (string) $template; // generate the content
$this->to = $to;
$this->subject = $subject;
}
public function send() {
// Send email using PHP mailer or whatever here
}
}
$tempalteContent = mysqli_query($conn, "select * from newsletter_template where name like '%$template%'");
if (mysqli_num_rows($tempalteContent) > 0) {
$validate = 0;
$messageFinal = '';
$message = '';
$row = mysqli_fetch_array($tempalteContent);
$template_id = $row['id'];
$productInc = $row['productsInc'];
$blogInc = $row['blogInc'];
$templateMsg = htmlspecialchars_decode($row['description']);
$divData = '';
$blogData = '';
if ($blogInc == 1 && $productInc == 1) {
/* * Product Query* */
$productQuery = mysqli_query($conn, "select * from newsletter_products where newsletter_tempalte_id = $template_id");
if (mysqli_num_rows($productQuery) > 0) {
$width = '';
$website = "http://demo.com/";
if (mysqli_num_rows($productQuery) > 2) {
$width = '100%';
} else if (mysqli_num_rows($productQuery) > 1) {
$width = '100%';
}
for ($i = 0; $i <= mysqli_num_rows($productQuery); $i++) {
$row[$i] = mysqli_fetch_array($productQuery);
$product_id[$i] = $row[$i]['product_id'];
$productDetails[$i] = mysqli_query($conn, "select * from products where product_id = $product_id[$i]");
if (mysqli_num_rows($productDetails[$i]) > 0) {
$rowproduct[$i] = mysqli_fetch_array($productDetails[$i]);
$productName[$i] = $rowproduct[$i]['product_name'];
$productImg[$i] = $website . $rowproduct[$i]['product_img'];
$productDesp[$i] = htmlspecialchars_decode($rowproduct[$i]['product_desp']);
$divData .= "<div style='width:" . $width . ";float:left;padding:7px;text-align:justify'><span style='width:100%;padding-bottom:5px;float:left'><img src='" . $productImg[$i] . "' width='30%' height='30%' style='max-width:100px;max-height:100px'></span><b style='vertical-align:bottom'>" . $productName[$i] . "</b><span style='font-size:12px;'>" . $productDesp[$i] . "</span></div>";
}
$validate++;
}
}
/* * Blog Query* */
$blogQuery = mysqli_query($conn, "select * from newsletter_blogs where newsletter_template_id = $template_id");
if (mysqli_num_rows($blogQuery) > 0) {
$width = '';
$website = "http://demo.com/";
if (mysqli_num_rows($blogQuery) > 2) {
$width = '100%';
} else if (mysqli_num_rows($blogQuery) > 1) {
$width = '100%';
}
for ($i = 0; $i <= mysqli_num_rows($blogQuery); $i++) {
$row[$i] = mysqli_fetch_array($blogQuery);
$blog_id[$i] = $row[$i]['blog_id'];
$blogDetails[$i] = mysqli_query($conn, "select * from blog where id = $blog_id[$i]");
if (mysqli_num_rows($blogDetails[$i]) > 0) {
$rowblog[$i] = mysqli_fetch_array($blogDetails[$i]);
$blogName[$i] = $rowblog[$i]['title'];
$blogImg[$i] = $website . $rowblog[$i]['img'];
$blogDesp[$i] = htmlspecialchars_decode($rowblog[$i]['desp']);
$blogData .= "<div style='width:100%;float:left;padding:7px;text-align:justify'><span style='width:100%;padding-bottom:5px;float:left'><img src='" . $blogImg[$i] . "' width='30%' height='30%' style='max-width:100px;max-height:100px'></span><b style='vertical-align:bottom'>" . $blogName[$i] . "</b><span style='font-size:12px;'>" . $blogDesp[$i] . "</span></div>";
}
$validate++;
}
}
$message = $templateMsg . $divData . $blogData;
} else if ($productInc == 1 && $blogInc == 0) {
$productQuery = mysqli_query($conn, "select * from newsletter_products where newsletter_tempalte_id = $template_id");
if (mysqli_num_rows($productQuery) > 0) {
$width = '';
$website = "http://demo.com/";
if (mysqli_num_rows($productQuery) > 2) {
$width = '100%';
} else if (mysqli_num_rows($productQuery) > 1) {
$width = '100%';
}
for ($i = 0; $i <= mysqli_num_rows($productQuery); $i++) {
$row[$i] = mysqli_fetch_array($productQuery);
$product_id[$i] = $row[$i]['product_id'];
$productDetails[$i] = mysqli_query($conn, "select * from products where product_id = $product_id[$i]");
if (mysqli_num_rows($productDetails[$i]) > 0) {
$rowproduct[$i] = mysqli_fetch_array($productDetails[$i]);
$productName[$i] = $rowproduct[$i]['product_name'];
$productImg[$i] = $website . $rowproduct[$i]['product_img'];
$productDesp[$i] = htmlspecialchars_decode($rowproduct[$i]['product_desp']);
$divData .= "<div style='width:" . $width . ";float:left;padding:7px;text-align:justify'><span style='width:100%;padding-bottom:5px;float:left'><img src='" . $productImg[$i] . "' width='30%' height='30%' style='max-width:100px;max-height:100px'></span><b style='vertical-align:bottom'>" . $productName[$i] . "</b><span style='font-size:12px;'>" . $productDesp[$i] . "</span></div>";
}
$validate++;
}
}
$message = $templateMsg . $divData;
} else if ($blogInc == 1 && $productInc == 0) {
$blogQuery = mysqli_query($conn, "select * from newsletter_blogs where newsletter_tempalte_id = $template_id");
if (mysqli_num_rows($blogQuery) > 0) {
$width = '';
$website = "http://demo.com/";
if (mysqli_num_rows($blogQuery) > 2) {
$width = '100%';
} else if (mysqli_num_rows($blogQuery) > 1) {
$width = '100%';
}
for ($i = 0; $i <= mysqli_num_rows($blogQuery); $i++) {
$row[$i] = mysqli_fetch_array($blogQuery);
$blog_id[$i] = $row[$i]['blog_id'];
$blogDetails[$i] = mysqli_query($conn, "select * from blog where id = $blog_id[$i]");
if (mysqli_num_rows($blogDetails[$i]) > 0) {
$rowblog[$i] = mysqli_fetch_array($blogDetails[$i]);
$blogName[$i] = $rowblog[$i]['title'];
$blogImg[$i] = $website . $rowblog[$i]['img'];
$blogDesp[$i] = htmlspecialchars_decode($rowblog[$i]['desp']);
$blogData .= "<div style='width:100%;float:left;padding:7px;text-align:justify'><span style='width:100%;padding-bottom:5px;float:left'><img src='" . $blogImg[$i] . "' width='30%' height='30%' style='max-width:100px;max-height:100px'></span><b style='vertical-align:bottom'>" . $blogName[$i] . "</b><span style='font-size:12px;'>" . $blogDesp[$i] . "</span></div>";
}
$validate++;
}
}
$message = $templateMsg . $blogData;
} else {
$message = $templateMsg;
}
$messageFinal = '<div style="width:100%">' . $message . '</div>';
echo $validate;
} else {
echo "Fail";
exit();
}
$subscribers = explode(',', $_POST['subscribers']);
for ($i = 0; $i < count($subscribers); $i++) {
$to = $subscribers[$i];
$subject = $template;
$from = 'demo#demo.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: Demo' . "\r\n" .
'Reply-To: Your Email Id' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<!DOCTYPE html><html><body>';
$message .= $messageFinal . $footer;
$message .= '</body></html>';
// Sending email
if (mail($to, $subject, $message, $headers)) {
$validate++;
} else {
$validate = 0;
}
}

Can't get this auto-email to work in php. Can you help me?

I'm trying to make this secret santa auto-email script, and it's really confusing me... It doesn't email properly (one person gets a TON of emails, no one else gets any...)
I tried to just change the length on the array in the for loops, did I do it right?
I use the shuffle function and the email function, which seem to work well (aside from the for loop on the email function - did I do that right?). It's the main function I need help on.
Here's the code:
<?php
function twodshuffle($array)
{
// Get array length
$count = count($array);
// Create a range of indicies
$indi = range(1,$count);
// Randomize indicies array
shuffle($indi);
// Initialize new array
$newarray = array($count);
// Holds current index
$i = 0;
// Shuffle multidimensional array
foreach ($indi as $index)
{
$newarray[$i] = $array[$index];
$i++;
}
return $newarray;
}
function email($Name, $Email, $Likes)
{
$to = $Email;
$subject = '[Secret Santa] Happy Lolidays from Santabot 2020! Your match is here!';
$message = "HEY! You are " . $Name ."'s Secret Santa. Isn't that exciting? \r\n\r\nThis is what your match filled in for likes/dislikes: \r\n----------------------------\r\n" . $Likes . "\r\n----------------------------\r\n\r\n\r\nNow get them something nice and thoughtful. We will be swapping sometime around Christmas, so you have a lot of time to put some thought in it.\r\nLet's keep gifts around the $30 mark.\r\n\r\nHappy Lolidays,\r\nDCo's Santabot 2020.\r\n\r\nPS: I have set this up so not even I know the matches nor is a list stored anywhere. DO NOT delete this email if you are at risk of forgetting your match.";
$headers = 'From: Santabot 2020 <santabot2020#mydomain.com>' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
sleep(1000);
}
$con = mysql_connect("mysql.myserver.com", "username", "password") or die(mysql_error());
mysql_select_db("lolidays_dco", $con) or die(mysql_error());
$array;
//I just changed this array size now. Is this the right syntax?
for ($i = 1; $i <= array.length() -1; $i++) {
$sql = "SELECT * FROM tbl_data WHERE Number = " . $i;
$result = mysql_query($sql);
$count=mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$Name = $row['Name'];
$Email = $row['Email'];
$Likes = $row['Likes'];
}
$array[$i] = array("Name" => $Name, "Email" => $Email, "Likes" => $Likes);
}
$array = twodshuffle($array);
$string;
//changed this array length as well, is this the right syntax?
for ($i = 0; $i <= array.length(); $i++) {
if ($i == 2) {
$n = $array['0']["Name"];
$e = $array[$i]["Email"];
$l = $array['0']["Likes"];
email($n, $e, $l);
echo "Email Sent!<br />";
$string = $string . $array[$i]["Name"] . " => " . $array['0']["Name"] . "\r\n";
email('Matches', 'backup#email.com', $string);
}
else {
$j = $i + 1;
$n = $array[$j]["Name"];
$e = $array[$i]["Email"];
$l = $array[$j]["Likes"];
email($n, $e, $l);
echo "Email Sent!<br />";
$string = $string . $array[$i]["Name"] . " => " . $array[$j]["Name"] . "\r\n";
}
}
echo "<strong>Done!</strong>";
?>
You've hard-coded the recipient:
$n = $array['0']["Name"]; <---should be $i, not '0'
$e = $array[$i]["Email"];
$l = $array['0']["Likes"]; <---should be $i, not '0'

Email forwarding in PHP

My following code is tested by my friends. They said its working fine. But I am unable to use it. Can anybody find where the error is.
`
<?php
try
{
// Change to your mail server
$host = "pop.gmail.com";
// Connecting to POP3 email server.
$connection = imap_open("{" . $host . ":995/pop3/notls}", 'someusername#gmail.com', 'somepassword');
// Total number of messages in Inbox
$count = imap_num_msg($connection);
echo $count . " messages found<br />";
// Read Messages in Loop, Forward it to Actual User email and than delete it from current email account.
for ($i = 1; $i <= $count; $i++) {
$headers = imap_headerinfo($connection, $i);
$subject = $headers->subject;
$from = $headers->from[0]->mailbox . '#' . $headers->from[0]->host;
if ($headers->cc[0]->mailbox)
$cc = $headers->cc[0]->mailbox . '#' . $headers->cc[0]->host;
$subject = $headers->subject;
$structure = imap_fetchstructure($connection, $i);
$type = $this->get_mime_type($structure);
// GET HTML BODY
$body = $this->get_part($connection, $i, "");
//$raw_body = imap_body($connection, $i);
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($e = 0; $e < count($structure->parts); $e++) {
$attachments[$e] = array('is_attachment' => false, 'filename' => '', 'name' => '', 'attachment' => '');
if ($structure->parts[$e]->ifdparameters) {
foreach ($structure->parts[$e]->dparameters as $object) {
if (strtolower($object->attribute) == 'filename') {
$attachments[$e]['is_attachment'] = true;
$attachments[$e]['filename'] = $object->value;
} //if (strtolower($object->attribute) == 'filename')
} //foreach ($structure->parts[$e]->dparameters as $object)
} //if ($structure->parts[$e]->ifdparameters)
if ($structure->parts[$e]->ifparameters) {
foreach ($structure->parts[$e]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$e]['is_attachment'] = true;
$attachments[$e]['name'] = $object->value;
} //if (strtolower($object->attribute) == 'name')
} //foreach ($structure->parts[$e]->parameters as $object)
} //if ($structure->parts[$e]->ifparameters)
if ($attachments[$e]['is_attachment']) {
$attachments[$e]['attachment'] = #imap_fetchbody($connection, $i, $e + 1);
if ($structure->parts[$e]->encoding == 3) {
// 3 = BASE64
$attachments[$e]['attachment'] = base64_decode($attachments[$e]['attachment']);
} //if ($structure->parts[$e]->encoding == 3)
elseif ($structure->parts[$e]->encoding == 4) {
// 4 = QUOTED-PRINTABLE
$attachments[$e]['attachment'] = quoted_printable_decode($attachments[$e]['attachment']);
} //elseif ($structure->parts[$e]->encoding == 4)
} //if ($attachments[$e]['is_attachment'])
if ($attachments[$e]['is_attachment']) {
$filename = $attachments[$e]['filename'];
$filename = $attachments[$e]['name'];
$filecontent = $attachments[$e]['attachment'];
} //if ($attachments[$e]['is_attachment'])
} //for ($e = 0; $e < count($structure->parts); $e++)
} //if (isset($structure->parts) && count($structure->parts))
/**** ****/
/*echo "<pre>";
echo "From: " . $headers->Unseen . "<br />";
echo "From: " . $from . "<br />";
echo "Cc: " . $cc . "<br />";
echo "Subject: " . $subject . "<br />";
echo "Content Type: " . $type . "<br />";
echo "Body: " . $body . "<br />";*/
$mail = new Zend_Mail();
$mail->settype(Zend_Mime::MULTIPART_MIXED);
for ($k = 0; $k < count($attachments); $k++) {
$filename = $attachments[$k]['name'];
$filecontent = $attachments[$k]['attachment'];
if ($filename && $filecontent) {
$file = $mail->createAttachment($filecontent);
$file->filename = $filename;
} //if ($filename && $filecontent)
} //for ($k = 0; $k < count($attachments); $k++)
$mail->setFrom($from);
$mail->addTo('testmail#softmail.me');
if ($cc)
$mail->addCc($cc);
$mail->setSubject($subject);
$mail->setBodyHtml($body);
$mail->send();
// Mark the email messages once read
//imap_delete($mbox, 1);
} //for ($i = 1; $i <= $count; $i++)
// Delete all marked message from current email account.
imap_expunge($mbox);
echo 'If you see this, the number is 1 or below';
}
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
Please check if you have enabled
extension=php_openssl.dll;
ext in you php.ini. I had faced this problem once and enabling the above extension worked for me.

PHP - IRC Private Message Function Help

For some reason my bot wont private message a % of people on the IRC Channel. Here is my script:
<?php
$ircServer = "///";
$ircPort = "6667";
$ircChannel = "#bots";
set_time_limit(0);
$msg = $_POST['message'];
$pr = $_POST['percentage'];
$pr /= 100;
$ircSocket = fsockopen($ircServer, $ircPort, $eN, $eS);
if ($ircSocket)
{
fwrite($ircSocket, "USER Lost rawr.test lol :code\n");
fwrite($ircSocket, "NICK Rawr" . rand() . "\n");
fwrite($ircSocket, "JOIN " . $ircChannel . "\n");
while(1)
{
while($data = fgets($ircSocket, 128))
{
echo nl2br($data);
flush();
// Separate all data
$exData = explode(' ', $data);
// Send PONG back to the server
if($exData[0] == "PING")
{
fwrite($ircSocket, "PONG ".$exData[1]."\n");
}
}
echo $eS . ": " . $eN;
}
shuffle($users);
$size = count($users);
$target = $size * $pr;
$target = $round($target);
for ($i = 0; $i <= $target; $i++) {
fwrite($ircSocket, "PRIVMSG " . $users[$i] . " :" . $msg . "\n");
}
}
?>
Here is the log on what I recieve:
:StatServ!stats#Mazzzzz.com PRIVMSG Rawr30566 :VERSION
I have even tried removing the Post data and replaced this part with this:
$msg = $_POST['message'];
With
$msg = hello;
The other people on the channel does not get a private message.
Is this your entire script? $users isn't set to anything; you probably meant to set it to an array of usernames. $round also isn't set; you probably meant to just call the built-in round() function. If you add a debugging line in the for loop you can at least tell which users (if any) should be getting messages:
for ($i = 0; $i <= $target; $i++) {
echo "Sending message to ${users[$i]}\n";
fwrite($ircSocket, "PRIVMSG " . $users[$i] . " :" . $msg . "\n");
}
The IRC protocol stuff looks right. The receive log you were worried about doesn't have anything to do with it; StatServ on the IRC server is sending your bot a CTCP VERSION request. Normally clients respond with their name and version, and StatServ probably logs it so opers can see what clients are common on the network

Categories