Page 1 of 1

gmail - send form

Posted: Thu Feb 21, 2008 5:58 pm
by sloman
Hi. Great project but i am an absolute beginer and i would like to have a contact form on my freehostia account and recive mail with gmail SMTP.

I found a form allready made in document's area.

But i dont know how to configure the code to sent message to my gmail account.

I honestly tried these myself for 2 days but awerything i did nothing worked.

Code: Select all

<?php
 
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
    || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
{
    //redirect back to form
    header("Location: ./form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
    exit();
}
 
//Copy into global variables
$name = $_POST["sender_name"];
$email = $_POST["sender_email"];
$title = $_POST["comment_title"];
$body = $_POST["comment_body"];
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: ./form.php?error=invalid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
//Everything looks ok, we can start Swift
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
//Create a Swift instance
$swift =& new Swift(new Swift_Connection_SMTP("your_smtp_server.tld"));
 
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
//Try sending the email
$sent = $swift->send($message, "your@address.tld", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
    header("Location: ./success.php");
    exit();
}
else
{
    header("Location: ./form.php?error=sending_failed");
    exit();
 
The code is copy/paste from your documentation area. I didn't change enything beacouse i really tried everything i could think off and nothing worked for me.

Colud somebody please help me and tell me what to change so that the form will work with gmail SMTP? I tried with averything in document area but didn't get it to work.

Edit: I know that i must do something after line 45. But nothing i did worked for me.

Re: gmail - send form

Posted: Thu Feb 21, 2008 8:56 pm
by Chris Corbyn
What version of PHP are you using?

This line needs changing:

Code: Select all

$swift =& new Swift(new Swift_Connection_SMTP("your_smtp_server.tld"));
You'll need to do:

Code: Select all

$smtp =& new Swift_Connection_SMTP("smtp.gmail.com", 456, Swift_Connection_SMTP::ENC_TLS);
$smtp->setUsername('your-gmail-username);
$smtp->setPassword('your-gmail-password');
$swift =& new Swift($smtp);
If you're using PHP4 however, ths will differ slightly in that Swift_Connection_SMTP::ENC_TLS will become SWIFT_SMTP_ENC_TLS.

Re: gmail - send form

Posted: Fri Feb 22, 2008 4:20 pm
by sloman

Code: Select all

<?php
 
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
    || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
{
    //redirect back to form
    header("Location: ./form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
    exit();
}
 
//Copy into global variables
$name = $_POST["sender_name"];
$email = $_POST["sender_email"];
$title = $_POST["comment_title"];
$body = $_POST["comment_body"];
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: ./form.php?error=invalid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
//Everything looks ok, we can start Swift
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
//Create a Swift instance
$smtp =& new Swift_Connection_SMTP("smtp.gmail.com", 456, Swift_Connection_SMTP::ENC_TLS);
$smtp->setUsername('username@gmail.com');
$smtp->setPassword('password');
$swift =& new Swift($smtp);
 
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
//Try sending the email
$sent = $swift->send($message, "username@gmail.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
    header("Location: ./success.php");
    exit();
}
else
{
    header("Location: ./form.php?error=sending_failed");
    exit();
}
Doesnt work. :cry: Connection time out error.

-I switched to php5 on freehostia account
- download php5 swiftmailer
- Saved 3 files for form with exact names and folder /lib from swiftmailer as suggested in tutorial and put on server
-I double cheked folder and files paths and input data as suggested but no luck.

Does code look ok? I hate to bother u with my noob questions but i am not able to do these my self.

Re: gmail - send form

Posted: Mon Mar 03, 2008 10:27 am
by geddi
Hi Sloman,Didi you get your code working ?
The reason I ask is because The server I am using has a blocked memail server and so I am thinking of routing it through gmail instead. Does it work ok ?

Thanks :)

Re: gmail - send form

Posted: Mon Mar 03, 2008 6:27 pm
by Chris Corbyn
If your web host are blocking traffic on port 465 then this won't work. What web host are you with?

Two I know it won't work with:

GoDaddy (shared hosting only)
1and1 (shared hosting only)

Shared hosts suck for mailing :(