How can I catch the information of a callback? - php

The code i am using to send messages works fine. I have configured the web-hooks correctly as the clickatell server is giving a 200 response which means my system received the callback but it is not catching the information of the callback. I am still new to this so What could i possibly be doing wrong.
clickatell library
CONTROLER--Send callback result via email(I am receiving the email but has no callback results)
public function callback(){
$this->load->library('clickatell_rest'); //Load Clickatell library
$this->clickatell_rest->parseStatusCallback(function ($result){
$callbackRunDate = date('Y-m-d H:i:s');
$from_email = 'admin#aaa.co.za';
$to = 'sebakets#gmail.com';
$message = $callbackRunDate.' '.print_r($result);
$subject = 'Callback Results';
$this->email->set_mailtype('html');
$this->email->from($from_email, 'Clickatell');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
});
}
Clickatell Library function
public static function parseStatusCallback($callback, $file = STDIN){
$body = file_get_contents($file);
$body = json_decode($body, true);
$keys = [
'apiKey',
'messageId',
'requestId',
'clientMessageId',
'to',
'from',
'status',
'statusDescription',
'timestamp'
];
if (!array_diff($keys, array_keys($body))) {
$callback($body);
}
return;
}

Related

Amazon SES RawMessage: Missing required header 'From'

use PHPMailer\PHPMailer\PHPMailer;
use Aws\Ses\SesClient;
use Aws\Ses\Exception\SesException;
require 'vendor/autoload.php';
if(!function_exists("sendmailalexraw")){
function sendmailalexraw($email,$subject,$messages,$definesender)
{
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender = $definesender;
$sendername = 'Alex';
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
$recipient = $email;
// Specify a configuration set.
$configset = 'ConfigSet';
// Replace us-west-2 with the AWS Region you're using for Amazon SES.
$region = 'eu-west-1';
$subject = $subject;
$htmlbody = <<<EOD
<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>Please see the attached file for a list of customers to contact.</p>
</body>
</html>
EOD;
$textbody = <<<EOD
Hello,
Please see the attached file for a list of customers to contact.
EOD;
//// The full path to the file that will be attached to the email.
$att = 'path/to/customers-to-contact.xlsx';
// Create an SesClient.
$client = SesClient::factory(array(
'version'=> 'latest',
'region' => $region
));
// Create a new PHPMailer object.
$mail = new PHPMailer;
// Add components to the email.
$mail->setFrom($sender, $sendername);
$mail->addAddress($recipient);
$mail->Subject = $subject;
$mail->Body = $htmlbody;
$mail->AltBody = $textbody;
$mail->addAttachment($att);
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configset);
// Attempt to assemble the above components into a MIME message.
if (!$mail->preSend()) {
echo $mail->ErrorInfo;
} else {
// Create a new variable that contains the MIME message.
$message = $mail->getSentMIMEMessage();
}
// Try to send the message.
try {
$result = $client->sendRawEmail([
'RawMessage' => [
'Data' => $messages
]
]);
// If the message was sent, show the message ID.
$messageId = $result->get('MessageId');
echo("Email sent! Message ID: $messageId"."\n");
} catch (SesException $error) {
// If the message was not sent, show a message explaining what went wrong.
echo("The email was not sent. Error message: "
.$error->getAwsErrorMessage()."\n");
}
}
}
$email='example#gmail.com';
$subject='abc';
$messages='xyz';
$definesender='info#verifieddomain.net';
sendmailalexraw($email,$subject,$messages,$definesender);
?>
I am trying to send RawMessage with Amazon SES but I get :
The email was not sent. Error message: Missing required header 'From'.
Sender I use it's verified, my Amazon SES it's active (out of sendbox ) .
I am needing to send as RAW Message to create unsubscribe option for emails I am sening. As I read from documentation it have to be raw email to be able to add this parameters.Thank you !
You have to base64 encode the message :
$result = $client->sendRawEmail([
'RawMessage' => [
'Data' => base64_encode($messages)
]
]);
Since you're using AWS Ses, you could do it this way:
define('SENDER', 'Your name<a#a.com>');
define('RECIPIENT', 'b#b.com');
define('CCLIST', 'c#c.com');
define('REGION','your region');
define('SUBJECT','Your subject goes here');
$bodytext = "<h2>Your body goes here.</h2>" . PHP_EOL;
$bodytext .= "<p>Append as many rows as you want</p>";
define('BODY',$bodytext);
$client = SesClient::factory(array(
'version'=> 'latest',
'region' => 'your region'
));
$request = array();
$request['Source'] = SENDER;
$request['Destination']['ToAddresses'] = array(RECIPIENT);
$request['Destination']['CcAddresses'] = array(CCLIST);
$request['Message']['Subject']['Data'] = SUBJECT;
$request['Message']['Body']['Html']['Data'] = BODY;
try {
$result = $client->sendEmail($request);
$messageId = $result->get('MessageId');
echo("Email successfully sent! Message ID: $messageId"."\n");
} catch (Exception $e) {
echo("The email was not sent. Error message: ");
echo($e->getMessage()."\n");
}

How to Loop recipient in email Codeigniter

Have tried send email to multiple recipients but only 1 data is sent:
function reminder(){
$recipients= $this->user_model->view();
var_dump($recipients[0]->email);
$emaill = $recipients->email;
$recipientsmail= $emaill.',';
$email = $recipientsmail;
$judul = 'Test Email';
$deskripsi = 'TESt Email';
$config = [...]; //config for email is OK
$this->load->library('email', $config);
$this->email->from('tes');
$this->email->to($email);
$this->email->subject($judul);
$this->email->message($deskripsi);
$this->email->send();
return TRUE;
}
is something wrong in my code?
Please Help Me
That's the way I use to send multiple emails in codeigniter. Instead of put all the email directions in a variable ($email), use a foreach to loop the array and follow the details in the code:
function reminder(){
$recipients= $this->user_model->view();
var_dump($recipients[0]->email);
$judul = 'Test Email';
$deskripsi = 'This is a test';
$emailuser = 'user123#gmial.com';//for example
$nameuser = 'name of the user';
$config = [...]; //config for email is OK
$this->load->library("email");
foreach ($recipients as $value) {
$this->email->initialize($config);
$this->email->from($emailuser, $nameuser);
$this->email->to($value->email);
$this->email->subject($judul);
$this->email->message($deskripsi);
if($this->email->send()){
$this->session->set_flashdata("email_sent","Email sent successfully.");
}else{
$this->session->set_flashdata("email_sent","Error in sending Email.");
}
}
return TRUE;
}
And with this you could send more than one email. I hope it helps you.

Codeigniter Send Separate Emails Simultaneously

Using Codeigniter 3, I have a web form that sends an email to the site admin once a form is submitted - this works as expected.
I am using email templates, once the form is submitted template 1 is sent (to the site admin). I would now like to simultaneously send template 2 (to the submitter's email address).
The emails will contain the same content apart from the email intro text, and subject - details below;
Email Template 1 - to admin;
'Hi, a new item has been requested on the site, ...'
Email Template 2 - to submitter;
'Hi, Here is the item you have requested on the site, ...'
My current code is as follows;
public function sendRequest() {
$this->load->library('email');
$from_email = $this->input->post('email');
$to_email = 'admin#example.com';
$subject = 'New Item Request - Admin Copy';
$name = $this->input->post('name');
$comment = $this->input->post('comment');
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject($subject);
$data = array(
'name' => $name,
'from' => $from_email,
'comment' => $comment,
);
// send email template 1
$this->email->message($this->load->view('template/email/item_request_template', $data, true));
// send email template 2 to submitter - how?
// change $subject to 'New Item Request - User Copy';
if($this->email->send()) {
// send the $data to my email template
$data = array(
'item_request_name' => $this->input->post('name'),
'item_request_email' => $this->input->post('email'),
'item_request_comment' => $this->input->post('comment'),
);
}
}
Is there a more efficient way of doing this?
You simply have to repeat all the steps required for sending an email in the first place. Only difference is that for the second call you need to call, and reset all the required config options.
public function sendRequest() {
$this->load->library('email');
$from_email = $this->input->post('email');
$to_email = 'admin#example.com';
$subject = 'New Item Request - Admin Copy';
$name = $this->input->post('name');
$comment = $this->input->post('comment');
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject($subject);
$data = array(
'name' => $name,
'from' => $from_email,
'comment' => $comment,
);
// send email template 1
$this->email->message($this->load->view('template/email/item_request_template', $data, true));
// send email template 2 to submitter - how?
// change $subject to 'New Item Request - User Copy';
if($this->email->send()) {
$this->email->clear(TRUE); // Pass TRUE as an argument if you are sending attachments
$this->email->from($from_email); // Update for second email
$this->email->to($to_email); // Update for second email
$this->email->subject($subject); // Update for second email
// send the $data to my email template
$data = array(
'item_request_name' => $this->input->post('name'),
'item_request_email' => $this->input->post('email'),
'item_request_comment' => $this->input->post('comment'),
);
$this->email->message($this->load->view('template/email/item_request_template_2', $data, true));
$this->email->send();
}
}

Sendgrid php send to multiple recipients

I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.
function sendEmail($subject, $to, $message) {
$from = new SendGrid\Email(null, "sample#email.com");
$subject = $subject;
$to = new SendGrid\Email(null, $to);
$content = new SendGrid\Content("text/html", $message);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = 'MY_KEY';
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
The SendGrid\Mail class supports adding multiple to addresses through the SendGrid\Personalization class.
You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each Sendgrid\Mail object, must have at least one Personalization.
Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
You can create a Mail object without this and later add your own Personalization.
In the end, this is how I have managed to do this and it's working good.
function sendEmail($subject, $to, $message, $cc)
{
$from = new SendGrid\Email(null, "sample#email.com");
$subject = $subject;
$to = new SendGrid\Email(null, $to);
$content = new SendGrid\Content("text/html", $message);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
foreach ($cc as $value) {
$to = new SendGrid\Email(null, $value);
$mail->personalization[0]->addCC($to);
}
$apiKey = 'MY_KEY';
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
If someone is still looking for an answer to how to add multiple emails in To, Cc, and Bcc, Using SendGrid, here is what helped me.
First, you need to add an associative array like this:
for multiple emails in to (recipients):
$tos = [
"example1#example.com" => "User 1",
"example1#example.com" => "User 2"
];
and in your SendMail class use this $email->addTos($tos); instead of $email->addTo;
Similarly, for multiple Cc use
$email->addCcs($cc);
and for Bcc
$email->addBccs($bcc);
Here is the link to see more details sendgrid php
function makeEmail($to_emails = array(),$from_email,$subject,$body) {
$from = new SendGrid\Email(null, $from_email);
$to = new SendGrid\Email(null, $to_emails[0]);
$content = new SendGrid\Content("text/plain", $body);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$to = new SendGrid\Email(null, $to_emails[1]);
$mail->personalization[0]->addTo($to);
return $mail;
}
function sendMail($to = array(),$from,$subject,$body) {
$apiKey = 'your api key';
$sg = new \SendGrid($apiKey);
$request_body = makeEmail($to ,$from,$subject,$body);
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());
}
$to = array('test1#example.com','test2#example.com');
$from = 'from#example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";
sendMail($to ,$from,$subject,$body);
Now Sendgrid provides an easy way for sending single mail to multiple recipients,
it provides Mail::addTos method in which we can add multiple mails to whom we want to send our mail,
we have to pass associative array of user emails and user names into addTos.
see below example:
$tos = [
//user emails => user names
"user1#example.com" => "Example User1",
"user2#example.com" => "Example User2",
"user3#example.com" => "Example User3"
];
$email->addTos($tos);
If you want to see Full example which is provided in sendgrid-php github library then i have included it below, so you can understand the whole example:
<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$tos = [
"test+test1#example.com" => "Example User1",
"test+test2#example.com" => "Example User2",
"test+test3#example.com" => "Example User3"
];
$email->addTos($tos);
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}
Ref:
https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#send-an-email-to-multiple-recipients

Email not sending to one particular domian

We are using amazon server and I am using PHP, Codeigniter framework.
Below is the script which I am using for sending emails.
It is working when I send an email to Gmail domain email id (abc#gmail.com) but it is not working on some specific domain name email id (abc#xyzdmainname.com).
public function sendMail() {
$to = 'abc#xyzdomain.com';
$fromEmail = 'pqr#gmail.com';
$fromName = 'pqr';
$subject = 'Testing';
$message = 'Testing of email';
$newFile = ''
$this->CI->load->library('email');
$this->CI->email->mailtype = 'html';
$this->CI->email->from($fromEmail, $fromName);
$this->CI->email->to($to);
$this->CI->email->subject($subject);
$this->CI->email->message($message);
if (!empty($newFile)) {
$this->CI->email->attach($newFile);
}
$result = $this->CI->email->send();
$message = '';
if ($result) {
return 1;
} else {
echo $this->CI->email->print_debugger();
return 0;
};
Please let me help what-what case may be here for fail sending email on a particular domain.
and How I can overcome this issue.

Categories