How I can use variables in mailgun?
In the test email (test#example.com) I added the following variable
{"age": 20}
but it does not go as is used when creating the mail.
Here goes the code when the mail is sent.
$mailformat = "CODE HTML :D";
$result = $mgClient->sendMessage($domain, array(
'from' => 'Example <example#example.com>',
'to' => 'test#example.com',
'subject' => '%recipient_fname%, testing mail',
'html' => $mailformat
));
You should add the user variables this way:
v:age=20
If you're using Python, in the data element you must add
data={
'from' : 'Name <email#email>',
'to' : email_list,
...
'v:age' : '20'
}
From the documentation:
v: prefix followed by an arbitrary name allows to attach a custom JSON data to the message. See Attaching Data to Messages for more information.
Related
I use mailgun and integrated with php. The mail sends fine without error. I create a default template in mailgun(Action Template). In that template there is a button. My Issue is
How to i pass a parameter to that button a tag
MY code is
$mg = Mailgun::create('MY API');
// Now, compose and send your message.
$result = $mg->messages()->send('MY DOMAIN NAME', [
'from' => 'support#figjam.net',
'to' => $email,
'subject' => 'Confirm Your E-mail',
'html' => ' <p>Please confirm your email address by click the link below <br>
<button type="button" class="btn btn-secondary">CLICK TO ACTIVATE YOUR ACCOUNT</button></p>'
'template' => 'verifyemail'
]);
I need to pass variable to that template verifyemail
Kindly Help me fix this.
Thanks
Let's assume you have myvariable with value someVariableValue
'template' => 'verifyemail',
'v:myvariable' => 'someVariableValue',
]);
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.
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
I'm trying to send an e-mail where the sender is specified. Instead i get Nobody#host.edu
$test='example body'.$random_String;
$message3 = array(
'to' => 'fake_user#place.com',
'from' => 'fake_sender#place.edu',
//'from' => variable_get('site_mail', 'admin#example.com'), //alternative doesn't work either
'subject' => t('Example subject'),
'body' => $test,
'headers' => array(),
);
drupal_mail_send($message3);
Just use good old php mail() function
http://www.php.net/manual/en/function.mail.php
Read the documentation for the drupal_mail_send() function. You're not providing the right parameters to drupal_mail_send(). You're missing the id key.
Plus, this function already uses PHP's mail() so you don't have to use it directly.