I was just wondering if I could have a variable to hold an image, I'm using phpmailer to send email and I need an image to be attached to it,
so I was wondering if I could put the image in a variable and use
$mailer->AddAttachment($image);
to send the email with attachment.
thanks for your help.
With PhpMailer adding an attachment is done the way you wrote it in the question
$mailer->AddAttachment('/home/mywebsite/file.jpg', 'file.jpg');
If you want to use a variable you can change the string by a variable without problem.
$imagePath = '/home/mywebsite/file.jpg';
imageName = 'file.jpg'
$mailer->AddAttachment($imagePath, $imageName);
I guess $image should contain local path to the image file.
If you look at phpMailer source, at line 1218:
http://phpmailer.svn.sourceforge.net/viewvc/phpmailer/phpmailer/trunk/class.phpmailer.php?revision=444&view=markup
you'll see that it verifies at first that what you have given is path to existing file. There is no other option.
Unless I'm missing something, that's exactly how it's supposed to be used.
According to this document, you'd do something like this:
$myImg = '/some/path/to/image.jpg';
$mailer->AddAttachment($myImg);
Is that not what you're trying?
Why cant you do this this way? Sending email attachments in PHP Using phpmailer class !
Related
I am sending email using PHPmailer and tying to embed image in mail body (using CID method <img src="cid:qrcode" />) but it always attaching image instead of embedding.Can anyone tell me what's wrong with my code (commented lines in code, already tried. ).
Here is the screenshot of my code
First of all you're using a very old version of PHPMailer, and have based your code on a very old example. Get the latest.
The other obvious problem is that while you're putting HTML into Body, you've commented out the call to isHTML(), so your message is being sent as plain text, which has no concept of displaying images inline. Uncomment this line:
$mail->isHTML();
Also bear in mind that in MIME there is essentially no difference between attachments and inline images - everything is an attachment, it's just that some attachments may be referred to from HTML parts, and HTML-capable clients can make use of that linkage.
If Outlook is removing src attributes, that's clearly not your sending code's problem. Outlook does some very unpleasant things to email.
One other minor thing: instead of dirname(__FILE__) you can use __DIR__ in any current version of PHP.
just as an assistance I encountered the same problem and after much mumbling arrived at a solution which might help others.
Tried dumping the image in the same folder - nope
Tried DIR variable to dynamicalyly pull in the image - nope
Finally, hardcoded the path to the folder in which my embedded image lived, hurrah
Magic forumla for me (using PHPmailer v5.5) - note I'm using Plesk so your definitive path may differ, use mine as a guide...
$mail->AddEmbeddedImage("/var/www/vhosts/{domainname}/httpdocs/{foldername)/image.jpg", "emailimg", "image.jpg");
I note that when calling in the image as an embedded image that i had to use the same filename as I think PHPmailer uses the structure:
embedded-img-name source, reference id, embedded-img-name within the internal AddEmbeddedImage call
Hope it helps someone!
Try this code i think it will work for you
$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');
and on the tag put src='cid:logo_2u'
I'm using PHPMailer to send automatic email (obviously using PHP as programming language).
I would like to add a PDF attachment generated with TCPdf.
Unfortunately I cannot generate the PDF inside the php page where I'm using PHPMailer, and I cannot create a function that generate it.
I can only use a link to generate it, like this:
www.mypage.com/app-pdf/link_generate_pdf.php?IDToGenerate=131&PDFOutput=I
I was thinking that I can recall the page with the PDFOutput=S and "return" in some way the text of the PDF and add it as attachment.
Otherwise I can call the page with PDFOutput=F and save it to a temp folder and then attach it to the email.
The problem is that I don't know how to "call" a page as it were a function and return what the recalling page actually returns.
Do you have some suggestion?
Thank you
edit: I now understand the problem! The problem is that the URL is accessible only from autentication (login page). I thought that as I was logged in, the script was automatically capable of read the page. How can I solve this?
Use this to save the file on the server first and then attach
file_put_contents("Tmpfile.pdf", fopen("http://example.com/file.pdf", 'r'));
Attach as
$mail->AddAttachment('path_to_pdf/Tmpfile.pdf', $name = 'Name_of_pdf_file',
$encoding = 'base64', $type = 'application/pdf');
Hope this helps
Edit: Try this. works fine over here
file_put_contents("path_to_pdf/Tmpfile.pdf",
file_get_contents("http://example.com/file.pdf"));
I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!
I am having some issues with trying to get SwiftMailer to attach a file I have created with FPDF. Basically I have a page called createPDF.php that is dynamically generated based on the ID number in the URL. This page is set to output the PDF inline using $pdf->Output("filename.pdf",I);. What I want to do is to be able to attach this file to an email using SwiftMailer from another page simply by calling my createPDF.php?id=xxx link.
From the PHP page where I want to send the email from, everything works, except the attachment. It attaches something, but not what I want and it is not viewable in a PDF viewer on my local machine. The line specific to the attaching the file is:
->attach(Swift_Attachment::fromPath('createPDF.php?id=xxxx'))
This does not work, but surely, it must be possible without saving the file on my web server by FPDF.
Is this possible? If so, how?
Thanks!
The problem here is Swiftmailer gets the file contents, it does not execute your php file. So the contents of your PDF will the code that is in createPDF.php.
why cant you safe the file first? You should be able to safe it and delete it when your email is sent.
<?php
$id = "xxx";
$fileName = "tmp/".sha1(time()+mt_rand(0,99999999));
include "createPDF.php"; //saves it to $fileName
->attach(Swift_Attachment::fromFile( $fileName )->setFilename('blaha.pdf'));
unlink($fileName);
Ok, so I just figured this out.
Basically I made a new PHP file with the bulk of my createPDF.php file as a function and simply passed in two variables into the function as my $id and an $output variable. $output is simply the way that FPDF outputs the file — inline, etc... I then set the function to return the output of the FPDF. In my createPDF.php file I simply call my function passing in $id and 'I' as the variables so it displays the correct PDF inline in the browser.
In my sendEmail function I simply pass in $id and 'S' and set it to a variable $content, which I pass into SwiftMailer as an attachment.
Works great.
Thanks for your help!
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.