Trying to Mail to a variable email.

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
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Trying to Mail to a variable email.

Post 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");

?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I've never used Swiftmailer before, but I've heard d11 say that the email needs to have <> around it.
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

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

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

Post 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.
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

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

Post 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?
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

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

Post 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.
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

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

Post 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?
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Post 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.
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 this show *immediately* before the send() command as shown here?

Code: Select all

var_dump($to); $swift->send($message, $to, "FindMeAFare.com@findmeafare.com");
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Post by aredden »

its coming back as a null value :S and I cant figure out why.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply