Showing posts with label SMTP. Show all posts
Showing posts with label SMTP. Show all posts

Saturday, October 18, 2014

PHP SMTP GoDaddy Email Alternative: SendGrid

If you have ever worked with GoDaddy's SMTP relay email service, you will find it sometimes just quits working. I know GoDaddy is not about being a reliable company known for quality so it very silly of me to complain. However, I kind of need to send out emails from a website as needed, NOT just when GoDaddy thinks it is needed.
I finally was fed up with this terrible service. And Gmail, which I was trying to use as an alternative, just up and deleted my email account for whatever reason, so I searched out an alternative. I found SendGrid. For a basic 'FREE' account, I can send up to 200 emails per day. Which is definitely good enough for me - I am not sending spam, I am sending out an occasional newsletter to a subscribed membership!
For PHP, you need at least 5.3, I believe. You can download their code package to use or replace your SMTP credentials.
Here is a sample using their code package:
  1. include('sendgrid-php/sendgrid-php.php');
  2. function sendEmailSendGrid($subject, $message, $to, $fileName){
  3.         
  4.      $sendgrid = new SendGrid('myusername', 'mypassword');
  5.      $to = trim($to);
  6.      $htmlMessage = "<html><body>".$message."</body></html>";
  7.      $from='junk@junk.com';
  8.     
  9.      $email = new SendGrid\Email();
  10.      $email->addTo($to)->
  11. setFrom($from)->
  12. setSubject($subject)->
  13. setText($message)->
  14. setHtml($htmlMessage);
  15. if($fileName != "")
  16.     $email->addAttachment($fileName);
  17.     
  18. return $sendgrid->send($email);
  19.     
  20. }
Otherwise replace your SMTP stuff like this:
  1. function sendEmailSendGrid($subject, $message, $to, $rid, $nid){
  2.     
  3.      $to = trim($to);
  4.     
  5.      $htmlMessage = "<html><body>".$message."</body></html>";
  6.     
  7.      $host = "smtp.sendgrid.net";
  8. $port = "587";
  9. $username = "username";
  10. $password = "password";
  11. $crlf = "\n";
  12.     
  13. $smtp = Mail::factory('smtp',
  14. array ('host' => $host,
  15. 'port' => $port,
  16. 'auth' => true,
  17. 'username' => $username,
  18. 'password' => $password));
  19.         
  20.     $from='junk@junk.com';
  21.                  
  22. $headers = array ('From' => $from,
  23. 'To' => $to,
  24. 'Subject' => $subject,
  25. 'Content-type' => 'text/html; charset=iso-8859-1');
  26. $mail = $smtp->send($to, $headers, $htmlMessage);
  27. if (PEAR::isError($mail)) {
  28. echo("<p>" . $mail->getMessage() . "</p>");
  29. } else {
  30. echo("<p>Message successfully sent to: ".$to."</p>");
  31. }
  32. }

Saturday, July 9, 2011

Struggling with PHP SMTP/Email?

Well, as I am diving into the world of PHP (coming from a Microsoft world), I have found setting up the PHP environment to be rather frustrating for myself. With MS, you install Visual Studio, use the built in web server or optionally hook up to the local IIS and you're off for the most part.

However, PHP, in my experience, isn't so ready to go. The installation of extra stuff can be quite a headache, but it is open source/free so you learn to just deal with it I suppose.

So I wanted to get a small program to send email. From my reading, I needed to install PEAR. I found this article to be the most helpful, easy instructions for those on a Windows system. http://www.geeksengine.com/article/install-pear-on-windows.html

I later found I needed to two more installs the Net_SMTP and the Net_Socket, it said Net_SMTP is optional, but I think I needed it.

To install these, I follow the same idea, I download the archive file, then unzip it to a local folder, copy the needed file/folder located inside to the PEAR directory which is inside the PHP install directory.

Extra installs:

http://pear.php.net/package/Net_SMTP/

http://pear.php.net/package/Net_Socket/

For the Net_SMTP, I copied SMTP.php to the NET folder (you many need to create the NET folder)

And then for the Net_Socket, I copied the Socket.php to the NET folder.

I can't quite be sure this are the precise/correct instructions, but I do know I was able to successfully send email!