Solved

PHP:  Email a PDF from a URL

Posted on 2013-10-28
6
1,122 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
Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php hashing methods 3 48
MySQL 6 47
Calling an MySQL Stored Prodecure from a php function with input and output parameters 6 48
Form not executing correctly 1 28
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
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 look for a specific file type in a local or remote server directory using PHP.

734 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