Technology Blog

Widgets

Archive for the ‘PHP’ Category

Sending mail in PHP - avoid sending to junk folder

We all know that PHP is great for back-end development. However, some functions can be tricky to fine tune for the best web development possible. The mail() function has a high probability of sending mail to user’s “junk” or spam folders because the connection was not authenticated. To avoid that, use the following function below. This assumes you have PEAR setup and working properly.

  1.  
  2. <?php
  3.  require_once "Mail.php";
  4.  
  5.  $from = "Sandra Sender <sender@example.com>";  $to = "Ramona Recipient <recipient@example.com>";  $subject = "Hi!";  $body = "Hi,\n\nHow are you?";
  6.  
  7.  $host = "mail.example.com";
  8.  $username = "smtp_username";
  9.  $password = "smtp_password";
  10.  
  11.  $headers = array (‘From’ => $from,
  12.    ‘To’ => $to,
  13.    ‘Subject’ => $subject);
  14.  $smtp = Mail::factory(’smtp’,
  15.    array (‘host’ => $host,
  16.      ‘auth’ => true,
  17.      ‘username’ => $username,
  18.      ‘password’ => $password));
  19.  
  20.  $mail = $smtp->send($to, $headers, $body);
  21.  
  22.  if (PEAR::isError($mail)) {
  23.    echo("<p>" . $mail->getMessage() . "</p>");
  24.   } else {
  25.    echo("<p>Message successfully sent!</p>");
  26.   }
  27.  ?>
  28.  
  29.  

PHP Pear File_PDF Install and Example

I personally like this package to create dynamic PDF files with PHP; however, the install instructions and documentation are horrible.

First of all, you need to have Pear installed
(open a cmd window on your server and run go-pear.bat from your PHP install directory)

Next to get File_PDF installed and working:
1. open a cmd window on your server
2. type: pear channel-update pear.php.net (hit enter)
3. type: pear upgrade-all (hit enter)
4. type: pear install HTTP_Download-1.1.3 (hit enter)
5. type: pear install File_PDF-0.3.2 File_PDF (hit enter)

That should get File_PDF installed along with all the upgraded dependencies

Now some code example:

  1.  
  2. <?php
  3. require (‘File/PDF.php’);
  4. $p = &File_PDF::factory(‘P’, ‘mm’, ‘A4′);
  5. $p->open();
  6. $p->setMargins(50, 50);
  7. $p->addPage(‘P’);
  8. $p->setFont(‘arial’, , 15);
  9. $p->write(10, ‘This is some text’);
  10. $p->text(10, 20, ‘Hello’);
  11. $p->close();
  12. $p->output(‘hello.pdf’);
  13. ?>
  14.  

Enjoy!

© ZADRO Solutions, Inc. All Rights Reserved. Disclaimer | Privacy Policy
RSS Facebook Digg Delicious Stumble Upon Twitter