Page 1 of 1

[SOLVED] Sending mail with Swift

Posted: Tue Jun 20, 2006 8:54 am
by NiGHTFiRE
Hey,
This is my code but it won't send out any emails. What's wrong?

Code: Select all

<?php 
session_start(); // Alltid överst på sidan 
error_reporting(E_STRICT);
 
// Kolla om inloggad = sessionen satt 
if (!isset($_SESSION['sess_user'])){ 
  header("Location: index.php"); 
  exit; 
} 
?>
<?php include '../meny.php'; ?>
<?php include '../meny_mail.php'; ?>
<?php
include "../conn.php"; 
require('./david/Swift.php');
require('./david/Swift/Swift_SMTP_Connection.php');
$connection = new Swift_SMTP_Connection('xxxxx');
$swift = new Swift($connection);
#################
#   VARIABLAR   #
#################
$text       = $_POST['meddelande'];
$subject    = $_POST['subject'];
$sender    = $_POST['sender'];
$user      = $_POST['user'];
$datum      = $_POST['datum'];
$tid      = $_POST['tid'];
$NewReplyTo    = $_POST['sender'];
$avser			= 'skarpt';
$tabell_arkiv	= "test_arkiv";
$tabell_epost	= "test_mailadresser";
$message2 = "<a href=\"http://www.nippe.net/test/lindahl/briefings/tabort.php?id=22?databas={$tabell_epost}\">Vill du inte längre få detta nyhetsbrev. Klicka här</a>\r\n"; 
##################
#   /VARIABLAR   #
##################
$recipients = array();
$result = mysql_query("select epost from test_mailadresser where aktiv='yes'") or die(mysql_error());
//Make our list
$swift->addPart($text, 'text/html');
while ($row = mysql_fetch_assoc($result)) $recipients[] = array($row['epost']);
if (!$swift->hasFailed())
{
    //Pass the array to send()
    $swift->send(
        $recipients,
        '"Johan" <"$sender">',
        '$subject',
        '$text'
    );
	echo "Allting funkade fin fint";
    //Closes cleanly... works without this but it's not as polite.
    $swift->close();
}
else echo "The mailer failed to connect. Errors: ".print_r($swift->errors, 1).". Log: ".print_r($swift->transactions, 1);
print_r($swift->transactions);
$laggTill = "INSERT INTO $tabell_arkiv (subject, meddelande, user, datum, tid, status, mailskick, avser) 
VALUES ('$subject', '$text', '$user', '$datum', '$tid', 'Ej utskickat', '$sum', '$avser')";
mysql_query($laggTill) or die("SQL: $laggtill <br>".mysql_error()); 
?>
I'm not even getting an error message.
Thanks

Posted: Tue Jun 20, 2006 10:15 am
by Chris Corbyn
What does the print_r() show you?

Posted: Tue Jun 20, 2006 10:43 am
by NiGHTFiRE
I don't get anything. It doesn't return anything.

Posted: Tue Jun 20, 2006 11:46 am
by Chris Corbyn
NiGHTFiRE wrote:I don't get anything. It doesn't return anything.
Nothing? Do you even see "Array ()" ? What about a print_r($swift->errors); ?

FYI (I haven't documented it much yet) but there's an isConnected() method to use rather than hasFailed() which is a bit less specific.

Posted: Tue Jun 20, 2006 12:07 pm
by RobertGonzalez
Underneath this line...

Code: Select all

<?php
error_reporting(E_STRICT); 
?>
Add this line...

Code: Select all

<?php
ini_set('display_errors', true); 
?>
Then run it and see if throws anything at you.

Posted: Tue Jun 20, 2006 1:13 pm
by NiGHTFiRE
I've got now:

Code: Select all

<?php 
session_start(); // Alltid överst på sidan 
error_reporting(E_STRICT); 
ini_set('display_errors', true);   
  
// Kolla om inloggad = sessionen satt 
if (!isset($_SESSION['sess_user'])){ 
  header("Location: index.php"); 
  exit; 
} 
?> 
<?php include '../meny.php'; ?> 
<?php include '../meny_mail.php'; ?> 
<?php 
include "../conn.php"; 
require('./david/Swift.php'); 
require('./david/Swift/Swift_SMTP_Connection.php'); 
$connection = new Swift_SMTP_Connection('xxxxx'); 
$swift = new Swift($connection); 
################# 
#   VARIABLAR   # 
################# 
$text       = $_POST['meddelande']; 
$subject    = $_POST['subject']; 
$sender    = $_POST['sender']; 
$user      = $_POST['user']; 
$datum      = $_POST['datum']; 
$tid      = $_POST['tid']; 
$NewReplyTo    = $_POST['sender']; 
$avser      = 'skarpt'; 
$tabell_arkiv   = "test_arkiv"; 
$tabell_epost   = "test_mailadresser"; 
$message2 = "<a href=\"http://www.nippe.net/test/lindahl/briefings/tabort.php?id=22?databas={$tabell_epost}\">Vill du inte längre få detta nyhetsbrev. Klicka här</a>\r\n"; 
################## 
#   /VARIABLAR   # 
################## 
$recipients = array(); 
$result = mysql_query("select epost from test_mailadresser where aktiv='yes'") or die(mysql_error()); 
//Make our list 
$swift->addPart($text, 'text/html'); 
while ($row = mysql_fetch_assoc($result)) $recipients[] = array($row['epost']); 
if ($swift->isConnected())
{ 
    //Pass the array to send() 
    $swift->send( 
        $recipients, 
        '"Johan" <"$sender">', 
        '$subject', 
        '$text' 
    ); 
        echo "Allting funkade fin fint"; 
    //Closes cleanly... works without this but it's not as polite. 
    $swift->close(); 
} 
else echo "The mailer failed to connect. Errors: ".print_r($swift->errors, 1).". Log: ".print_r($swift->transactions, 1); 
print_r($swift->transactions); 
$laggTill = "INSERT INTO $tabell_arkiv (subject, meddelande, user, datum, tid, status, mailskick, avser) 
VALUES ('$subject', '$text', '$user', '$datum', '$tid', 'Ej utskickat', '$sum', '$avser')"; 
mysql_query($laggTill) or die("SQL: $laggtill <br>".mysql_error()); 
print_r($swift->errors);
?>
And i still don't anything. Not any output result. Not even Array ()

Posted: Tue Jun 20, 2006 3:50 pm
by Chris Corbyn
Try echo()'ing something at the bottom of the script. If you're not even seeing an empty array printed something is stopping script execution. Are you using the PHP5 version of swift in PHP4 perhaps? It's been done a few times before.

Posted: Tue Jun 20, 2006 4:01 pm
by NiGHTFiRE
I'll try echoing something. But when i tryed an nice easy script it worked but not now. I'll tell you if it echoes out. Hold on.

EDIT: Now it's just loading the page :/ But I'm not getting any mail so i'll wait some time and see if it's on the "wait list". But i'm really wondering why this isn't working :/

Posted: Wed Jun 21, 2006 7:03 am
by Chris Corbyn
Pssttt... there's no such constant as E_STRICT on PHP4 ;) That could explain why error reporting is off.