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.
Related
Email shows up with no file attached. I have a text file at test/test.txt which will not attach. Here is a simple function. When used, the email shows up with no attachment:
function send_email_with_attachment($email,$name){
$mgClient = new Mailgun(MAILGUN_KEY);
$domain = "mg.example.com";
$file = array(
array(
'filePath' => 'test',
'filename' => 'test.txt'
)
);
$result = $mgClient->sendMessage($domain,array(
"from" => "Company Support <support#example.com>",
"to" => "{$name}<{$email}>",
"subject" => "File Attached",
"text" => "Here is a cool text file",
'attachment' => json_encode($file)
));
}
Why don't use directly attachment like that:
$result = $mgClient->sendMessage($domain,array(
"from" => "Company Support <support#example.com>",
"to" => "{$name}<{$email}>",
"subject" => "File Attached",
"text" => "Here is a cool text file",
'attachment' => [
['filePath'=>'/test.txt', 'filename'=>'test']
]
));
Try this code to from official source:
$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']
]
]);
The issue had to do with the confusion on my part about how Mailgun shows their example filePath and filename variable values.
As stated above, here is one the examples from MailGun:
$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'] //what? Which file is being sent?
]
]);
There were a couple of gotcha's here for me:
First was that I was using a path relative to the php file and not from the root of my server.
The second was that I didn't understand the mailgun example. In the example, which file is being sent? For me, this example would have been clearer:
$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'=>'foo.jpg'] //this is a better way to demonstrate
]
]);
I've been using mailgun for a while and I've always wondered how to make a cc/bcc optional (so if the user wants to Cc themselves it would populate and if not it wouldn't populate).
$result = $mgClient->sendMessage($domain, array(
'from' => 'postmaster#example.com',
'to' =>'to#example.com',
'cc' => 'MAKE THIS OPTIONAL',
'h:Reply-To' => 'reply#example.com',
'subject' => 'Subject,
'html' => '<!doctype html>....'
Much more simplest way is next.
If you have filled 'cc' or 'bcc', for example, in $_POST or $_GET:
$default_array = [
'from' => 'postmaster#example.com',
'to' =>'to#example.com',
'h:Reply-To' => 'reply#example.com',
'subject' => 'Subject,
'html' => '<!doctype html>....
];
if (!empty($_POST['cc']) && isset($_POST['cc'])) $default_array['cc'] = $_POST['cc'];
// the same with 'bcc' and others
$result = $mgClient->sendMessage($domain, $default_array);
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);
}
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
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',
)
),
)
));
?>