Getting Mail body In PHP - php

What I am doing:
First I am trying to get email and save the reply of user in my database.
For this I am using PHP imap method to get mail from my email address.
Code:
function get_email()
{
//define server, email , password
$server = '{server.example.com:143}INBOX';
$user_name = 'login';
$user_pass = 'password';
$mail = imap_open( $server , $user_name , $user_pass );
$headers = imap_num_msg($mail);//counting the no of msg
for ($i = 1; $i <= $headers ; $i++)
{
$body = imap_fetchbody($mail, $i ,1);
$body = explode(">",$body);
$body = $body['0'];
$body = substr($body, 0, -5);
echo "<pre>";
echo "/-/-/-/-/-/".$body."/-/-/-/-/-/";
echo "</pre>";
}
// close the connection
imap_close($mail);
}
Now:
The Result that i am getting from $body = imap_fetchbody($mail, $i ,$i); is this:
Okay What new in it.
On Thu, Feb 12, 2015 at 4:56 PM, admin wrote:
> My name is admin!
>
>
but only wanted a the msg body that Okay What new in it. . So I explode() the massage and get the first element of array and remove the last line.
Problem:
I could not remove last line the current result of this program Is:
Okay What new in it.
On Thu, Feb 12, 2015 at 4:56 PM, admin
Even I have add "/-/-/-/-/-/" symbol but the symbol are not showing.

Your approach is not ideal, and this answer will not be ideal either. Because frankly what you are trying to accomplish can be quite difficult, because previous messages in a thread on emails will be different depending on client used and you cannot trust the user either (the thread below the 'body' could have been altered by the user etc.)
Using explode by '>' is not ideal, because what if it is part of the message?
exploding by \n> is a no go too because a user might inline responses as #Michael_Berkovski said.
Anyways here is my solution:
$body = preg_replace('#(\n>.*?)+$#','',$body);
This will reduce the following:
This is the body
> And this is a comment within the body
Also just the body
>Previous messages
>here
>> Even indented who cares?
to:
This is the body
> And this is a comment within the body
Also just the body

For trying very hard I come to a solution that ask user to reply with '#' at the end of the massage. As I notice there are different types of replies from different mailing services but all services show reply first.
Code:
$msgno = imap_num_msg($mail);
for ($i = 1; $i <= $msgno ; $i++)
{
$body = imap_fetchbody($mail, $i ,1);
$body = explode("#",$body);
$body = $body['0'];
echo $body;
echo "<br>";
}
From this I got my desire result.

Related

Few chars getting replaced with '=' in mail content in wp_mail

Whenever mails are triggered from wordpress site, a few alphabets are randomly getting replaced with '=' in the mail content. I have set the charset and content type in headers even then this weird error is coming. What could be the possible fix to this ?
Below is my code to trigger mails from wordpress site:
$footerText = "<br/><br/>
Regards,<br/>
ABC<br/><br/>
Note: This is an automated mail. Please do not reply to this message";
$post = get_post($postId);
$post_date = strtotime($post->post_date);
$author_email = get_the_author_meta( 'user_email', $post->post_author);
$headers = array();
$headers[] = 'Content-type: text/html; charset=UTF-8';
$headers[] = 'From: '.FROM_EMAIL;
//$headers[] = 'Bcc: '.$author_email;
$subject = "Request to share your expertise on - '".$post->post_title."'";
$post_title = $post->post_title;
$post_content = $post->post_content;
$post_url = get_permalink($post->ID);
$mail_message = "Your expertise would help solve complex business problems that would
help our associates solve our client problems faster.
Request you to share your expertise on the following post,
which has not been answered for over ".$days." days now.<br/><br/>
Post: <strong>".$post_title."</strong><br/>
Description: ".$post_content."<br/><br/>
Click <a href='".$post_url."'>here</a> to respond to the post.<br/><br/>
Thanks You!
".$footerText;
$hello_text = "Dear Expert,<br /><br />";
$full_message = $hello_text.$mail_message;
wp_mail('abc#gmail.com',$subject,$full_message,$headers);
Emails that i receive using this code is as follows:
Dear Expert,
Your expertise would help solve complex business p=oblems that would help our associates solve our Cli=nt(s) problems faster. Request you to share your experti=e on the following post, which has not been answered for o=er 8 days now.
Post: RFP for Business De=elopment,Functional Testing,Technology Expert,Perfecto,Healthcare,Medical =anagement,Mobile,Digital,North America This is Dynamic content, retrieved from database
Description: Customer is asking f=r RFQ to develop a new mobile app for care management application in AHM. =HM is a subsidary of Aetna Inc. This is Dynamic content, retrieved from database
Click here to respond to the post.
Thank you!
Regards, ABC
Note: This is an automated mail.=lease do not reply to this message
Totally confused on why random letters are getting replaced with '='.Kindly point out and suggest what is wrong with this
Commenting couple of return statements and replacing them as suggested in the below link on wordpress.org worked for me, now the mails are sent properly and the equal sign '=' issue is solved.
Fixed by making following changes in wp-includes\class-phpmailer.php
public function encodeQP($string, $line_max = 76)
{
// Use native function if it's available (>= PHP5.3)
if (function_exists('quoted_printable_encode')) {
//return $this->fixEOL(quoted_printable_encode($string)); commented this one
return quoted_printable_encode($string); // added this line
}
// Fall back to a pure PHP implementation
$string = str_replace(
array('%20', '%0D%0A.', '%0D%0A', '%'),
array(' ', "\r\n=2E", "\r\n", '='),
rawurlencode($string)
);
$string = preg_replace('/[^\r\n]{' . ($line_max - 3) . '}[^=\r\n]{2}/', "$0=\r\n", $string);
//return $this->fixEOL($string); commented this one
return $string; // added this line
}
Read more here: https://core.trac.wordpress.org/ticket/33815

Assigning php variables from email body

I am having a hard time figuring out where to go next.
I have set up a pipe to program on my cpanel that takes emails and sends em to a php script I have.
The script takes the email, extracts the body, and assigns the entire body to a variable called $body. What I need to do next, is take the information in the body, and make them more php variables that I can sent to an API.
The email will ALWAYS be in the same format.
This is what I get when I ask the script to send me an email of the $body variable.
--14dae934062f9d9cee04d111829f
Content-Type: text/plain; charset=ISO-8859-1
*field_1* = Illinois
*field_2* = miguel
*field_3* = martinez
*field_4* = miguel41303427#sbcglobal.net
*field_5* = 2305250033
*field_6* = streamwood
*field_7* = il
*field_8* = 2001
*field_9* = BMW
*field_10* = 325i
*field_11* = 129000
--14dae934062f9d9cee04d111829f
Content-Type: text/html; charset=ISO-8859-1
<p class="MsoNormal"><b>field_1</b> = Illinois<br>
<b>field_2</b> = miguel<br>
<b>field_3</b> = martinez<br>
<b>field_4</b> = miguel41307#sbcglobal.net<br>
<b>field_5</b> = 2305250033<br>
<b>field_6</b> = streamwood<br>
<b>field_7</b> = il<br>
<b>field_8</b> = 2001<br>
<b>field_9</b> = BMW<br>
<b>field_10</b> = 325i<br>
<b>field_11</b> = 129000<br></p>
--14dae934062f9d9cee04d111829f--
Is there a way to take the value of field_1, in this case Illinois, and assign it to say $state
It can be done with a regex. Try something like that :
if(preg_match('/<b>field\_1<\/b>(.*)<br>/', $body, $matches))
$field1 = $matches[1];
}

Foreign Characters in HTML Email

I wrote a script that takes HTML input from a web page and sends it out as an HTML email. For some reason there are foreign characters, usually exclamation points, showing up in the email that arrives in our inboxes that do not exist in the input. I've checked for hidden characters and there are none. The input is not being copied and pasted but directly entered. Here's a before and after example.
Input:
<p>Katherine Kemler, LSU flute professor, will perform at 7:30 p.m. Feb. 1 in Wattenbarger Auditorium. The performance is free and open to the public.</p>
Output: http://img42.com/LKEou
You can see the erroneous characters between Feb. and 1 and after Watt. I don't think it's the email client because it's the same in every client I've checked.
I've read some other questions around here that have led me to try things like using UTF-8 encoding instead of ISO-8859-1, using base64_encode() on the body, setting the content-transfer-encoding to 8bit but none of that has worked. I've read something about needing a line break after 998 characters but these exclamation points aren't showing up in way that suggests that to me. In my example above, one follows only ten characters after another. Here's my script:
<?php
if(!isset($_GET["id"])) return "<span class=\"text-error\">No ID specified.</span>"; //this should never happen
$id = $_GET["id"];
$ok = false;
$students = //redacted
$facstaff = //redacted
$studentsBCC = //redacted
$facstaffBCC = //redacted
$subject = "Tech Times for ".date("m/d");
$headers = "From: \"Tennessee Tech University\" <techtimes#tntech.edu>\r\n".
"Reply-to: no-reply#tntech.edu\r\n".
"MIME-Version: 1.0\r\n".
"Content-Transfer-Encoding: 8bit\r\n".
"Content-type: text/html; charset=UTF-8\r\n".//iso-8859-1\r\n".
"X-Mailer: PHP/".phpversion();
$resource = $modx->getObject("modResource", $id);
if(is_null($resource)) return "<span class=\"text-error\">Failed to get Resource $id</span>"; //this should never happen either
//get the template and insert the content
$body = $modx->getChunk("techtimes", array("copy"=>$resource->getContent(), "headerimg"=>$resource->getTVValue("headerImg")));
//check to see if this is a test run and if so reassign destination email address and empty BCCs
if(isset($_GET["email"])){
$facstaff = $_GET["email"];
$students = $_GET["email"];
$studentsBCC = "";
$facstaffBCC = "";
}
switch($id){
case 50: $ok = mail($facstaff,$subject,$body,$headers."\r\nBcc:".$facstaffBCC); break;
case 51: $ok = mail($students,$subject,$body,$headers."\r\nBcc:".$studentsBCC); break;
default: return "<span class=\"text-error\">Invalid or no ID specified for Tech Times.</span>"; //this, too, should never happen
}
if($ok){
$output = "<span class=\"text-success\">Tech Times for <strong>".(($id == 50) ? "Fac/Staff" : "students")." ($id)</strong> has been sent to <strong>".(($id == 50) ? $facstaff : $students)."</strong></span>.";
}else{ //this could happen if something goes wrong with the mailer
$error = error_get_last();
var_dump($error);
$output = "<span class=\"text-error\">Failed to send Tech Times!</span>";
}
return $output;
?>
I'm using MODx Revolution to format the output. I'm stumped.
Help?
Thanks!

Sending SMS using PHP API with unicode message

I trying to send SMS via RoutoMessaging PHP API. I readed all documentation and examples what i was able to find.
They have PHP example script for sending SMS in unicode format:
<?php
// include RoutoSMS class
include("RoutoTelecomSMS.php");
// creating object
$sms = new RoutoTelecomSMS;
// setting login parameters
$sms->SetUssms->SetOwnNum("44792838383838");
$sms->SetType("unicode");
// get values entered from FORM
$sms->SetNumber($number);
$message="04220432043E04580435002004370435043B0435043D04350020043E0
44704380020044104430020043C04380020043F0430043C043504420020043F043E
043C044304420438043B0435002E002E002E";
$sms->SetMessage($message);
// send SMS and print result
$smsresult = $sms->Send();
print $smsresult;
?>
What i not understand is how i can transform text from submited string to this code needed for including in $message.
Can anyone suggest function to convert text for $message please?
I currently work with PHP version 5.3.3.
The message you are sending is a Cryllic text. It's probably in Serbian. It reads as "Твоје зелене очи су ми памет помутиле..."
Decoding
header('Content-Type: text/html; charset=utf-8');
$str = "04220432043E04580435002004370435043B0435043D04350020043E044704380020044104430020043C04380020043F0430043C043504420020043F043E043C044304420438043B0435002E002E002E";
foreach(str_split($str, 4) as $char) echo "&#x{$char};";
And this is how you would encode the message
$string = "Твоје зелене очи су ми памет помутиле...";
$string = mb_convert_encoding($string, 'UCS-2', 'utf8');
for($i =0; $i < strlen($string); $i++)
echo strtoupper(bin2hex($string[$i]));

Microsoft outlook and PHP email fail to be a valid email it goes straight to the junk box if i change email contents to hello world works. rubish err

Nightmare Episode 1
I put 21 hours today to solve this. But i failed, asking experts to have a look, checked almost PEAR all possibilities but this Microsoft Outlook never gives up. What is this secret ?
Microsoft Outlook 2010 as receives as junk email when i put junk filter to "High". If i send another email with my same account from Google it goes to Inbox.
What is the problem with this Outlook ? I tried to follow multipart/alternative or multipart/mixed or multipart/relative but all Same.
My server log shows: 100% ok no spam not blacklist, all clear
Dec 8 15:42:30 www postfix/smtp[15250]: C99908162: to=, relay=mail.andmylab.com[01.01.01.01]:25, delay=0.25, delays=0.07/0.01/0.08/0.09, dsn=2.0.0, status=sent (250 OK id=1PQQqL-0001b6-TA)
My blacklist: www.whatismyipaddress.com shows no black list all green
- I can send to Google or to my own domain and other domain without any problems.
My code: its generating exactly multipart/alternative where i am following RFC standards
/* DB details */
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/cloude.ini', 'production');
define("DBHOST", $config->resources->db->params->host);
define("DBUSER", $config->resources->db->params->username);
define("DBPASS", $config->resources->db->params->password);
define("DEFAULT_DB", $config->resources->db->params->dbname);
define("MAILER_TABLE", "mail_queue");
/* 1 --------------------- PEAR!! */
require_once "Mail/Queue.php";
require_once 'Mail/mime.php';
require_once 'Mail/mimePart.php';
/* 2 --------------------- DB */
$db_options['type'] = 'db';
$db_options['dsn'] = 'mysql://' . DBUSER . ":" . DBPASS . "#" . DBHOST . "/" . DEFAULT_DB;
$db_options['mail_table'] = MAILER_TABLE;
$mail_options['driver'] = 'mail';
/* Step ------------------ 1 */
$mail_queue =& new Mail_Queue($db_options, $mail_options);
$from = "validguy#lul.be";
/* Step ------------------ 2 */
$mime =& new Mail_mime($crlf = "\n");
$mail =& Mail::factory($mail_options['driver']);
/* Step ------------------ 3 Plain text and Html */
$data->mode = 'html';
if ($data->mode=='html')
{
/* A <--------------- part */
$params['content_type'] = 'multipart/alternative';
$email = new Mail_mimePart('', $params);
/* B <--------------- part */
$email = $email->encode();
$email['headers']['Mime-Version'] = '1.0';
$email['headers']['Subject'] = $fix;
$email['headers']['From'] = $from;
//Zend_Debug::dump($email);
// exit;
/* C <--------------- part */
$mime->setTXTBody('Test');
$mime->setHTMLBody($txt2);
/* D <--------------- part */
$body = $mime->get();
$hdrs = $mime->headers($email['headers']);
} else if($data->mode=='both') {
// later... for multipart/relative
} else {
// later... for inline
}
/* Step 4 - done */
$mailResult = $mail_queue->put($from, $row->email, $hdrs, $body, 0,true,$nres[0]['id']);
//$mailResult = $mail_queue->put($from, $row->email, $email['headers'], $email['body'], 0,true,$nres[0]['id']);
if(!PEAR::isError($mailResult)){ $m++; } else { $n++; }
}
}
/* Relax........ */
echo "Records transfered: " . $m . "<br/>";
echo "Records failed to transfer: " . $n . "<br/>";
Nightmare Episode 1 (FOLLOW UP)
SPF fix (zone file setting, required)
- Go to all SPF testing sites, and check what there wizard saying
Fix PTR (zone file setting, required)
Dkim proxy (zone file setting, required)
Prepare two version Plain text and Html
Check message headers from working emails that arrives without any problem to your inbox like (Google/Yahoo and others top companies), and compare that towards yours
Do not trust Microsoft Outlook 2010 or Old version, because in junk filter (high) its also written similar, it may not be a spam that must be a business issue, to make us stupid. And put us in nightmare to solve those issues.
If you don't agree in those TOP reasons, please advise and bit it, this is what i learned and trying to share, because its very annoying as a developer, if you don't have any answer for this RUBISH EROR, caused by Microsoft Outlook 2010.
First of all its Microsoft itself. To really overcome with this issue you can take action such as:
MailChimp: very nice, after pulling all my hair out why it does not work, i found that they got some nice templates with special secret inside, but any way, i just customized it, and it works, i can hit straight crap Outlook express "Inbox" even its high spam filtered.

Categories