SparkPost PHP reply_to not working - php

I am using SparkPost PHP API for sending emails and it seems like reply_to feature is not working. I tried to both ways with headers and with reply_to field. Any ideas what could be wrong? Domain name of reply_to emails is different as senders one. I didn't found any restrictions regarding this in their documentation. Any ideas?
Here is my code:
$emailData = array(
'from'=> $data["from_name"].' <'.$data["from_email"].'>',
'html'=> $data["html"],
'inline_css' => true,
'transactional' => true,
'subject'=> $data["subject"],
'recipients'=> $rec["r"]
);
if(isset($data["headers"]["Reply-To"]))
$emailData['reply_to'] = $data["headers"]["Reply-To"];
try {
// Build your email and send it!
$this->mandrill->transmission->send($emailData);
} catch (\Exception $err) {
echo "<pre>";
print_r($err);
echo "</pre>";
}

Regarding: SparkPost PHP ReplyTo, reply_to, Reply
For anyone else wondering the same thing. Here's my implementation using SparkPost client library for PHP v2.1. I hope it helps.
I used the transmissions endpoint as seen in the docs.
https://github.com/sparkpost/php-sparkpost
$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'Company Name',
'email' => 'noreply#company.com',
],
'reply_to' => $email,
'subject' => 'Some Subject',
'html' => $html_message,
'text' => $text_message,
],
'substitution_data' => $subData,
'recipients' => [
[
'address' => [
'name' => 'My Recipient',
'email' => 'me#company.com',
]
],
],
]);

Thank god for slack :)
Solution is that SparkPost has different name for parameters in API documentation. Correct parameter for PHP API is not reply_to (as it's written in doc) but replyTo.

Related

How to save emails in an email list on SendGrid using Laravel?

I'm working on a functionality where once a user signs up for an account, he will start getting a series of email throughout a time period (email drips).
I'm using Laravel. I need to grab the email on sign up and save it on an email list on SendGrid. But I don't find this on the sendgrid/sendgrid-php package's docs.
So, would you please tell me how I may implement this?
It can be done with the sendgrid-php package:
use SendGrid;
$api = new SendGrid(YOUR_API_KEY);
$contacts = [
[
'email' => "email#something",
'address_line_1' => '',
'city' => '',
'country' => '',
'first_name' => '',
'last_name' => '',
etc....
]
]
$request_body = [
'contacts' => $contacts
];
$response = $api->client->marketing()->contacts()->put($request_body);
if($response->statusCode() !== 202) {
// error has occured
}

Using the Onesignal Api "ADDITIONAL DATA" field

I use Onesignal notifications for the app.
I don't have a problem sending the notifications but wanted to send these notifications using API help and this library.
php-onesignal library
The notifications I sent must be opened on the relevant page in the application. For this reason, I do not know how to write the URL in the code section below.
require_once(dirname(__FILE__).'/vendor/autoload.php');
use CWG\OneSignal\OneSignal;
$appID = '92b9c6bb-89d2-4cbc-8862-a80e4e81a251';
$authorizationRestApiKey = 'MWRjMTg2MjEtNTBmYS00ODA4LWE1M2EtM2YyZjU5ZmRkNGQ5';
$deviceID = '69aeecc1-7b58-44d1-8000-7767de437adf';
$api = new OneSignal($appID, $authorizationRestApiKey);
$retorno = $api->notification->setBody('Ola')
->setTitle('Titulo')
->addDevice($deviceID)
->send();
I entered the addTag section as in the Onesignal panel, but I could not run it.
$retorno = $api->notification->setBody('Ola')
->setTitle('Titulo')
->addTag('url', 'http://www.example.com/news/testtitle')
->send();
print_r($retorno);
How can I use it here in the Url section of the "ADDITIONAL DATA" field?
Can I solve this problem with addTag?
The following will send a notification with the $data['data'] as additional, as you want.
$data = [
'headings' => ['en' => 'Case 123'],
'contents' => ['en' => 'Case assigned ' . date('d-m-Y H:i')],
'data' => [
'type' => 'new',
'user_id' => 123,
'url' => 'http://www.example.com/news/testtitle'
],
];
OneSignal::sendNotificationCustom([
'app_id' => 1234,
'api_key' => abcd,
'included_segments' => ['All'],
'headings' => $data['headings'],
'contents' => $data['contents'],
'data' => $data['data'],
]);

Sending email with sendgrid on Azure using PHP returning status code 0

my PHP application is hosted on Azure and I'm using SendGrid PHP client to send emails to the application users. Everything was working fine till yesterday evening and suddenly after that emails have stopped working. On further debugging I found that the following piece of code
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
is returning statusCode as zero. 0 as I understood is not returned by the server. To cross check I sent test mails through my local XAMPP PHP server and it worked well. So I assume that Azure is not able to reach sendgrid servers.
Please note that the same code was working till yesterday evening and neither code changes nor setting changes were done to trigger any issue.
To reproduce the issue, I have created a simple procedure
public function test_sendgrid_email(){
$api_key = '***************************'; //Hidden for security reasons
$request_body = [
'personalizations' => [
[
'to' => [
[
'email' => 'test#example.com',
'name' => 'Test'
]
],
'subject' => "Sending with SendGrid is Fun"
]
],
'from' => [
'email' => 'example#test.com',
'name' => 'Example'
],
'content' => [
[
'type' => 'text/html',
'value' => "and easy to do anywhere, even with PHP"
]
]
];
$request_body_formatted = json_decode(json_encode(($request_body)));
$sg = new \SendGrid($api_key);
try{
$response = $sg->client->mail()->send()->post($request_body_formatted);
return ['status' => 'success', 'message' => 'status code : ' . $response->statusCode()];
} catch (Exception $ex) {
return ['status' => 'failure', 'message' => 'Failure'];
}
}
Output: {"status":"success","message":"status code : 0"}
Is there any thing that has to be changed in Azure settings to get it back working? Any recent updates to Azure environments that forced this issue ?

Yii2 SwiftMailer sends mails twice

I write a game for football fans. So, I have to send similar mails to a group of people (not completely duplicated e-mail copies).
When I send the mails in a cycle - Yii framework sends the mails twice.
I suppose - it is because of the static variable Yii::$app.
Can someone give me a hint, please.
A code for example.
foreach ($oRace->user as $currUser) {
$htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
Yii::$app->mailer->compose()
->setFrom('info#example.com')
->setTo($currUser->mail)
->setSubject('Race "' . $raceName . '" has Started')
->setHtmlBody($htmlContent)
->send();
}
Thanks all in advance!
My Mailer config.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'mail.example.eu',
'username' => 'support#example.com',
'password' => 'password',
'port' => '587',
'encryption' => 'TLS',
]
],
One more thing. The last mail in the cycle is never duplicated (only the last).
Another failed option.
Yii::$app->mailer->sendMultiple($allMails);
I recommend you to use CC or BCC option in the email instead of using foreach loop to send emails. I hope this will help someone.
$email = [];
foreach ($oRace->user as $currUser) {
$email[] = $currUser->mail;
}
$htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
Yii::$app->mailer->compose()
->setFrom('info#example.com')
->setCc($email) // If you're using Bcc use "setBcc()"
->setSubject('Race "' . $raceName . '" has Started')
->setHtmlBody($htmlContent)
->send();
From the provided code snippets, there are 3 possible reasons for that. Either:
$oRace->user contains every user twice
$currUser->mail contains the email twice like `email#example.com;email#example.com"
something is wrong inside the send function of SwiftMailer
After all - I have found that the issue was not with my Yii2 framework, but with my hosting mail server.
I have used https://github.com/ChangemakerStudios/Papercut for listening what my framework sends. It receives mails on port 25, while it listens for events on port 37804. It's a little bit confusing. Yii2 web.php simple configuration for local mail server is:
$config = [
'components' =>
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost', // '127.0.0.1'
'port' => '25',
]
],
],
];
Thank of all, who have read my post!

Retrieving JSON response using HTTPful library for Laravel

I am currently building a e-mail client (inbound and outbound sending) using Mandrill as the e-mail sending / inbound service and Laravel 3.x.
In order to send messages, I am using the HTTPful bundle with the Mandrill using the following code in my mail/compose POST method.
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
Link to better formatted code above: http://paste.laravel.com/m79
Now as far as I can tell from the API log, the request is correctly made (with the expected JSON) and a response of the following format is sent back:
[
{
"email": "test#test.com",
"status": "queued",
"_id": "longmessageID"
}
]
However, what I am trying to do is access the response from the request (specifically the _id attribute), which is in JSON. Now as far as I'm aware, the HTTPful class should do this automatically (using json_decode()). However, accessing:
$request->_id;
is not working and I'm not entirely sure how to get this data out (it is required so I can record this for soft-bounce, hard-bounce and rejection messages for postmaster-like functionality)
Any assistance would be appreciated.
Edit
Using the following code, results in the mail being sent but an error returned:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request[0]->status == "queued" ) {
$success = true;
}
Results in an exception being thrown: Cannot use object of type Httpful\Response as array
I must say, a huge thanks to Aiias for his assistance. I managed to fix this myself (I must have spent hours looking at this). For anyone who wants to know, the HTTPful bundle has a body array, where the response is kept. Therefore, the code below works:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request->body[0]->status == "queued" ) {
$success = true;
}
Again, huge thanks to Aiias for clearing some major confusion up for me!

Categories