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

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
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

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

Post 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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Bad SMTP connection? You sure you using the correct server, port, and credentials?
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

Post by FuzzyLogik »

that's what I used in the test configuration, and it worked fine (I actually received the email)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

Post 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]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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();
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

Post 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();
}
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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(); 
} 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ahem... relative header() based redirection.... ;)
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

OK, the SMTP process looks to be working fine. Where does $body come from in your code?
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

Post by FuzzyLogik »

well, it's actually a post variable, $message, but it looks like swift is using that, so i'll make that $body
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

FuzzyLogik wrote:What is the proper syntax for absolute redirects? Just http://www.phazm.com/contact.php?
Yes.
Post Reply