MailGun attachment not showing up PHP - php

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
]
]);

Related

MS Graph and PHP : Add location that is recognized in Outlook

we are using the following code in order to add a room (Resource) to the location in Outlook calendar event:
$response = [
'Subject' => $subject,
'Body' => [
'ContentType' => 'HTML',
'Content' => 'This is event generated by Clebex!'
],
'Start' => [
'DateTime' => $declaration->datetime_from,
'TimeZone' => 'UTC'
],
'End' => [
'DateTime' => $declaration->datetime_to,
'TimeZone' => 'UTC'
],
'Attendees' => $attendees,
'Location' => [
[
'displayName' => 'Room 1',
'emailAddress' => 'room1#example.com',
'locationIdentifier' => [
'id' => $organizer,
'type' => 'room'
],
]
],
'isOnlineMeeting' => true,
'onlineMeetingProvider' => $meetingProvider
];
Outlook calendar event is created successfully, but location resource is not recognize as such by Outlook. Instead of that in the location field there is only a string 'Room 1'.
Is there a way to 'force' Outlook to read the information from the location and recognize it as the resource?
Many thanks!
So, after doing more research: solution is to add the room as attendee, with type 'resource' instead of 'required'.
After this, room is placed in Location field and is recognized as the resource in Outlook.

Guzzle: how to send a file and a second text field?

I must simulate the sending of a form which has
a file selector
a text input
I'm sending the file using this snippet
$response = $this->getClient()->request(
$method,
$endpoint,
[
'headers' => ['Content-Type' => 'multipart/form-data' ],
'multipart' => [
[
'Content-type' => 'multipart/form-data',
'name' => 'file _name.tiicsti',
'contents' => 'hello my content',
'filename' => 'filename.txt',
]
]
]
)
;
It works.
My question is: how to send a text field in addition to my file?
Resolved !
I am surprise that headers is not needed. Probably Guzzle makes some magic
$response = $this->getClient()->request(
$method,
$endpoint,
'multipart' => [
[
'name' => 'text-field-name',
'contents' => 'text field value'
],
[
'name' => 'file _name.tiicsti',
'contents' => 'hello my content',
'filename' => 'filename.txt',
]
]
]
)
;

mailgun - send mail with attachment

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.

Sendgrid sending mail with attachment using web api v3

I'm new to using sendgrid web api v3. link here
Right now. It was easy to send a plain html using there api 'POST https://api.sendgrid.com/v3/mail/send' but I have this instance where we will attach a file (csv/xls,pdf) and I can't seem to get it right.
Here is my code below:
My function postSendMail
public function postSendMail($data = [])
{
if ( ! arrayHasValue($data) ) $this->error(__METHOD__, "Data is empty.");
$request = Curl::to( $this->apiUrl.'mail/send' )
->withHeader('Authorization: Bearer '. $this->apiKey)
->withData( $data )
->asJson(true)
->enableDebug(storage_path('logs/laravel-'.php_sapi_name().'.log'))
->post();
return $request;
}
//my instance
$sendgrid = new Sendgrid;
$data = [
'personalizations' => [
[
'to' => [
[ 'email' => 'myemail#gmail.com' ]
],
'subject' => 'Hello, World!'
]
],
'from' => [
'email' => 'myemail#gmail.com',
'name' => 'my_site'
],
'content' => [
[
'type' => 'text',
'value' => 'Hello, World!'
]
],
'track_settings' => [
[
'click_tracking' => true,
'open_tracking' => true
]
],
'attachments' => [
[
'content' => base64_encode(config('global.UPLOAD_PATH') . '/my_file.pdf'),
'type' => 'application/pdf',
'filename' => 'my_file.pdf',
'disposition' => 'attachment'
]
]
];
$lists = $sendgrid->postSendMail($data);
Mail was successfully sent but when I view the attached file, it was corrupted/unable to view. Can anyone help me? :(
Please help.
The problem is that you are not reading the file into an object and then encoding that object; you're encoding a string containing the file path.
'content' => base64_encode(config('global.UPLOAD_PATH') . '/my_file.pdf')
All of your attachments in the tests are probably the same size, and smaller than the actual file as a result.
Try something like:
$imagedata = file_get_contents(config('global.UPLOAD_PATH') . '/my_file.pdf');
$base64 = base64_encode($imagedata);
Coming to main point you need to get file content either by curl request or by file_get_content then encode the that content into attachments->content parameter, Please check following code which works for me:
'attachments' => [
[
'content' => base64_encode(file_get_contents(config('global.UPLOAD_PATH') . '/my_file.pdf')),
'type' => 'application/pdf',
'filename' => 'my_file.pdf',
'disposition' => 'attachment'
]
]

Sending email with Mandrill using PHP

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.

Categories