Using Gmail with Swift Mailer when my server is blocked ?

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Using Gmail with Swift Mailer when my server is blocked ?

Post by geddi »

I am been having problems sending out emails and just found out that my hosting provider is on some "ban lists" . He says he will transfer me to a new server.

In the meantime I want to continue to send out conformation emails which are generated by php when someone completes a form on the website.

My question is - can I load the swift mailer onto my current server and carry out the required tests (smoke tests etc) even when the mail server itself is blocked ?

Can I use my Gmail account to send out emails ?

I am thinking of opening a new Gmail account with a similar name to my website so that I only use it for sending emails from swift mailer.

Have been using this in my code (until the server stopped working)

Code: Select all

// Send email
mail($to, $subject, $message, $headers) ;
 

I picked this code up from an earlier thread.
(of course I will change it abit to suit my script)

But will it work as a replacement to my original code ?
1. <?php
2.
3. //Check if the required fields were sent
4. // Redirect back to the form if not
5. if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
6. || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
7. {
8. //redirect back to form
9. header("Location: ./form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
10. exit();
11. }
12.
13. //Copy into global variables
14. $name = $_POST["sender_name"];
15. $email = $_POST["sender_email"];
16. $title = $_POST["comment_title"];
17. $body = $_POST["comment_body"];
18.
19. //Validate the email address using a regex (I suggest you use a better one than this!!)
20. if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
21. {
22. header("Location: ./form.php?error=invalid_email");
23. exit();
24. }
25.
26. //Check if an attachment was uploaded
27. $file_path = false;
28. $file_name = false;
29. $file_type = false;
30. if (!empty($_FILES["attachment"]["tmp_name"]))
31. {
32. if ($_FILES["attachment"]["error"])
33. {
34. //Redirect if the upload has failed
35. header("Location: ./form.php?error=upload_failed");
36. exit();
37. }
38. $file_path = $_FILES["attachment"]["tmp_name"];
39. $file_name = $_FILES["attachment"]["name"];
40. $file_type = $_FILES["attachment"]["type"];
41. }
42.
43. //Everything looks ok, we can start Swift
44.
45. require_once "lib/Swift.php";
46. require_once "lib/Swift/Connection/SMTP.php";
47.
48. //Enable disk caching if we can
49. if (is_writable("/tmp"))
50. {
51. Swift_CacheFactory::setClassName("Swift_Cache_Disk");
52. Swift_Cache_Disk::setSavePath("/tmp");
53. }
54.
55. //Create a Swift instance
56a. $smtp =& new Swift_Connection_SMTP("smtp.gmail.com", 456, Swift_Connection_SMTP::ENC_TLS);
56b. $smtp->setUsername('your-gmail-username);
56c. $smtp->setPassword('your-gmail-password');
56d. $swift =& new Swift($smtp);
57.
58. //Create the sender from the details we've been given
59. $sender =& new Swift_Address($email, $name);
60.
61. //Create the message to send
62. $message =& new Swift_Message("New comment: " . $title);
63. $message->attach(new Swift_Message_Part($body));
64.
65. //If an attachment was sent, attach it
66. if ($file_path && $file_name && $file_type)
67. {
68. $message->attach(
69. new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
70. }
71.
72. //Try sending the email
73. $sent = $swift->send($message, "your@address.tld", $sender);
74. //Disconnect from SMTP, we're done
75. $swift->disconnect();
76.
77. if ($sent)
78. {
79. header("Location: ./success.php");
80. exit();
81. }
82. else
83. {
84. header("Location: ./form.php?error=sending_failed");
85. exit();
86.


I am complete newbie to this but hav spent a couple of hours reading threads.

Would really appreciate any guidance.
Thanks
Dave
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Using Gmail with Swift Mailer when my server is blocked ?

Post by Chris Corbyn »

Using Gmail will work fine provided you don't intend to send more than 500 emails per day. You need to enable TLS encryption if using Gmail but that's covered on the SMTP conneciton page in the documentation :)
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Re: Using Gmail with Swift Mailer when my server is blocked ?

Post by geddi »

Thanks for your fast reply.

I read the docs.

I am cutting out all the stuff I dont need and I end up with this:

Code: Select all

 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
//Create a Swift instance and connect to Gmail (PHP5)
 $swift = [color=#BF0040]new[/color] Swift(new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
 
 $smtp->setUsername('your-gmail-username);
 $smtp->setPassword('your-gmail-password');
 $swift =& [color=#BF0040]new [/color]Swift($smtp);
 
//Try sending the email
 $sent = $swift->send($message, "$email", $sender);
 
 //Disconnect from SMTP, we're done
 $swift->disconnect();
 
Just a couple of questions: ( hope you dont mind ).

The above code seems to be creating two new instances of the swift object.
One is before the user and password info and one after.
Is this correct ?


Or should I put the user and password info first like this :

( you can probably tell I dont do much OOP :oops: )

Code: Select all

 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
 $smtp->setUsername('your-gmail-username);
 $smtp->setPassword('your-gmail-password');
 
//Create a Swift instance and connect to Gmail (PHP5)
 $swift = new Swift(new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
 
 
//Try sending the email
 $sent = $swift->send($message, "$email", $sender);
 
 //Disconnect from SMTP, we're done
 $swift->disconnect();
 
second question:
And I just noticed that before instead of PORT_SECURE
we used either "456" .
Is this an insecure port for Gmail ?

I assume that I should use the secure method ?

Thanks very much.
Dave
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Using Gmail with Swift Mailer when my server is blocked ?

Post by Chris Corbyn »

465 is gmail's secure port (not 456 ;))

You need to create $smtp before you can call any methods on it...

Code: Select all

//Create a Swift instance and connect to Gmail (PHP5)
 $smtp = new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
 
 $smtp->setUsername('your-gmail-username');
 $smtp->setPassword('your-gmail-password');
 
 $swift = new Swift($smtp);
 
:)
Post Reply