Is it just me or is there almost no documentation on how to do this? Mandrill's site isn't much help. Anyway...here's the problem...
try {
$mandrill = new Mandrill('API_KEY');
$attachment = file_get_contents("../template.xls");
$attachment_encoded = base64_encode($attachment);
$message = array(
'html' => '<p>Some html text</p>',
'text' => 'Some text',
'subject' => 'Subject',
'from_email' => 'email#domain.com',
'from_name' => 'Name',
'to' => array(
array(
'email' => 'myemail#domain.com',
'name' => 'Name',
'type' => 'to'
)
),
'attachments' => array(
array(
'type' => 'application/vnd.ms-excel',
'name' => 'New_Features_Submission.xls',
'path' => $attachment_encoded
)
)
);
$async = false;
$result = $mandrill->messages->send($message, $async);
print_r($result);
} catch (Mandrill_Error $e) {
echo 'A Mandrill error occured: ' . get_class($e) . '-' . $e->getMessage();
throw $e;
}
The email sends correctly, but I can't even open the .xls file. I tried using 'application/xls' for the type, that didn't work either. I'm not familiar with encoding, help please!
This line:
'path' => $attachment_encoded
Needs to be this:
'content' => $attachment_encoded
Related
I am trying to send mail in symfony with attachment with the help of mailgun
I have referred this Question. And this Reference. But didn't helped me.
This is my function,
public function sendMail($to, $subject, $body, $attachment = null)
{
$mgClient = new Mailgun($this->container->getParameter('mailgun_key'));
$domain = $this->container->getParameter('mailgun_domain');
$content = [
'from' => $this->container->getParameter('mailgun_from'),
'to' => 'tester <' . $to . '>',
'subject' => $subject,
'text' => $body
];
if (!empty($attachment)) {
$result = $mgClient->sendMessage("$domain", $content);
} else {
$result = $mgClient->sendMessage("$domain", $content, [
'attachment[0]' => [$attachment]
]);
}
return $result;
}
In attachment, I'm passing the file path. i.e /home/mypc/Downloads/test.txt
But I'm receiving only blank mail. Attachment is not coming.
Where am I wrong? Please help.
The mailgun attachments documentation has the following information:
Attachments
You may attach a file from memory or by a file path.
From file path
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'Test file path attachments',
'text' => 'Test',
'attachment' => [
['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
]
]);
From memory
// Some how load the file to memory
$binaryFile = '[Binary data]';
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'Test memory attachments',
'text' => 'Test',
'attachment' => [
['fileContent'=>$binaryFile, 'filename'=>'test.jpg']
]
]);
Inline attachments
$mg->messages()->send('example.com', [
'from' => 'bob#example.com',
'to' => 'sally#example.com',
'subject' => 'Test inline attachments',
'text' => 'Test',
'inline' => [
['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
]
]);
Please replace below code
$result = $mgClient->sendMessage("$domain", $content, [
'attachment[0]' => [$attachment]
]);
With
$result = $mgClient->sendMessage("$domain", $content, array(
'attachment' => array($attachment)
));
Eg.
$result = $mgClient->sendMessage("$domain", $content, array(
'attachment' => array('/home/mypc/Downloads/test.txt')
));
Referance: https://documentation.mailgun.com/user_manual.html#sending-via-api
Example below works for me.
Test
$mgClient = new Mailgun('key-abcfdfa5b40b6ea0ec0ccf9c33a90y65');
$domain = "sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org";
// SEND
$result = $mgClient->sendMessage(
$domain,
[
'from' => 'Sender <mailgun#sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org>',
'to' => 'Receiver <bentcoder#sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org>',
'subject' => 'Test subject',
'text' => 'Test message body',
],
[
'attachment' => [
'/var/www/html/local/test_app/logo.jpg',
'/var/www/html/local/test_app/ngrok.png'
]
]
);
// END
print_r($result);
stdClass Object
(
[http_response_body] => stdClass Object
(
[id] =>
[message] => Queued. Thank you.
)
[http_response_code] => 200
)
You might look into https://github.com/tehplague/swiftmailer-mailgun-bundle as well.
I can't find what's wrong with the attachment. The email is sent empty.
This is for drupal 7 customized webform. Here is my code:
<?php
module_load_include('inc', 'print_pdf', 'print_pdf.pages');
$file_content = module_invoke('print_pdf', 'generate_path', '/****.pdf');
$attachment = array(
'filecontent' => $file_content,
'filename' => '****.pdf',
'filemime' => 'application/pdf',
'list' => TRUE
);
$message = array(
'headers' => array('Content-Type' => 'text/html'),
'key' => 'test',
'to' => '****#****.com',
'from' => '****#****.com',
'attachments' => $attachments,
'subject' => 'Test email',
'body' => 'test'
);
$system = drupal_mail_system('mimemail', 'test');
$system->format($message);
$result = $system->mail($message);
?>
Try to check path and access to file.
I use this code to send unmanaged files as attachments with Mimemail and MailSystem:
/**
* Send email with attachment
*/
mymodule_send('default', array(
'params' => array(
'subject' => 'Mail suject',
'body' => '<p>Mail body</p>',
'attachments' => array(
array(
'filename' => 'HumanreadableFileName.xls',
'filemime' => 'application/vnd.ms-excel', // xls mime
'filepath' => 'public://2016-04-18_report.xls', // path to file
),
),
),
), 'john#doe.com');
/**
* Implements hook_send().
*/
function mymodule_send($key, $params = array(), $to = NULL, $from = NULL) {
if (!valid_email_address($to)) {
$to = variable_get('site_mail', ini_get('sendmail_from'));
}
drupal_mail('snabmail', $key, $to, language_default(), $params, $from);
}
I'm trying to send email using Mandrill and PHP and I can't get it to send.
I've downloaded the PHP API wrapper from here:https://packagist.org/packages/mandrill/mandrill
Mandrill.php is in my root and the Mandrill folder is in the same directory.
Here's my code:
<?php
require_once 'Mandrill.php';
$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
'subject' => 'Test message',
'from_email' => 'jwjody#yahoo.com',
'from_name' => 'Sender person',
'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
'to' => array(array('email' => 'jwjody#yahoo.com', 'name' => 'Recipient 1')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#domain.com',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Recipient 1 first name'),
array(
'name' => 'LASTNAME',
'content' => 'Last name')
))));
//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
echo ("hello");
?>
But it won't send. I'm not sure where the failure is. Is it something obvious I'm missing?
I see the issue now.
I see what's going on now!
I changed
$mandrill->messages->sendTemplate($template_name, $template_content, $message));
to
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
And it works!
Instead of calling the sendTemplate() function I should have used
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
Once I changed the function call the mail was sent.
I'm using AWS SDK for PHP (https://github.com/aws/aws-sdk-php) to send emails using Amazon SES.
Here's the code:
<?php
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
$client = SesClient::factory(array(
'key' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'region' => 'eu-west-1'
));
$result = $client->sendEmail(array(
// Source is required
'Source' => 'Télécom Co <email#address.com>',
// Destination is required
'Destination' => array(
'ToAddresses' => array('Grégory Smith <another_email#address.com>')
),
// Message is required
'Message' => array(
// Subject is required
'Subject' => array(
// Data is required
'Data' => 'The subject',
'Charset' => 'utf-8',
),
// Body is required
'Body' => array(
'Text' => array(
// Data is required
'Data' => 'The message',
'Charset' => 'utf-8',
)
),
)
));
?>
The problem is that in the email clients "Télécom" appears like "T�l�com" and "Grégory" like "Gr�gory".
Are there any solutions for this problem?
Here's the solution:
<?php
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
$client = SesClient::factory(array(
'key' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'region' => 'eu-west-1'
));
$from_name = base64_encode("Télécom Co");
$from = "=?utf-8?B?$from_name?= <email#address.com>";
$to_name = base64_encode('Grégory Smith');
$to = array("=?utf-8?B?$to_name?= <another_email#address.com>");
$result = $client->sendEmail(array(
// Source is required
'Source' => $from,
// Destination is required
'Destination' => array(
'ToAddresses' => $to
),
// Message is required
'Message' => array(
// Subject is required
'Subject' => array(
// Data is required
'Data' => 'The subject',
'Charset' => 'utf-8',
),
// Body is required
'Body' => array(
'Text' => array(
// Data is required
'Data' => 'The message',
'Charset' => 'utf-8',
)
),
)
));
?>
Im trying to take data from Contact Form 7 WordPress plugin and pass it with json to GetResponse API
I have a php file that pull the data and send it, and its looks like this
I'm getting the CF7 email of confirmation but im not getting GetResponse Email confirmation for newsletter, i don't understand why, i tried to do some debugging and there was no errors
add_action('wpcf7_before_send_mail', 'mytheme_save_to_getresponse', 10, 1);
function mytheme_save_to_getresponse($form)
{
include 'ChromePhp.php';
require_once 'jsonRPCClient.php';
$api_key = 'some_api_key';
$api_url = 'http://api2.getresponse.com';
$client = new jsonRPCClient($api_url);
$result = NULL;
try {
$result = $client->get_campaigns(
$api_key,
array (
# find by name literally
'name' => array ( 'EQUALS' => 'testcase_001' )
)
);
}
catch (Exception $e) {
# check for communication and response errors
# implement handling if needed
die($e->getMessage());
}
$campaigns = array_keys($result);
$CAMPAIGN_ID = array_pop($campaigns);
$subscriberName = $_POST['name'];
$subscriberEmail = $_POST['email'];
$subscriberPhone = $_POST['c_phone'];
$subscriberCellphone = $_POST['c_cellphone'];
$subscriberArea = $_POST['c_area'];
$subscriberInsuranceType = $_POST['c_type'];
$subscriberCarType = $_POST['c_cartype'];
$subscriberManifacture = $_POST['c_manifacture'];
$subscriberManifacturemodel = $_POST['c_manifacturemodel'];
$subscriberManifactureYear = $_POST['c_manifactureyear'];
$subscriberDriverAge = $_POST['c_driversage'];
$subscriberPrevent = $_POST['c_prevent'];
$subscriberClaim = $_POST['c_claim'];
try {
$result = $client->add_contact(
$api_key,
array (
'campaign' => $CAMPAIGN_ID,
'name' => $subscriberName,
'email' => $subscriberEmail,
'cycle_day' => '0',
'customs' => array(
array(
'name' => 'home_phone',
'content' => $subscriberPhone
),
array(
'name' => 'cell_phone',
'content' => $subscriberCellphone
),
array(
'name' => 'living_area',
'content' => $subscriberArea
),
array(
'name' => 'drivers_age',
'content' => $subscriberDriverAge
),
array(
'name' => 'insurance_type',
'content' => $subscriberInsuranceType
),
array(
'name' => 'car_type',
'content' => $subscriberCarType
),
array(
'name' => 'manifacture_type',
'content' => $subscriberManifacture
),
array(
'name' => 'manifacture_model',
'content' => $subscriberManifacturemodel
),
array(
'name' => 'manifacture_year',
'content' => $subscriberManifactureYear
),
array(
'name' => 'license_loss',
'content' => $subscriberPrevent
),
array(
'name' => 'claims_number',
'content' => $subscriberClaim
)
)
)
);
}
catch (Exception $e) {
# check for communication and response errors
# implement handling if needed
die($e->getMessage());
}
}
Thanks in advance
everything was OK with this code, i GetResponse don't send confirmation requests more than once on the same email..