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