Parse Postmark Bounce hook in PHP - php

I am using Postmark to send emails.
But postmark allows you to set a URL to process bounced emails.
I want to use this, but don't know how to get and handle the data.
My API works but I don't know how to retrieve the data postmark sends to my API.
<?php
class BackendBillingAPI
{
public static function postmarkBounceHook()
{
$log = new SpoonLog('custom', PATH_WWW . '/backend/cache/logs/billing');
// logging when we are in debugmode
if(SPOON_DEBUG) $log->write('Billing post (' . serialize($_POST) . ') triggered.');
if(SPOON_DEBUG) $log->write('Billing get (' . serialize($_GET) . ') triggered.');
if(SPOON_DEBUG) $log->write('Billing _REQUEST (' . serialize($_REQUEST) . ') triggered.');
}
}
Any thoughts/ideas?

You'd need to parse the json data inside the POST, and you can't rely on _POST apparently (since this is not a multipart form, see this for more info)
Here's a simple code that grabs a few parameters from the postmark bounce and generates an email. You can grab the parameters and do whatever else you need to of course
<?php
$form_data = json_decode(file_get_contents("php://input"));
// If your form data has an 'Email Address' field, here's how you extract it:
$email_address = $form_data->Email;
$details = $form_data->Details;
$type = $form_data->Type;
// Assemble the body of the email...
$message_body = <<<EOM
Bounced Email Address: $email_address
Details: $details
Type: $type
EOM;
if ($email_address) {
mail('ENTER YOUR EMAIL ADDRESS',
'Email bounced!',
$message_body);
}
?>

Related

iphone messages truncating php mailer email

I have a website that uses php mailer to send form input to a phone using the Verizon service. The issue is that the online form is being sent to a phone via text using the PHP mailer.
On the Android phones, the email is received properly and displayed in its entirety as a text message.
On an iPhone, the email is received as a text message, but it's truncated and only a portion is displayed. Notice that it stops at 'Cu.' It supposed to have about eight more lines of information pertaining to the appointment schedule. I'm wondering if iPhone has a character limit shorter than Android phones.
Anyone know how to avoid the text from being truncated on the iPhone?
I've googled for information, but all I get is how to fix the native iPhone email client. Which, isn't the issue.
Thank you.
It's pretty straight forward. The code pulls the form variables and assigns them as variable variables to the $message variable in a foreach loop.
<pre>
if ($OK_customers && $OK_appointments && $OK_customers_appointment) {
$message = '';
$selected_appt_day = date('l', strtotime($appt_date));
$selected_appt_date = date('m-d-Y', strtotime($appt_date));
$message .= "\r\n: ".$selected_appt_day.", ".$selected_appt_date."\r\n\r\n";
$message .= "Please confirm:\r\n\r\n";
foreach ($expected as $item) {
if (isset(${$item}) && !empty(${$item})) {
$val = ${$item};
}else {
$val = 'Not Selected';
}
if (is_array($val)) {
$val = implode(', ', $val);
}
$item = str_replace(['_', '-'], ' ', $item);
$message .= ucfirst($item) . ": $val\r\n";
}
$message = wordwrap($message, 70);
$mailSent = mail($to, $subject, $message, $headers);
if (!$mailSent) {
$errors['mailfail'] = true;
}
}
</pre>

Parse piped email with PHP

I have been trying to setup up a system with this functionality:
Send email to my server
Server pipes email to php script
Php script parses email getting subject and body
Get phone number from subject of email and send text with email body
I have found an email parser here: https://github.com/daniele-occhipinti/php-email-parser
I think I have setup it up correctly but I do not know how to test it besides just sending an email. But I cannot see what my script echos at that point. Also when I do send the email I know something is not working because the text does not send via twilio. What am I doing wrong?
Here is the code:
#!/usr/bin/php -q
<?php
require_once '../resources/Twilio/autoload.php';
use Twilio\Rest\Client;
require('config.php');
// Retrieve Email
require_once("../resources/PlancakeEmailParser.php");
$email = "php://stdin";
$emailParser = new PlancakeEmailParser(file_get_contents($email));
$subject = $emailParser->getSubject();
$text = $emailParser->getPlainBody();
$number = preg_replace('/[^0-9]/', '', $subject);
$phone = "+".$number;
// After this I send the message via Twilio
You are piping the input so you probably need to buffer the stream to ensure you have received all the data before you try and process it.
Something like this:
$dataIn = fopen('php://stdin', 'r');
if ($dataIn) {
$email = '';
while($line = fgets($dataIn)) {
$email .= $line;
}
fclose($dataIn);
}

Why is this script sending blank emails without using template in SugarCRM?

This is my code to send emails in SugarCRM CE 6.5.x version,
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid EntryPoint');
require_once('include/SugarPHPMailer.php');
$msg = new SugarPHPMailer();
$emailObj = new Email();
$defaults = $emailObj->getSystemDefaultEmail();
//Setup template
require_once('XTemplate/xtpl.php');
$xtpl = new XTemplate('custom/modules/Users/ChangePassword.html');
//assign recipients email and name
$rcpt_email = 'sohan.tirpude#mphasis.com';
$rcpt_name = 'Sohan';
//Set RECIPIENT's address
$email = $msg->AddAddress($rcpt_email, $rcpt_name);
//Send msg to users
$template_name = 'User';
//Assign values to variables in template
$xtpl->assign('EMAIL_NAME', $rcpt_name);
$xtpl->parse();
//$body = "Hello Sohan, It's been 80 days that you haven't changed your password. Please take some time out to change password first before it expires.";
$msg->From = $defaults['email'];
$msg->FromName = $defaults['name'];
$msg->Body = from_html(trim($xtpl->text($template_name)));
//$msg->Body = $body;
$msg->Subject = 'Change Password Request';
$msg->prepForOutbound();
$msg->AddAddress($email);
$msg->setMailerForSystem();
//Send the message, log if error occurs
if (!$msg->Send()){
$GLOBALS['log']->fatal('Error sending e-mail: ' . $msg->ErrorInfo);
}
?>
Now this script send blanks emails, and when I hardcoded body part then it is takes those message written in body, and sends an email. So, please help me here.

Save variable as variable in MYSQL for later use

I have an email template which i am saving in database. My problem is some part of message are variable means these data are coming from current user data.
For Example My Template is
$message="This is test for $username. I am sending mail to $email."
here $username and $email is coming from current users and it is varying from user to user.
So problem is how to save it in database so i can use it as variable on php page later.
anybody have any idea please help me.your help would be appreciated.
If you really need to store whole template in database, you can save it using your own created constants e.g. [USERNAME], [EMAIL] and then in php script just use str_replace() on them.
$messageTemplate = 'This is test for [USERNAME]. I am sending mail to [EMAIL].';
$message = str_replace(array('[USERNAME]', '[EMAIL]'), array($username, $email), $messageTemplate);
But you can also divide this string and concatenate it with variables from database as follows:
$message = 'This is test for ' . $username . '. I am sending mail to ' . $email . '.';
You can use something like this:
$input = "This is test for {username}. I am sending mail to {email}.";
$tokens = array("username" => $username, "email" => $email);
$tmp = $input;
foreach($tokens as $key => $token)
{
$tmp = str_replace("{".$key."}", $token, $tmp);
}
echo $tmp;
The variables in the string will not be evaluated as variables automatically just because you are adding it to your php scope. You need to eval the string in order for the variables to be replaced:
$username = 'test';
$email = 'test#test.com';
$str = "This is a test for $username. I am sending mail to some person $email.";
echo $str. "\n";
// This is a test for $username. I am sending mail to some person $email.
eval("\$str = \"$str\";");
echo $str. "\n";
// This is a test for test. I am sending mail to some person test#test.com.
For more information, see http://php.net/manual/en/function.eval.php

send email to multiple recepients

I'm trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need to have them query my database and send the e-mail to each e-mail address. Where am I going wrong here?
$elist = $database->getRows("SELECT * FROM `emails`");
if ($elist) {
foreach ($elist as $elist_result) {
$frm = 'rdsyh#gmail.com';
$sub = 'Weekly Work Report';
ob_start(); // start output buffering
include_once('mail_content.php');
$mail_body = ob_get_contents(); // get the contents from the buffer
ob_end_clean();
$to = $elist_result['email'];
$mailstatus = l_mail('', '', $to, $elist_result['firstname'] . ' ' . $elist_result['lastname'], $frm, 'HR', $sub, $mail_body);
}
}
if ($mailstatus == 'ok') {
echo '<center><font color=red style="font-size:14px">Message has been sent Succesfully.....!</font></center><br>';
} else {
echo $mailstatus;
}
Well, there's a lot of abstraction here that we know nothing about from your code. Things to check:
Are you certain that your database query is returning all of the results you're looking for (is $elist populated properly)?
Are you certain that the query is returning data in the format that you're trying to access it in (is $to populated properly)?
Are you certain your l_mail() function is behaving (is it possible it exit's or otherwise terminates script execution in the middle of the first pass)?
Based on what I see here, if everything else was working properly, you should successfully be sending a bunch of emails, one to each email in your list.
Now, if instead you're trying to send a single email that is sent to all of the addresses at once, then you need to group the email addresses in the for loop and then run your mail function afterwards:
<?
$tos = array();
foreach ($elist as $elist_result) {
$tos[] = $elist_result['email'];
}
$frm = 'rdsyh#gmail.com';
$sub = 'Weekly Work Report';
ob_start(); // start output buffering
include_once('mail_content.php');
$mail_body = ob_get_contents(); // get the contents from the buffer
ob_end_clean();
$to = implode(', ', $tos);
$mailstatus = l_mail('', '', $to, $elist_result['firstname'] . ' ' . $elist_result['lastname'], $frm, 'HR', $sub, $mail_body);
?>
What does l_mail() do? If its a web service, then it might have limit for mass emails.

Categories