?
Solved

PHP:  Email a PDF from a URL

Posted on 2013-10-28
6
Medium Priority
?
1,189 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

771 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