Page 1 of 1

It say's it sent fine, but it didn't.

Posted: Sun Jul 01, 2007 5:58 am
by FuzzyLogik
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is my first time using SwiftMailer.

I am sending my emails through SMTP, and my script is saying that it's sending the message fine (it gets to if($sent)) but it's not (I actually changed the password to an incorrect password, and it still said it sent it fine)

Here is the relevant code:

Code: Select all

require_once "swift/lib/Swift.php";
require_once "swift/lib/Swift/Connection/SMTP.php";
require_once "swift/lib/Swift/Authenticator/LOGIN.php";
 
//Create a Swift instance
$smtp =& new Swift_Connection_SMTP("mail.phazm.com", 25);
$smtp->setUsername("myuser");
$smtp->setPassword("mypass");

$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 Message");
$message->attach(new Swift_Message_Part($body));

//Try sending the email
$sent = $swift->send($message, "nospam@phazm.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
    header("Location: ./contact.php?status=sent");
    exit();
}
else
{
    header("Location: ./contact.php?error=sending_failed");
    exit();
}
Does anyone know what could be happening?

Thank you,

- Jon


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 01, 2007 6:14 am
by superdezign
Bad SMTP connection? You sure you using the correct server, port, and credentials?

Posted: Sun Jul 01, 2007 6:59 am
by FuzzyLogik
that's what I used in the test configuration, and it worked fine (I actually received the email)

Posted: Sun Jul 01, 2007 7:59 am
by Chris Corbyn
FuzzyLogik wrote:that's what I used in the test configuration, and it worked fine (I actually received the email)
Could you run this for me? That's if the error doesn't stop it displaying the log.

Code: Select all

$swift =& new Swift($connection);
$swift->log->enable();

$swift->send( ... ); //Pseudo, send emails in the normal way

echo "<pre>", $swift->log->dump();
//Thinking out loud... it would be great if the log was dumped with the error stack trace when logging is enabled :idea: I'll add this to 3.3.

Posted: Sun Jul 01, 2007 9:28 am
by FuzzyLogik
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Catchable fatal error: Argument 1 passed to Swift::__construct() must implement interface Swift_Connection, null given, called in /home/phazm/public_html/phazm/processform.php on line 52 and defined in /home/phazm/public_html/phazm/swift/lib/Swift.php on line 85

It's possible I added this to the wrong spot, however...

Code: Select all

//Create the message to send
$message =& new Swift_Message("PZMAIL -- New Message");
$message->attach(new Swift_Message_Part($body));

$swift =& new Swift($connection);
$swift->log->enable(); 

//Try sending the email
$sent = $swift->send($message, "nospam@phazm.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
 
 echo "<pre>", $swift->log->dump();
 
if ($sent)

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 01, 2007 10:16 am
by Chris Corbyn
Sorry, I meant for you to replace $connection with whatever connection you're curently using ;)

Also, if you're using PHP5 you can definitely dump the log:

Code: Select all

try {
  //Create the message to send 
  $message =& new Swift_Message("PZMAIL -- New Message"); 
  $message->attach(new Swift_Message_Part($body)); 

  $swift =& new Swift($connection); 
  $swift->log->enable(); 

  //Try sending the email 
  $sent = $swift->send($message, "nospam@phazm.com", $sender); 
  //Disconnect from SMTP, we're done 
  $swift->disconnect(); 
} catch (Exception $e) {
  echo "Error caught<br />";
}  
  
 echo "<pre>", $swift->log->dump();

Posted: Sun Jul 01, 2007 1:42 pm
by FuzzyLogik
hm.. same error:

Catchable fatal error: Argument 1 passed to Swift::__construct() must implement interface Swift_Connection, null given, called in /home/phazm/public_html/phazm/processform.php on line 53 and defined in /home/phazm/public_html/phazm/swift/lib/Swift.php on line 85


here's my complete code:

Code: Select all

<?
if (empty($_POST["fname"]) || empty($_POST["lname"])
    || empty($_POST["email"]) || empty($_POST["message"]))
{
    header("Location: ./contact.php?error=blank");
	exit();
}
if ($passed != "yes")
{
    header("Location: ./contact.php?error=nopass");
	exit();
}
$fname = $_POST["fname"];
$email = $_POST["lname"];
$email = $_POST["email"];
$message = $_POST["message"];
 
//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_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $email))
{
    header("Location: ./contact.php?error=invalid_email");
    exit();
}

require_once "swift/lib/Swift.php";
require_once "swift/lib/Swift/Connection/SMTP.php";
require_once "swift/lib/Swift/Authenticator/LOGIN.php";

/*Enable disk caching if we can
if (is_writable("swift/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("swift/tmp");
}
*/
$name = "$fname $lname"; 
 
//Create a Swift instance
$smtp =& new Swift_Connection_SMTP("mail.phazm.com", 25);
$smtp->setUsername("myuser");
$smtp->setPassword("mypass");

$swift =& new Swift($smtp);

//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
try {
  //Create the message to send
  $message =& new Swift_Message("PZMAIL -- New Message");
  $message->attach(new Swift_Message_Part($body));

  $swift =& new Swift($connection);
  $swift->log->enable();

  //Try sending the email
  $sent = $swift->send($message, "nospam@phazm.com", $sender);
  //Disconnect from SMTP, we're done
  $swift->disconnect();
} catch (Exception $e) {
  echo "Error caught<br />";
} 
 
 echo "<pre>", $swift->log->dump();
 
if ($sent)
{
    header("Location: ./contact.php?status=sent");
    exit();
}
else
{
    header("Location: ./contact.php?error=sending_failed");
    exit();
}
?>

Posted: Sun Jul 01, 2007 2:20 pm
by Chris Corbyn
Nope, still not right, read the code rather than just copying and pasting it ;)

Anyway, I'll do it for you since you posted the full script :)

PS: Stop using short_open_tags (i.e. <? ?> rather than <?php ?>)

Code: Select all

<?php

if (empty($_POST["fname"]) || empty($_POST["lname"]) 
    || empty($_POST["email"]) || empty($_POST["message"])) 
{ 
    header("Location: ./contact.php?error=blank"); 
        exit(); 
} 
if ($passed != "yes") 
{ 
    header("Location: ./contact.php?error=nopass"); 
        exit(); 
} 
$fname = $_POST["fname"]; 
$email = $_POST["lname"]; 
$email = $_POST["email"]; 
$message = $_POST["message"]; 
  
//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_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $email)) 
{ 
    header("Location: ./contact.php?error=invalid_email"); 
    exit(); 
} 

require_once "swift/lib/Swift.php"; 
require_once "swift/lib/Swift/Connection/SMTP.php"; 
require_once "swift/lib/Swift/Authenticator/LOGIN.php"; 

/*Enable disk caching if we can 
if (is_writable("swift/tmp")) 
{ 
    Swift_CacheFactory::setClassName("Swift_Cache_Disk"); 
    Swift_Cache_Disk::setSavePath("swift/tmp"); 
} 
*/ 
$name = "$fname $lname"; 
  
//Create a Swift instance 
$smtp =& new Swift_Connection_SMTP("mail.phazm.com", 25); 
$smtp->setUsername("myuser"); 
$smtp->setPassword("mypass"); 

//Create the sender from the details we've been given 
$sender =& new Swift_Address($email, $name); 
  
try { 
  //Create the message to send 
  $message =& new Swift_Message("PZMAIL -- New Message"); 
  $message->attach(new Swift_Message_Part($body)); 

  $swift =& new Swift($smtp); 
  $swift->log->enable(); 

  //Try sending the email 
  $sent = $swift->send($message, "nospam@phazm.com", $sender); 
  //Disconnect from SMTP, we're done 
  $swift->disconnect(); 
} catch (Exception $e) { 
  echo "Error caught<br />"; 
} 
  
 echo "<pre>", $swift->log->dump(); 
  
if ($sent) 
{ 
    header("Location: ./contact.php?status=sent"); 
    exit(); 
} 
else 
{ 
    header("Location: ./contact.php?error=sending_failed"); 
    exit(); 
} 
?>

Posted: Sun Jul 01, 2007 2:29 pm
by feyd
ahem... relative header() based redirection.... ;)

Posted: Sun Jul 01, 2007 3:02 pm
by FuzzyLogik
thanks for that... sorry for not reading the code very carefully.

Here's what I got:

Code: Select all

++ Enabling logging
>> MAIL FROM: 
<< 250 OK
>> RCPT TO: 
<< 250 Accepted
>> DATA
<< 354 Enter message, ending with "." on a line by itself
>> 
>> 
.
<< 250 OK id=1I55bZ-0005Op-Hn
++ Message sent to 1/1 recipients
>> QUIT
<< 221 gator104php5sql5.hostgator.com closing connection

Warning:  Cannot modify header information - headers already sent by (output started at /home/phazm/public_html/phazm/processform.php:63) in /home/phazm/public_html/phazm/processform.php on line 67

Then I got a blank email in my inbox :)

feyd:

What is the proper syntax for absolute redirects? Just http://www.phazm.com/contact.php?

Posted: Sun Jul 01, 2007 3:32 pm
by Chris Corbyn
OK, the SMTP process looks to be working fine. Where does $body come from in your code?

Posted: Sun Jul 01, 2007 3:34 pm
by FuzzyLogik
well, it's actually a post variable, $message, but it looks like swift is using that, so i'll make that $body

Posted: Sun Jul 01, 2007 5:15 pm
by superdezign
FuzzyLogik wrote:What is the proper syntax for absolute redirects? Just http://www.phazm.com/contact.php?
Yes.