[SOLVED] PHPMailer Usage Problem

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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

[SOLVED] PHPMailer Usage Problem

Post by jayshields »

Ok.

New problem.

I've decided to use PHPMailer in my Newsletter sending script. I've done something like this successfully before with just mail(), but this time, the emails never arrive. For the record, mail() works fine on the same server I'm using PHPMailer on.

This is my code snippet:

Code: Select all

//Include the PHP Mailer and SMTP class
					include('includes/php/phpmailer/class.phpmailer.php');
					include('includes/php/phpmailer/class.smtp.php');
									
					//Initiate the PHPMailer
					$mailer = new PHPMailer();

					//Set some PHPMailer properties
					$mailer->Subject = EMAIL_SUBJECT; //Subject
					$mailer->FromName = EMAIL_FROM_NAME; //From name
					$mailer->From = EMAIL_FROM; //From email
					$mailer->isHTML(true); //HTML email activation
					$mailer->isMail(); //Use PHP's mail() function
					$mailer->AltBody = "blah."; //Text body

					//Initiate a counter
					$counter = 0;

					//Loop through all the people on the mailing list
					while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
						//Add them to the list of BCC recipients
						$mailer->AddBCC($row['email'], $row['name']);
						$counter++;
					}
					
					//Set a formatted date string
					$tmp = explode('-', $today);
					$dateformatted = $tmp[2] . '/' . $tmp[1] . '/' . substr($tmp[0], 2); 

					//Set the HTML email body
					$mail->Body =	str_replace('{DATE}', $dateformatted, file_get_contents(EMAIL_HEADER)) . //Add the HTML header
									$htmlbody . //Add the HTML body
									file_get_contents(EMAIL_FOOTER); //Add the HTML footer

					//Send the email
					if($mailer->Send()) {
						//Set a success message
						$msg .= '<font color="green">You have sent <b>' . $caramount  . '</b> set(s) of car details to <b>' . $counter . '</b> person(s).</font>';
					} else {
						//Set an error mesage
						$msg .= '<font color="red">The newsletter sending failed.<br />Please try again.</font>';
					}
There's alot more before and after that, but that doesn't affect it.

I have error reporting set to E_ALL, and get nothing.

When sent, the $msg echo'd is always the success one, with the right amount in each of my counters.

Does anyone have any experience with PHPMailer and can point me in the right direction? I've Google'd for some good usage examples but can't find any half decent ones except the one on PHP Freaks.

Thanks.
Last edited by jayshields on Thu Feb 16, 2006 5:24 am, edited 1 time in total.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

AddBCC() doesn't work with win32 using Mail(). Are you using Windows?

phpMailer Tutorial
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I think it is a Windows server, yeah. That must be it.

I've already found that tutorial - I said in my original post.

Anyway, thanks for the information; looks like I'm gunna be looping through every person and emailing them seperately... just what I wanted to avoid...
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

jayshields wrote:I've already found that tutorial - I said in my original post.
:oops: ... well at least others will know which tutorial you were referring to in your post.

“They tell you that you'll lose your mind when you grow older. What they don't tell you is that you won't miss it very much.” - Malcolm Cowley
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Does BCC not work with PHP's mail() at all on Windows servers?

I'm looking into using PEAR mail() now and I'm also wondering if it's actually possible to not specify a To: address and instead just specify a bunch of BCC: addresses? This is not function specific, just about sending email in general :P

The examples in alot of mail sending processes I've seen say that without a To: address set an error will be given, so I hope it would notice the BCC's and use them, or I'll have to set the first address in my loop to the To: address, which seems like an unnecessary work-around to me :( I thought I would just ask this aswell while I'm here, it will save me testing it :)

Thanks.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

As far as I'm aware To:'s are needed but you could always use the webmaster@yoursite.com or newslettersignup@yoursite.com type email address something that's already in the public domain so if it gets spammed you can easily change it.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Does anyone know if BCC works at all on Windows servers when using mail()?
AddBCC() doesn't work with win32 using Mail().
...lead me to believe it's just the AddBCC() function within PHPMailer, but now I think you mean BCC in general...?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

jayshields wrote:Does anyone know if BCC works at all on Windows servers when using mail()?
AddBCC() doesn't work with win32 using Mail().
...lead me to believe it's just the AddBCC() function within PHPMailer, but now I think you mean BCC in general...?
I was referring to only PHPMailer in my reply [it's in their documentation somewhere] and I'm not aware of a wider problem. However, I also have little experience with Windows servers. Why not set up an email account on the server and try SMTP? Maybe you'll have better results. [Unless sockets are banned .... ]
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Argh, I'm going round in circles now. I've decided to setup SMTP like you said and revert back to using PHPMailer.

Now, everything works fine and dandy, unless I use BCC.

I always set the default To: address with $mailer->AddAddress('blah@blah.com') and then go on to add the BCC's after.

If I comment out the BCC address additions it sends to the default address fine and works a treat. As soon as I uncomment the BCC address additions PHPMailer throws an error:

Code: Select all

Errors: Language string failed to load: recipients_failedj4yshields@hotmail.com, jayshields@gmail.com, jay@jay-designs.co.uk
The 3 email addresses in the error are my 3 BCC's, they all go to me - this is just testing.

Google gives me mixed results and talks about class.phplistmailer.php and versions of PHPMailer that are newer than the one I just got off SourceForge.

I'm confused.

Help please!!?!
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

First you need to fix the Language file problem:
Language string failed to load:
Your not getting a proper error because PHPMailer can't find the Language file.

Looks like a "SMTP Error: The following recipients failed:" message though.

If you set "$SMTPDebug" to 2 (or more) you should get more info out of SMTP(). [Yes, I realize it says on/off but it's used to set do_debug in class.smtp.php. Be nice if it was properly documented wouldn't it?]
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

This is driving me insane.

SMTPDebug is now set to 2.

I get this:

Code: Select all

SMTP -> FROM SERVER: 220 X1 NT-ESMTP Server Athena.serverdns.net (IMail 6.06 42390-40) SMTP -> FROM SERVER: 250-Athena.serverdns.net says hello 250-SIZE 0 250-8BITMIME 250-DSN 250-ETRN 250-AUTH LOGIN CRAM-MD5 250-AUTH=LOGIN 250 EXPN SMTP -> FROM SERVER: 250 ok SMTP -> FROM SERVER: 250 ok deliver to alternate SMTP -> FROM SERVER: 550 not local host hotmail.com, not a gateway SMTP -> ERROR: RCPT not accepted from server: 550 not local host hotmail.com, not a gateway SMTP -> FROM SERVER: 550 not local host gmail.com, not a gateway SMTP -> ERROR: RCPT not accepted from server: 550 not local host gmail.com, not a gateway SMTP -> FROM SERVER: 550 not local host jay-designs.co.uk, not a gateway SMTP -> ERROR: RCPT not accepted from server: 550 not local host jay-designs.co.uk, not a gateway SMTP -> FROM SERVER: 250 ok its reset
...which is absolutely no help at all.

I cannot fix the language file problem. My language file is on the FTP, in the right folder. I have even explicitly stated the language to use in my script using:

Code: Select all

$mailer->SetLanguage('en'); //Set the language to english
When I find the if that triggers the Cannot load language string error it refers to if a the language array key is unset, and when I try to find where the array is populated... I can't.

Google gives me some results saying about using ini_set in the script to change the SMTP settings, and also it says about changing some lines in the PHPMailer class - which I can't find.

Shall I just try PEAR's mail() and scrap this now?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

jayshields wrote:...which is absolutely no help at all.
Say WHAT? Its totally helpful!

Code: Select all

SMTP -> FROM SERVER: 220 X1 NT-ESMTP Server Athena.serverdns.net (IMail 6.06 42390-40) SMTP -> FROM SERVER: 250-Athena.serverdns.net says hello 250-SIZE 0 250-8BITMIME 250-DSN 250-ETRN 250-AUTH LOGIN CRAM-MD5 250-AUTH=LOGIN 250 EXPN SMTP -> FROM SERVER: 250 ok SMTP -> FROM SERVER: 250 ok deliver to alternate
So far, we've done fine. All hello/login, and "ok"'s. Solid!

Code: Select all

SMTP-> FROM SERVER: 550 not local host hotmail.com, not a gateway
Flag on the play! You aren't either a local host (a machine on the same network as hotmail.com), OR a gateway that hotmail accepts from. We better check you out - you might be breaking the rules.

Code: Select all

SMTP -> ERROR: RCPT not accepted from server: 550 not local host hotmail.com, not a gateway
Yup, sorry, he says he won't take your email, because you arent on the same network OR a gateway. Go away now.

Code: Select all

SMTP -> FROM SERVER: 550 not local host gmail.com, not a gateway
Gmail says the same thing. Quit that.

Code: Select all

SMTP -> ERROR: RCPT not accepted from server: 550 not local host gmail.com, not a gateway
Uh huh! See! The server won't take it!

Code: Select all

SMTP -> FROM SERVER: 550 not local host jay-designs.co.uk, not a gateway
Hmm. I'll ask jay-designs too, but seems like the same deal!

Code: Select all

SMTP -> ERROR: RCPT not accepted from server: 550 not local host jay-designs.co.uk, not a gateway
Third strike and you are OUT!

Code: Select all

SMTP -> FROM SERVER: 250 ok its reset
<cartman>Screw you guys, we're going home!</cartman>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

LOL. Thanks :)

So how can I become a gateway to these external servers/domains?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Finally sorted this out.

I searched Google about SMTP not a gateway problem and found out I needed to use SMTP authentication.

I should've realised this myself, because if SMTP authentication was not needed anyone who knew the SMTP hostname could just spam emails from a known email address to anyone. That's why it works sending to local emails and not external ones.

So I added this to my script:

Code: Select all

//Set the SMTP details for PHPMailer
					$mailer->isSMTP(); //Tell PHPMailer to send the emails via SMTP
					$mailer->SMTPAuth = TRUE; //Tell PHPMailer to authenticate with SMTP before sending
					$mailer->Host = 'mail.mydomain.co.uk'; //Set the SMTP host name
					$mailer->Username = EMAIL_FROM; //Set the SMTP username
					$mailer->Password = SITE_PASS; //Set the SMTP password
Now it works fine, what a relief :)
Post Reply