phpmailer file attachment delivered, but downloads a string only - php

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

Related

using PHP to remove the extension from a file and then downloading it

I recently had a asked a question very similar to this one, however after evaluating that I did not explain it in the best way I have come back once again explaining it in a greater manner.
So, I am creating a system that will gather data from a MySQL database and use a unique id to download a file, however depending on the value of a column within that database called type, this file could be anything from a png file to an xml file. What I am currently doing is trying to download these files WITHOUT any extension.
As an example to maybe make this easier to understand, a file named image.png would be converted to just image and then downloaded.
With this you could rename the file to image.png again on the local machine and view the image.
This may seem very inefficient to most reading this but for my current situation it's all that will work.
How could I remove a files extension and then download it? (in php)
Thank you in advance.
Just use headers to specify response type.
$filepath = '/wherever/the/file/is.png';
$filename = 'new-cool-name';
header('Content-Type: whatever/content-type-is');
header("Content-disposition: attachment;filename=$filename");
readfile($filepath);
This basically sends a response with specified content-type as an attachment and the body of the attachment contains the file contents. If you never sure what's the content type is, then just use application/octet-stream
Usually when you set out to push a file for downloading from a serverside script, you do so by utilizing http headers like https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
The filename of the downloadable file is specified in that header
Okay so to remove an extention from a file you could do is
$withoutExtion = preg_replace('/\\.[^.\\s]{3,4}$/', '', $youfilename);
...followed by your file download code

PHP output CSV to email

I have a script that currently takes some data from a wordpress DB and then loops over the returned rows and uses fputcsv(). The file handle was setup with:
$fh = fopen('php://output', 'w');
The script is linked to a link of a webpage and when you click the link it downloads the CSV (using the content-disposition header).
Is it possible to write to php://output this CSV file and then use PHP's mail() function to send it in an attachment?
I have a mail function I've written that will set the MIME type to multipart/mixed, I'm just not sure how to create the actual attachment to be emailed.
Use tmpfile() instead. Write your CSV data to that temp file, then attach it to your email.
Plus, don't write your own mime handling/generating functions. Use a library like PHPMailer or Swiftmailer to do it for you. Far easier and far more reliable. Part of their attachment handling code allows you specify the filename the user sees, so even though it might be "/tmp/abc123def", it'll show up as "data.csv" (or whatever you specify) in the actual email.

Email image stored as a variable with mosMail()

I would like to send an email with an image attachment using mosMail(). Everything works perfectly when I attach a file located on the hard drive, but because the image that I want to attach is generated on the fly, I would rather not have to store it. Is it possible to attach an image stored in a variable?
$attachment = $im;
mosMail(..., $attachment);
You can't, but the underlying mailer library is PHPMailer. Simply include this and use it directly. You will have to reproduce how mail sending configuration settings are read from Joomla's configuration, simply copy and paste from MosMail

PHPmailer's AddEmbeddedImage method not working

I'm trying to test the PHPmailer class to embed image (http://www.google.gr/intl/en_com/images/srpr/logo1w.png) inside an e-mail
I'm using this code (along with standard one, that 100% works, mail is delivered):
$mail->AddEmbeddedImage($src, 'test', basename($src));
and this is placed the e-mail body:
<img src="cid:test">
Image is not showing up. What may I doing wrong?
Taken from some piece of the documentation:
$path is the path of the filename. It can be a relative one (from your
script, not the PHPMailer class) or a full path to the file you want
to attach.
Have you tried using a local image?
cid:test isn't valid url for image.

Retrieving a file name to attach to an email with SwiftMailer and PHP

I am able to attach a file using SwiftMailer with its name hardcoded.
But what if the file is uploaded by a user from an HTML form's 'file input type control and has to be sent with an email by a PHP script?
How do I specify the file name in SwiftMailer?
Thank you!
I'm pretty sure in this case you could use the $_FILES['file']['tmp_name'] value. This gives the path to the temporary uploaded file.

Categories