[SOLVED] Sending mail with Swift

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

[SOLVED] Sending mail with Swift

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

Post by Chris Corbyn »

What does the print_r() show you?
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

I don't get anything. It doesn't return anything.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

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

Post 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.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

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

Post by Chris Corbyn »

Pssttt... there's no such constant as E_STRICT on PHP4 ;) That could explain why error reporting is off.
Post Reply