Page 1 of 1

Trying to Mail to a variable email.

Posted: Fri Jun 01, 2007 9:58 am
by aredden
First of Swift Mailer is a very nice PHP app, should take pride in it. Secondly I'm having some issues with it.

Im trying to use swift mailer to email to an email address that gets pulled out of my database. but Im not having any luck. heres what I have for the code. The error I get I know why i get it I just dont know how to get around it. in the line

$swift->send($message, $to, "FindMeAFare.com@findmeafare.com"); where I have $to swift doesnt recoginize it as an email address it reads it in as an incorrect string. ive tried changing $to -> "".to$ thinking I could force the variable into the quotes but nothing :S

Just incase here is the error I get, Notice: The recipients parameter must either be a valid string email address, an instance of Swift_RecipientList or an instance of Swift_Address. in /home/content/f/m/a/fmafadmin/html/swift/Swift.php on line 392

Any help would be fantastic

PS: IF I test it with a given email it doesnt sent the .rows['name']; variable either :cry

Code: Select all

<?php
$con = mysql_connect("","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("", $con);

$id = 20;

$result = mysql_query("SELECT * FROM main2 WHERE main2.id = $id");

$row = mysql_fetch_array($result);

echo "Name: ".$row['name']; // is just here to confirm the proper name and email
echo " Age: ".$row['email'];

require_once "swift/Swift.php";
require_once "swift/Swift/Connection/NativeMail.php"; //There are various connections to use
 
$swift =& new Swift(new Swift_Connection_NativeMail());
 
$message =& new Swift_Message("Fare Request From: ".$rows['name']);

$to = $rows['email']; 
 
$swift->send($message, $to, "FindMeAFare.com@findmeafare.com");

?>

Posted: Fri Jun 01, 2007 10:26 am
by superdezign
I've never used Swiftmailer before, but I've heard d11 say that the email needs to have <> around it.

Posted: Fri Jun 01, 2007 10:30 am
by aredden
superdezign wrote:I've never used Swiftmailer before, but I've heard d11 say that the email needs to have <> around it.

That makes no sense to me. Any chance you could clairfy?

Posted: Fri Jun 01, 2007 3:09 pm
by Chris Corbyn
superdezign wrote:I've never used Swiftmailer before, but I've heard d11 say that the email needs to have <> around it.
Well remembered, but sadly you got the wrong end of the stick there ;) I was helping someone run the command directly on their SMTP server rather than through Swift :)

What does $to look like when you echo it ~aredden? :) It should just be the email address, not special characters or < and > in the address.

This would be valid: foo@bar.com
This would be valid: new Swift_Address("foo@bar.com", "Foobar");

These wouldn't:

Foobar <foo@bar.com>
"Foobar" <foo@bar.com>

If you're wondering why I stopped people formatting the address themselves it's because it was wasting too much of my time providing support to people who were doing it wrongly ;) The Swift_Address class does that job.

Posted: Fri Jun 01, 2007 3:58 pm
by Chris Corbyn
I've just had another thought on this too. If your database field is char rather than varchar it will be padded with whitespace on the right hand side. Use trim() if that's the case. It would probably be a good idea for me to trim it myself either way.

Posted: Mon Jun 04, 2007 12:50 pm
by aredden
When I echo it I get mail@me.com for instance. What I am trying to do is to use swift as an automailer of sorts. to issue emails to people who have filled out a form on my website. but so far no luck.

even if i $to = "me@example.com";

when I try to send it i get the same error.

Posted: Mon Jun 04, 2007 1:18 pm
by Chris Corbyn
In your first post you have:

Code: Select all

$row = mysql_fetch_assoc( ... );
Yet you do:

Code: Select all

$to = $rows["email"];
Notice anything odd about that?

Posted: Mon Jun 04, 2007 1:33 pm
by aredden
I dont see anything odd with that. I call the data into an arry $name['email'] and then reassign that variable to $to. what is odd about that? I did that because I couldnt get it to work with just $name and I wasnt sure if mailers read certain variables to email out to.

either way, is it possible to use swift to mail to a variable? if not thats ok I can use the php mailer. I just would rather use swift for its easy in html emails.

Posted: Mon Jun 04, 2007 2:07 pm
by Chris Corbyn
Look again. Turn error reporting onto full and you'll see what I'm on about:

Code: Select all

<?php error_reporting(E_ALL); ini_set("display_errors", true); ?>
$rows != $row

EDIT | And yes, of course you can use variables ;) That has nothing to do with Swift, it's just basic PHP. You've just got an undefined variable error in your script that's all so NULL is what the contents of $to are in reality.

Posted: Mon Jun 04, 2007 3:10 pm
by aredden
ok I gave that a check and I see what you mean.

but if i assign $to = "aredden@gmail.com"; it wont issue me an email.?

Posted: Tue Jun 05, 2007 10:47 am
by Chris Corbyn
aredden wrote:ok I gave that a check and I see what you mean.

but if i assign $to = "aredden@gmail.com"; it wont issue me an email.?
It gives you the error about it not being a valid email address, or it gives you an error that the SMTP server rejected the address?

Posted: Wed Jun 06, 2007 7:41 am
by aredden
Notice: The recipients parameter must either be a valid string email address, an instance of Swift_RecipientList or an instance of Swift_Address. in /home/content/f/m/a/fmafadmin/html/swift/Swift.php on line 392 same error ive been getting all along.

Posted: Wed Jun 06, 2007 7:48 am
by Chris Corbyn
What does this show *immediately* before the send() command as shown here?

Code: Select all

var_dump($to); $swift->send($message, $to, "FindMeAFare.com@findmeafare.com");

Posted: Wed Jun 06, 2007 1:51 pm
by aredden
its coming back as a null value :S and I cant figure out why.

Posted: Wed Jun 06, 2007 2:57 pm
by Chris Corbyn
I'll leave you to it then ;) You'll suss it out. First thing I'd try is checking the contents of your mysql resultset are what you expect.