problem using swift mailer

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
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

problem using swift mailer

Post by choubix »

hello,

I just uploaded the files, read the wikidoc and gave it a try.
it doesnt work for a reason i ignore. (even though I guess it comes from SMTP.php - I didnt alter the file in any way)

can someone give me a hand in getting this script to work please?

Here is my code:

Code: Select all

<?php
session_start();

if (isset($id_clients))

{
require('../includes/config.php');
require('../includes/functions.tpl.php');

//store id of the client
$id = $_SESSION['id_clients'];

//get all data from the form, check if it is empty
$fname1 = (isset($_POST['fname1']))?$_POST['fname1']:'';
$lname1 = (isset($_POST['lname1']))?$_POST['lname1']:'';
$email_a = (isset($_POST['email1']))?$_POST['email1']:'';

$fname2 = (isset($_POST['fname2']))?$_POST['fname2']:'';
$lname2 = (isset($_POST['lname2']))?$_POST['lname2']:'';
$email_b = (isset($_POST['email2']))?$_POST['email2']:'';

$fname3 = (isset($_POST['fname3']))?$_POST['fname3']:'';
$lname3 = (isset($_POST['lname3']))?$_POST['lname3']:'';
$email_c = (isset($_POST['email3']))?$_POST['email3']:'';

$fname4 = (isset($_POST['fname4']))?$_POST['fname4']:'';
$lname4 = (isset($_POST['lname4']))?$_POST['lname4']:'';
$email_d = (isset($_POST['email4']))?$_POST['email4']:'';

$fname5 = (isset($_POST['fname5']))?$_POST['fname5']:'';
$lname5 = (isset($_POST['lname5']))?$_POST['lname5']:'';
$email_e = (isset($_POST['email5']))?$_POST['email5']:'';



//1st case
if(!empty($fname1) && !empty($lname1) && !empty($email_a))
	{
	//protect against mysql injection
	$email1 = mysql_real_escape_string(htmlentities($email_a, ENT_QUOTES)); 
	
	//check if email already exists
	$query = (" SELECT email FROM clients WHERE email = '$email1' ");
	$result = mysql_query($query) or die('Invalid query: ' . mysql_error()); 
	$data = mysql_num_rows($result);
	
	if ($data < 1)
		{
		//generates random password
		$password_temp = random_password();
		$password1 = md5($password_temp);
		
		//insert into database new account: beware, EMAIL IS WRONG
		$query = (" INSERT INTO clients (fname, lname, password, email, points, referral, maxgrids, gridsleft) 
					VALUES ('$fname1', '$lname1', '$password1', '$email1', '500', '$id', '11', '1') ");
		mysql_query($query) or die('Invalid query: ' . mysql_error()); 
		
		
		//return id of this new client, random grid, and current time
		$id1 = mysql_insert_id();
		
		//get a random grid (sorted) + convert it to an array
		$sortedlist = random_draw();
		$array = strtoArray($sortedlist);
		$array2 = implode("-", $array);
		$arraylist = substr_replace($array2,"",-1);
		
		//gets the current time
		$registration = time();
		
		$query = (" INSERT INTO grids_clients (id_clients, grids_1, grids_2, gridnumber, timeplayed) 
				  VALUES ('$id1', '$sortedlist', '$arraylist', '1', '$registration') ");
		mysql_query($query) or die('Invalid query: ' . mysql_error()); 	

		//free memory of successive queries
		mysql_free_result($result);
		
		print "ok";
		//email here
		//$sendmail1 = 1;
		}
		//email is already in database
		else
		{
		$email1_taken = 1;
		}
		
	}


//EMAIL TO SEND HERE

require_once "../includes/class_mailer/Swift.php";
require_once "../includes/class_mailer/Swift/Connection/SMTP.php"


//Start Swift
$smtp =& new Swift_Connection_SMTP("smtp.myserver.com");
$smtp->setUsername("admin");
$smtp->setPassword("mypassword");

$swift =& new Swift($smtp);

//Create the message
$message =& new Swift_Message("Test: title of the email", "Content: works?");
 
//Now check if Swift actually sends it
if ($swift->send($message, "recipient_here@email.com", "admin@myserver.com"))
{
echo "Sent";
}
else
{ 
echo "Failed";
}




//end 1st IF statement (line 4)
}

else
{
mysql_close();
$_SESSION['error'] = 5;
header('Location: ../errors/error.php');
}
I changed all login info for generic info here
this returns a blank page AND my form is not processed (no insert in the database)...



if I use this (basically: disable the require_once[...]SMTP.php and perform a different test)
my form is processed AND I can see the message "sent" (even though nothing can be processed because I disabled the actual test)

Code: Select all

require_once "../includes/class_mailer/Swift.php";
//require_once "../includes/class_mailer/Swift/Connection/SMTP.php"
//require_once "../includes/class_mailer/Swift/Authenticator/LOGIN.php";

//Start Swift
$smtp =& new Swift_Connection_SMTP("smtp.myserver.com");
$smtp->setUsername("admin");
$smtp->setPassword("mypassword");

$swift =& new Swift($smtp);

//Create the message
$message =& new Swift_Message("Test: title of the email", "Content: works?");
 
//Now check if Swift actually sends it
if ($sendmail1= 1)
{
echo "Sent";
}
else
{ 
echo "Failed";
}

Hope I gave enough info for someone to help me out :)

thanks!
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

last but not least:

the smoke test doesnt work here:

Code: Select all

Error: Message did not send!
Log Information

++ Log level changed to 4
++ Trying to connect...
++ Trying to connect to SMTP server at 'smtp.myserver.com:25
<< 220 wpc1115.amenworld.com ESMTP
>> EHLO [62.193.226.216]
<< 250-wpc1115.amenworld.com
250-AUTH=LOGIN CRAM-MD5 PLAIN
250-AUTH LOGIN CRAM-MD5 PLAIN
250-STARTTLS
250-PIPELINING
250 8BITMIME
++ SMTP extension 'AUTH' reported with attributes [LOGIN, CRAM-MD5, PLAIN].
++ SMTP extension 'AUTH' reported with attributes [LOGIN, CRAM-MD5, PLAIN].
++ SMTP extension 'STARTTLS' reported with attributes [].
++ SMTP extension 'PIPELINING' reported with attributes [].
++ SMTP extension '8BITMIME' reported with attributes [].
>> MAIL FROM: <admin@myserser.com>
<< 250 ok
>> RCPT TO: <myemail@myemail.fr>
<< 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)
!! Expected response code(s) [250] but got response [553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)]
!! Recipient 'myemail@myemail.fr' rejected by connection.
>> RSET
<< 250 flushed
I changed the "sensitive" data on purpose here.
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

hi, well: changing smtp.myserver.com for localhost helped me run the smoke test: i recive the basic email

but i cant figure out how to make swiftmailer send an email in the main script :(

hope someone can help :)
thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

choubix wrote:hi, well: changing smtp.myserver.com for localhost helped me run the smoke test: i recive the basic email

but i cant figure out how to make swiftmailer send an email in the main script :(

hope someone can help :)
thanks
Change the SMTP server in the main script too. You're using a SMTP server which doesn't allow you to relay mail ;) It could also just be a case of authenticating:

http://www.swiftmailer.org/wikidocs/v3/smtpauth
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

hi Chris, thanks for taking the time to give me a hand!

I checked in my admin panel (plesk) whether relaying was authorized and it apparently is (the fact that i am already using the server torealy my emails in outlook should have been a clue ;) )

I separated the script I wrote from the one used by Swift mailer. it still returns a blank page :(

I tried a few things without success...

Code: Select all

require_once "../includes/class_mailer/Swift.php";
require_once "../includes/class_mailer/Swift/Connection/SMTP.php"
//require_once "../includes/class_mailer/Swift/Authenticator/LOGIN.php";

//Start Swift
$smtp =& new Swift_Connection_SMTP("localhost",25);
$smtp->setUsername("myaccount");
$smtp->setPassword("mypassword");

$swift =& new Swift($smtp);


//Create the message
$message =& new Swift_Message("Title here", "content here");
 
//Now check if Swift actually sends it
if ($swift->send($message, "receiver@mail.com", "sender@myserver.com"))
{
echo "Sent";
}
else
{ 
echo "Failed";
}

$smtp->setUsername("myaccount");
$smtp->setPassword("mypassword");

username and password are the one i use to connect to my webmail
I also tried localhost AND smtp.myserver.com without success

it still returns a blank page: doesnt even return me "Failed" :(

i am a bit lost...
by the way Chris if you are reading this: great job on explaining how to avoid getting emails being considered Spams by the servers! :)
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

i was wondering: maybe it is because of the path:
i stored swift mailer in includes/class_mailer

maybe swift has to be stored at the root...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

choubix wrote:i was wondering: maybe it is because of the path:
i stored swift mailer in includes/class_mailer

maybe swift has to be stored at the root...
A blank page usually indicates that you're getting a fatal error.

Put this just after the first <?php tag in your page.

Code: Select all

<?php

error_reporting(E_ALL); ini_set('display_errors', true);
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

hi chris

i was already using error_reporting with E_ALL
nothing in my error_log

here is what I have in the script now:


<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

require_once "../includes/class_mailer/Swift.php";
require_once "../includes/class_mailer/Swift/Connection/SMTP.php"
//require_once "../includes/class_mailer/Swift/Authenticator/LOGIN.php";

//Start Swift
$smtp =& new Swift_Connection_SMTP("smtp.myserver.com",25);
$smtp->setUsername("mylogin");
$smtp->setPassword("mypassword");


$swift =& new Swift($smtp);

//Create the message
$message =& new Swift_Message("Test ", "test");

//Now check if Swift actually sends it
if ($swift->send($message, "receiver@email.com", "mylogin@myserver.com"))
{
echo "Sent";
}
else
{
echo "Failed";
}
?>

i still get a blank page. i tried with / without the login and password and with / without localhost instead of the server name.

really dont have a clue why it doesnt work. mail() function works though
smoke test also works (I get the test message in my mailbox)
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

well: all smokes tests work (jsut tried the others)

tried to check again the error_log : nothing... tried variations of the code I could find in the tutorials of swift mailer...

I dont get why it works with the smokes test and not when i try to send an email...

and the realy is activated in Plesk + SMTP authentication

would really appreciate if someone could help me out :)
sure I'm not the only one who had difficulties to get swift to run ;)

i'm pretty amazed by all the work that has been done and would really like to enjoy it's full power on my server

thanks! :)
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

hello,

once again: i did everything from scratch: uploaded the files (didnt change the path so it's lib/Swift....)
I opened a Gmail account for the occasion.

I created a test.php with the following content but still : a blank page (and if I try to have a print "a message"; anywhere on the page it doesnt show)



<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

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

//Start Swift
$smtp = new Swift(new Swift_Connection_SMTP(
"smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
$smtp->setUsername("mylogin");
$smtp->setpassword("mypassword");

$swift =& new Swift($smtp);
print "alex";
//Create the message
$message =& new Swift_Message("Test intitule", "ca marche??");

//Now check if Swift actually sends it
$swift->send($message, "me@myemail.com", "admin@mywebsite.com")


?>

I really dont know what to do to make the script to run...
I am using php 5.04 and didnt alter a single file of the package.

I tried a series of servers (localhost, my server's name, my server's ip address, now Gmail...)

hope someone can help me...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I suggest upgrading PHP before doing anything else ;) 5.0 is old and had some huge problems. I'm not even sure Swift works with 5.0 correctly but I'm not 100% on that.

Have you tried echo'ing things throughout your code to figure out where it's dying? The next step would then be to start commenting out the code line by line until it runs.

You know, this is almost definitely a path problem.

Where is the script you're invoking, and where is swift installed? Full paths please :)
choubix
Forum Commoner
Posts: 42
Joined: Tue Nov 06, 2007 12:21 pm

Post by choubix »

hi chris,

the upgrade is planned next week :)

as for the problem: I managed to get something!

Code: Select all

Notice: Use of undefined constant self::LOG_NOTHING - assumed 'self::LOG_NOTHING' in /server_path......./includes/class_mailer/Swift/LogContainer.php on line 43

Fatal error: Call to undefined method Swift::attachAuthenticator() in /path_to_the _file/test.php on line 11
with this:

Code: Select all

<?php
error_reporting(E_ALL); 
ini_set('display_errors', true);

require_once "includes/class_mailer/Swift.php";
require_once "includes/class_mailer/Swift/Connection/SMTP.php";
require_once "includes/class_mailer/Swift/Authenticator/LOGIN.php"; //tried without the authenticator too

//Start Swift
$smtp = new Swift(new Swift_Connection_SMTP("smtp.myserver.com",25)); //tried with locahost
$smtp->attachAuthenticator(new Swift_Authenticator_LOGIN());
$smtp->setUsername("myaccount");  //on the server I use the actual login / password
$smtp->setPassword("mypassword");

 
$swift =& new Swift($smtp);


//Create the message
$message =& new Swift_Message("Title", "body");
 
//Now check if Swift actually sends it
if($swift->send($message, "receiver@mail.com", "sender@server.com"))
{
print "works";
}
else
{
print "didn't work";
}

$swift->disconnect();
?>
any idea??

by the way: where do I find the log file if I want to use the log method please?

thanks
Post Reply