I am fairly new to Yii. I am using YiiMail extension to send mails. I am able to send mails but unable to send attachments with it.
I have got the following code but now knowing what that "tempName" would exactly mean?
mycontroller-
$uploadedFile = CUploadedFile::getInstanceByName('filename'); // get the CUploadedFile
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFile); // create a Swift Attachment
$this->email->attach($swiftAttachment); // now attach the correct type
The if you upload a file (e.g. c:\path\file\myfile.jpg ), if is temporary stored on the server in a temporarry folder with a temporary name (e.g. /tmp/zxhjkqwf.tmp ).
The CUploadedFile wraps all the functions you need to access and manipulate the file.
So the tempname will be the path to your file on the server.
I guess you should try to alter your code lioke this:
$uploadedFile = CUploadedFile::getInstanceByName('filename'); // get the CUploadedFile
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName); // create a Swift Attachment from the temporary file
$this->email->attach($swiftAttachment); // now attach the correct type
See more info in the Documentation
Related
I tried to attach my local file using PHP Mailer. I'm getting attachment only if the attachment file is in own server, but when I tried to attach the file from my c drive say [C:\Users\emp10144\Downloads], am getting attachment but with blank page. Did I need to modify my codings. Below is the codings I have used.
$mail->From = 'admin123#sampledemos123.online';
$mail->FromName = 'Admin';
$mail->AddAddress('targetmail#gmail.com', 'User'); // Add a recipient
//$mail->AddAddress('ellen#example.com'); // Name is optional
//$mail->AddAttachment('Daily_Milk_Report.csv','Daily_Milk_Report.csv');
This is working fine as the attcahment file is in own server
$filename = "C:\Users\emp10144\Downloads','Daily_Milk_Report.csv"; // Need to attcah this file from C drive/folder.
//$string = file_get_contents("C:\Users\emp10144\Downloads\sample.pdf");
$mail->AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/vnd.ms-excel');
$mail->IsHTML(true); // Set email format to HTML
No, you cannot pass a URL to addAttachment and have it fetch the resource.
This is intentional; PHPMailer is not an HTTP client, and actively avoids being one. If you want to do this, you need to take responsibility for the fetch yourself, which is most easily achieved like this:
$mail->addStringAttachment(file_get_contents($url, 'myfile.png'));
I have a dxf file saved in my public_html folder on my server. I would like to add this as an attachment to an email. I apply the following code line:
$mail->AddStringAttachment($_SERVER['DOCUMENT_ROOT'] . '/myDxf.dxf', 'myFile.dxf', 'base64', 'application/pdf');
This attaches a dxf and the email is sent. However, when I download the attachment, instead of being a true dxf, it just has a string inside with the file path:
/home3/frank/public_html/myDxf.dxf
Can anyone see what I am doing wrong?
Here are two places to start troubleshooting:
1. Attaching of File
Instead of this:
$mail->AddStringAttachment()
try this:
$mail->AddAttachment()
File Attachments
The command to attach a local file is simply
$mail->addAttachment($path);, where $path contains the path to the
file you want to send, and can be placed anywhere between $mail = new PHPMailer; and sending the message. Note that you cannot use a URL
for the path - you may only use local filesystem path.
If you want to send content from a database or web API (e.g. a remote PDF generator), do not use this method - use addStringAttachment instead.
2. MIME type
Instead of this:
application/pdf
try this:
image/vnd.dxf
List of MIME types: http://www.freeformatter.com/mime-types-list.html
How to update stripe's legal_entity.verification.document with PHP ?
Is it a file to upload to stripe.
The legal_entity[verification][document] attribute should be set to a file upload's ID.
This part of the documentation explains how to upload a file using Stripe's API, and this part explains how to attach the uploaded file to a managed account.
You need to first pass the file to your server in multipart/form-data format. Get the path and add the path in Stripe FileUpload function.
You can even test the file upload by keeping the file manually in server directory and pass the file path to the function.
Below is the sample.
$account = \Stripe\Account::retrieve('acct_xxxxxxxxxxx');
$uploadedFile = \Stripe\FileUpload::create(
array("purpose" => "identity_document",
"file" => fopen('file.png', 'r')
)
);
You will get a success upload response with file id and this id you need to pass in legal_entity.verification.document like :
$account->legal_entity->verification->document = $uploadedFile->id;
$account->save();
As always here is the place where I have learned a lot. And I have now a new things to learn:
I have a html form:
<tr><td width="16%">File attachment</td><td width="2%">:</td><td><input type="file" name="fileatt" /></td></tr>
and a mail.php:
$attachfile=$_POST["fileatt"];
and a correct swiftmailer code to send emails out;
I have googled and I found many examples how to send attachment with a file stored on the website but I would like to do it on the fly. So when you submit the button it would send it to peoples out rather than uploading the file.
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.server.co.uk', 25)
->setUsername('user')
->setPassword('pass')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance($subject)
->setFrom(array('emai#emai.com' => 'name'))
->setBody($html, 'text/html')
;
// Add alternative parts with addPart()
$message->addPart(strip_tags($html), 'text/plain');
// Send the message
$result = $mailer->send($message);
could anyone help me how to do the on the fly file uploading, please? Thanks in advance!!!
There's a simple way to do this, here you go:
$message->attach(
Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('myfilename.jpg')
);
That's one way SwiftMail can do this, now just the /tmp file, and turn the above into the following:
Assuming: fileatt is the variable for the $_FILE, ['tmp_name'] actually is the tmp file that PHP creates from the form upload.
$message->attach(
Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);
More information on SwiftMail Attachments can be found on this docs page
More information on $_FILES can be found here on w3schools, despite I don't like w3schools, this page is solid.
Another way to do this, using only a single variable for path and filename is:
$message->attach(Swift_Attachment::fromPath('full-path-with-attachment-name'));
Single Attachment
My answer is similar to that of André Catita. However, in Laravel 6 you can use $request instead of $_FILES. Let me simplify the code above:
$path = $request->file('import')->getPathName();
$fileName = $request->file('import')->getClientOriginalName();
$message->attach(
Swift_Attachment::fromPath($path)->setFilename($fileName)
);
Here I assume that the name of your file tag is import. For eg: <input type="file" name="import" />
Multiple Attachments
Now, lets say instead of single attachment you need multiple attachments. Then the code needs to be changed.
First your html code will become: <input type="file" name="import[]" multiple />
And for backend or laravel; code will be:
$files = $request->file('import');
foreach($files as $file){
$path = $file->getPathName();
$fileName = $file->getClientOriginalName();
$message->attach(
Swift_Attachment::fromPath($path)->setFilename($fileName)
);
}
I am using dompdf which seems to be going really well.
I get my generated pdf file as an email attachment. I wanted to know how to append a unique number to the file name so that every users that receives the pdf does not have the same exact file name.
I currently get name the file and get whatever I name have assigned it successfully, such as myfile.pdf.
As a test, I can also get a random number appended to the filename like this:
$message = Swift_Message::newInstance()
->setSubject('My Subject Text') // Message subject
->setTo(array('me#mydomain.com' => 'MyName')) // Array of people to send to
->setFrom(array('no-reply#mydomain.com' => 'SenderName')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'myfile'.rand(10,1000).'.pdf', 'application/pdf')); // Attach the generated PDF from earlier
That works great for sending a pdf with the filename and random number.
But how can I do this with something that makes more sense?
Like starting with a specific number and auto-incrementing for every file that gets submitted?
Or using numbers based on (or from) the SESSION?
What is the best practice that would make sense?
I would like each file name to be myfile_UNIQUENUMBER.pdf (I think some type of auto-increment or SESSION number would be best)
Thoughts?
You may use the date as unique number when use it with d/m/year AND TIME with a name you choose so every filename will have different number..try this code :
$tym = date('m-d-Y : hi a');
$filename = ' APPLICATION '.$tym; // YOU CAN CHOOSE YOUR FILE NAME
$message = Swift_Message::newInstance()
->attach(Swift_Attachment::newInstance($pdf_content, $_POST["username"] . $filename. ".pdf", 'application/pdf')); //