I am trying to pass multiple images via Mailgun's inline API-parameter. I have no problem with only one image, but when I try with multiple images - as in the code below - only the last image in the array is displayed in the email.
$template = View::make('emails.template')->render();
$result = $mgClient->sendMessage($domain, array(
'from' => $sender,
'to' => implode(',',$emailAddresses),
'subject' => '%recipient.subject%',
'text' => $messageText,
'recipient-variables' => json_encode($credentials),
'html' => $template
), array(
'inline' => array(
'path/to/image1.png',
'path/to/image2.png',
'path/to/image3.png',
'path/to/image4.png')
));
The code above works as if the last element in the array is the only element.
The documentation for sending inline images with Mailgun is found here and it's said here that "You can post multiple inline values" which means that I'm definitely doing something wrong.
Try this once:
$result = $mgClient->sendMessage($domain, array(
'from' => $sender,
'to' => implode(',',$emailAddresses),
'subject' => '%recipient.subject%',
'text' => $messageText,
'recipient-variables' => json_encode($credentials),
'html' => $template
), array(
'inline' => array(
array('path/to/image1.png'),
array('path/to/image2.png'),
array('path/to/image3.png'),
array('path/to/image4.png')
)));
Basically wrapping each image path in an array.
Also what is the contents of $template?
This was actually a recently introduced bug. A new pull request has been submitted to the official Mailgun PHP SDK, for more info see here.
So to answer the question: the code is working fine, as soon as the SDK is updated according to above pull request. For now I edited my local copy of mailgun-php accordingly and it worked fine. Many thanks to Travis Swientek on Mailgun for quick response.
Related
If I put $author='Steven King'; it works without issue, however it does not work with a post variable.
To be "clear" if I hard code the the author in the JSON it will in fact post the message to the SQS queue. This is the expected result, however if I pass the string from the Post Variable e.g. $author=$_POST['author], the message is never delivered.
$message = array(
// Associative array of custom 'String' key names
'Author' => array(
'StringValue' =>$author,
'DataType' => 'String'
),
);
Any thoughts or help on this I would be grateful.
<?php
$author =$_POST["author"];
require 'vendor/autoload.php';
use Aws\Common\Aws;
use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;
// Get the client from the builder by namespace
$client = SqsClient::factory(array(
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2012-11-05'
));
$queueUrl ='https://sqs.us-west-2.amazonaws.com/blahblahblah';
$message = array(
// Associative array of custom 'String' key names
'Author' => array(
'StringValue' =>$author,
'DataType' => 'String'
),
);
var_dump($message);
$result = $client->sendMessage(array(
'QueueUrl' => $queueUrl,
'MessageBody' => 'An awesome message!',
'MessageAttributes' =>$message,
));
So the issue was caused by the credential provider, which is why it worked in the cli e.g. php posts.php and not in the browser. Because in the CLI it has the correct environment and permissions.
Note: However on AWS SDK example does not include the credential provider only a reference to 'profile' => 'default' which would be thought to grab your default credentials, however that is not the case.
Thank you apache logs!
The fix was to set the right permissions on the /.aws/credentials and ensure
that your $HOME path is set correctly.
On to the next piece. Thanks community.
How do I create a campaign using this wrapper and pass parameters? Ive searched the internet and the documentation but their isn't enough information or example code
$mc->post('campaigns');
just returns an error. Thanks in advance.
Assuming you've authenticated, etc., you can just add in your parameters like the Mailchimp docs show:
$result = $MailChimp->post("campaigns", [
'type' => 'regular',
'recipients' => ['list_id' => 'xxx'],
'settings' => ['subject_line' => 'Your Purchase Receipt',
'reply_to' => 'orders#example.com',
'from_name' => 'Customer Service']
]);
The wrapper doesn't really do anything; it just provides a standard way to pass these arrays to Mailchimp.
I am using hype_mailchimp to create a campaign By MailChimp service.
I ma using symfony2 and my controller code is As,
$mc = $this->get('hype_mailchimp');
$data = $mc->getCampaign()->create('regular', array(
'list_id' => '4a74c85872',
'subject' => 'test created subject',
'from_email' => 'mnish.shah#kivite.com',
'from_name' => 'Mnish Shah',
'to_name' => 'mnishshah'
), array(
'html' => '<h5>Html content</h5>',
'sections' => array(),
'text' => 'test',
'url' => 'http://www.example.com',
'archive' => 'test'
))->send();
The Error shows Error: Call to a member function send() on a non-object
means $data remains null always.
pls help me.
It looks like the create method does not return an object with the send method.
Looking at the read me examples, sending the campaign needs to call getCampaign first so it looks like you need to do it as 2 seperate requests.
https://github.com/AhmedSamy/HypeMailchimpBundle
I'm trying to send a Mandrill email in the default Laravel 4 Mailer, and I want to set the template and send the data to Mandrill. I'd rather not use a local file in my Laravel, I have this all set up to work inside of Mandrill. This is how I have it set in Laravel:
Mail::send('emails.test',[],function($message)
{
$message->to(Input::get('email'), 'John Smith')->subject('Welcome!');
});
return
This sends an email from my "test" view inside of "emails" like it should. In vanilla PHP using the Mandrill library this would work to send to the template I want.
$message = array(
'subject' => 'Subject redacted',
'from_email' => $main_email,
'to' => array(array('email' => $to_email)),
'merge_vars' => array(array(
'rcpt' => $to_email,
'vars' =>
array(
array(
'name' => 'DETAIL',
'content' => $detail),
array(
'name' => 'ERROR_AMOUNT',
'content' => '$'.$trans_amount)
,
array(
'name' => 'CO_NAME',
'content' => $business_name),
array(
'name' => 'SMS',
'content' => $sms)
))));
$template_name = 'Test Email';
$template_content = array(
array(
'name' => 'main',
)
);
$response = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
As an alternative, you could set the X-MC-MergeVars and X-MC-Template headers using Laravel's Mandrill functions.
Mail::send('emails.test', [], function($message) use ($mergevars)
{
$headers = $message->getHeaders();
$headers->addTextHeader('X-MC-MergeVars', json_encode($mergevars));
$headers->addTextHeader('X-MC-Template', 'my-template');
$message->to(Input::get('email'), 'John Smith')->subject('Welcome!');
});
Note that I haven't tested this - just going by what I read here: Using SMTP Headers to customize your messages.
You might need to use something other than addTextHeader - check with the Swiftmailer documentation to work out how to add the correct header type for the data you are setting.
It doesn't appear that this is possible using Illuminate\Mail\Mailer. It looks like the Mandrill transport specifically uses the send-raw Mandrill API endpoint.
This leaves you with two options: Implement your own transport and work within the Mailer, or work outside the Mailer system. If you're going to go with the latter, you'd probably be best off just using the official library.
If you want to implement your own transport, take a look at the Swift_Transport interface. It's not going to be a perfect match, since you're attempting to do something that the Mailer was never intended to do, but with enough hacking, you could get something up and running. You'd probably need to use undefined members on the Message class, like:
$message->template = "template_name";
$message->content = array(/* content here */);
$message->message = array(/* message here */);
// MandrilTemplateTransport.php
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->messages->send($message->template, $message->content, $message->message);
}
This is absolutely less than ideal, but a workable solution. I'd vote that you just use the Mandrill library outside of the Mailer system.
I am trying to add some attachments to an email that is being sent using the mandrill api via a php wrapper. I have tried a number of different things to try to successfully attach the file but to no avail.
I am using cakephp 2.x but I don't think that has any particular significance in this instance (maybe it does?!).
I am using the php wrapper maintained by mandrill at https://bitbucket.org/mailchimp/mandrill-api-php
Here is the code:
$mandrill = new Mandrill(Configure::read('Site.mandrill_key'));
$params = array(
'html' => '
<p>Hi '.$user['User']['name'].',</p>
<p>tIt is that time of the year again.<br />
Please login to the website members area and upload your renewal requirements.</p>
<p>Kind regards.</p>',
"text" => null,
"from_email" => Configure::read('Site.email'),
"from_name" => Configure::read('Site.title'),
"subject" => "Renewal Pending",
"to" => array(array('email' => $user['User']['email'])),
"track_opens" => true,
"track_clicks" => true,
"auto_text" => true,
"attachments" => array(
array(
'path' => WWW_ROOT.'files/downloads/renewals',
'type' => "application/pdf",
'name' => 'document.pdf',
)
)
);
$mandrill->messages->send($params, true);
}
This shows that an attachment has been added to the email and is a pdf but the actual pdf has not been attached.
I also tried by adding the path directly onto the file as in:
"attachments" => array(
array(
'type' => "application/pdf",
'name' => WWW_ROOT.'files/downloads/renewals/document.pdf',
)
I have googled and read every article I can find but cannot find any specific reference as to how I should specify the path for mandrill to correctly attach my attachment.
Any help will be greatly appreciated.
OK. So thanks to Kaitlin for her input.
The PHP way to deal with this is to get the file and then base64_encode it:
$attachment = file_get_contents(WWW_ROOT.'files/downloads/file.pdf');
$attachment_encoded = base64_encode($attachment);
and then in the attachments part of the mandrill array you pass the :
"attachments" => array(
array(
'content' => $attachment_encoded,
'type' => "application/pdf",
'name' => 'file.pdf',
)
So easy! Thanks again Kaitlin!
It looks like you're passing a parameter called path, but the Mandrill API doesn't take the path of a file for attachments. If you're using the send or send-template call, attachments should be an associative array (hash) with three keys: type, name, and content.
The content parameter should be the contents of the file as a Base64 encoded string, so instead of path, you'll want to get the file contents, Base64 encode them, and then pass them in a parameter called content instead of path.
You can see the full details of the parameters, including for attachments, in the Mandrill API docs here: https://mandrillapp.com/api/docs/messages.html#method=send