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'));
Related
The way I create my Word document is this :
// I use my templace that's with my files :
$templateProcessor = new TemplateProcessor('Template.docx');
// I fill the template values from an sql query :
$templateProcessor->setValue('titre', $options['titre']);
$templateProcessor->setValue('source', $options['source']);
$templateProcessor->setValue('auteur', $options['auteur']);
$templateProcessor->setValue('date_pub', $options['date_pub']);
$templateProcessor->setValue('contenu', $options['contenu']);
// I give the user the file (I don't fully understand how this works but it does)
header("Content-Disposition: attachment; filename=$title.docx");
$templateProcessor->saveAs('php://output');
The way people recommend attaching files to php mails is the following:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer();
$email->SetFrom('you#example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress#example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();
I have a problem with the PATH_OF_YOUR_FILE_Here part, The code I use for creating the word document just serves it to the user so they download it, but what is the path to it ?
Help is really appreciated, thanks
You need to create the file on your server first in order to attach it to an email. You can delete the file from your server after sending the email.
The header() and php://output are telling the user's browser to download the file, so if you remove the header and change saveAs() to be a real (writeable) path on your server, you should end up with a file that you can then attach to an email.
I'd recommend writing the file to a new location (above your server root) to store these temporary files rather than within the source code. Then ->saveAs('/path/to/file/File.docx'); and $file_to_attach = '/path/to/file/File.docx';.
It just so happens we have two files that are the exact same size that we some times try to send together. We can send anything else we want with these files but as soon as we include both of them it only sends the first one. I'll include my phpmailer code just to be safe but its worked thus far.
$mail = new PHPMailer(true);
$mail->IsSendMail();
$mail->SetFrom($from_addy, $from_name);
foreach(explode(',',$to) as $address1){
foreach(explode(';',$address1) as $address2){
if($address2 != ''){
$mail->AddAddress($address2);
}
}
}
$mail->WordWrap = 70;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $css.$message;
$mail->AltBody = nl2br($message);
$mail->MsgHTML($css.$message);
if(is_array($attachments)){
foreach($attachments as $attachment){
$file = file_get_contents($attachment['tmp_name']);
$mail->AddStringAttachment($file,$attachment['name']);
// I have put checks here and both attachments do make it this far.
}
}
You're not checking the return value from addStringAttachment so you don't know if it's working or not. PHPMailer doesn't throw exceptions for everything, and while you've requested them, you're not catching them anyway. Why read the file and use addStringAttachment - why not read the attachment directly from the file with addAttachment? String attachments are more useful when you need to attach the results of a remote API call, PDF generation etc. Like this:
if (!$mail->addAttachment($attachment['tmp_name'], $attachment['name'])) {
echo "Failed to attach ".$attachment['tmp_name'];
}
Why are you using isSendmail()? It's unlikely you need that.
You've not presented any evidence that it's got anything to do with the file size - it's not as if attachments are indexed by their size value or anything.
You're setting AltBody before calling msgHTML(), which overwrites AltBody. Calling nl2br() on AltBody contents is pointless because AltBody will usually be presented as plain text, so <br> tags will show up.
It looks like your $attachment array may be sourced from $_FILES, in which case it looks you are handling file uploads unsafely. read the PHP docs on that, and look at the "send file upload" example provided with PHPMailer.
Overall, it looks like you've based your code on a very old PHPMailer example, so make sure you're using the latest version, and look at the examples provided with it.
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
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
using PHPMailer's AddAttachment method on PHP 5, it seems I can't get any mail client to decode base64 section.
Practically, receiving the e-mail message with Thunderbird, instead of my attached file "asyF074.tmp", I find attached a "Part1.2" file, which contains section headers (
name="asyF074.tmp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="asyF074.tmp"
) followed by base64 representation of file contents.
I can't figure out what is going wrong with the attachment:
$filename = tempnam ( "" , "asy" );
$f = fopen($filename, "wb");
fwrite($f, $tbl, strlen($tbl));
fclose($f);
/*
* Send mail...
*/
$mail = new phpmailer();
$mail->IsSMTP();
$mail->From = "andrew#" . $domain;
$mail->FromName = "Andrew";
$mail->Host = $mail_host;
$mail->Port = $mail_port;
$mail->AddAddress($mail_destination);
$mail->IsHTML(true);
$mail->Subject = "2011 Polls";
$mail->Body = $msg;
$mail->AddAttachment($filename, "2011-Polls.xls", "quoted-printable", "application/vnd.ms-excel");
$mail->Send(); // send message
unlink($filename);
Also, even if I explicitly set encoding to "quoted-printable", the attachment always comes encoded in base64.
Thanks.
It may not be an answer to your problem but I suggest you try Zend_Mail from the Zend_Framework I've never say problems like these using it.
... found! I don't know why, but it was due to the PHPMailer version I was using (I know what you're thinking: "This guy blew the former download", but I can assure you that I downloaded the proper lib).
I re-downloaded (2 days later) the same library for PHP 5 and now it's working.
Didn't have the time to check, but there was something wrong into the library code.