Solved

PHP:  Email a PDF from a URL

Posted on 2013-10-28
6
1,144 Views
Last Modified: 2013-10-28
Hi All,

Our intranet has a PDF report that is created every time its accessed.

To save on server resources I want to email it to several users at 2am every night.

The server has Xapps installed and I've tested PHP mail(), but cant work out how to attach the PDF from a URL


All help is welcome.


Thanks in advance
D
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 39605942
You'll want to download the PDF to the local disk first, then attach it from there.

This can be done with Curl.

After the email is sent successfully, you can then remove the PDF from the disk.
0
 
LVL 2

Author Comment

by:detox1978
ID: 39605949
can you give me a hand with the code?


thanks
0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 39605962
Sure, is the internal location 80/TCP or 443/TCP?  Is the PDF download a GET or POST request?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 250 total points
ID: 39605974
Here's an example GET request over HTTP:

$ch = curl_init("http://www.example.com/report.pdf");
$fp = fopen("report.pdf", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

Open in new window


Or, if fopen wrappers are enabled in your PHP conf:

$file = 'http://example.com/report.pdf'; // URL to the file
$contents = file_get_contents($file); // read the remote file
touch('report.pdf'); // create a local EMPTY copy
file_put_contents('report.pdf', $contents);

Open in new window


You'll then attach the local report.pdf to your mail function.  

$attachment = chunk_split(base64_encode(file_get_contents('report.pdf')));

Open in new window


You could add extra code to verify a minimum file size, etc. before proceeding to email.

You may want to consider PHPMailer or Swiftmailer as well.
0
 
LVL 10

Assisted Solution

by:Damjan
Damjan earned 250 total points
ID: 39606013
I use this code for sending pdf files and it works fine:

<?php  
$fileatt = "mypdffile.pdf"; // Path to the file                  
$fileatt_type = "application/pdf"; // File Type  
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment  
$email_from = "sales@mysite.com"; // Who the email is from  
$email_subject = "Your attached file"; // The Subject of the email  
$email_message = "Thanks for visiting mysite.com!  Here is your free file.<br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it  
$email_to = "to@mysite.com"; // Who the email is to  
$headers = "From: ".$email_from;  
$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file);  
$semi_rand = md5(time());  
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
    
$headers .= "\nMIME-Version: 1.0\n" .  
            "Content-Type: multipart/mixed;\n" .  
            " boundary=\"{$mime_boundary}\"";  
$email_message .= "This is a multi-part message in MIME format.\n\n" .  
                "--{$mime_boundary}\n" .  
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" .  
               "Content-Transfer-Encoding: 7bit\n\n" .  
$email_message .= "\n\n";  
$data = chunk_split(base64_encode($data));  
$email_message .= "--{$mime_boundary}\n" .  
                  "Content-Type: {$fileatt_type};\n" .  
                  " name=\"{$fileatt_name}\"\n" .  
                  //"Content-Disposition: attachment;\n" .  
                  //" filename=\"{$fileatt_name}\"\n" .  
                  "Content-Transfer-Encoding: base64\n\n" .  
                 $data .= "\n\n" .  
                  "--{$mime_boundary}--\n";  
$ok = @mail($email_to, $email_subject, $email_message, $headers);  
if($ok) { 
echo "<font face=verdana size=2><center>You file has been sent<br> to the email address you specified.<br> 
Make sure to check your junk mail!<br>
Click <a href=\"#\" onclick=\"history.back();\">here</a> to return to mysite.com.</center>";
} else {  
die("Sorry but the email could not be sent. Please go back and try again!");  
}  
?>

Open in new window

Source: http://www.daniweb.com/web-development/php/threads/60795/send-email-with-attachment

Update: Sorry, I missed that you want attach pdf from url...
0
 
LVL 2

Author Closing Comment

by:detox1978
ID: 39606214
I used the second part of x66_x72_x65_x65 to download the PDF and damjanholsedl's to email it.

many thanks
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question