Php smtp email not working


#1

I am unable to send smtp eamil from the following script.
require_once “Mail.php”;

$to = "someone@theirdomain.com";
$from = "me@mydomain.com";
$host = “mail.mydomain.com”;
$username = "user@mydomain.com";
$password = “pswrd”;

$subject = “My subject”;

$body = "Dear someone,

Thank you for joining.

$message = " HTML email

This email contains HTML Tags!

{$body}
";

// Always set content-type when sending HTML email
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type:text/html;charset=iso-8859-1” . “\r\n”;
$headers .= “From: {$from}\r\n”;
$headers .= “To: {$to}\r\n”;
$headers .= “Subject: {$subject}\r\n”;

$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body );

if (PEAR::isError($mail)) {
echo("

" . $mail->getMessage() . “

”);
} else {
echo(“

Message successfully sent!

”);
}

I get this error
authentication failure [SMTP: Invalid response code received from server (code: 535, response: 5.7.8 Error: authentication failed: authentication failure)]

If I cut and paste the username and password from my script in oour webmail login, I am able to login without a problem. Using the same setting in Outlook, I can receive but i cannot send.

I am pretty new to this php email. Especially with PEAR and SSH where I have zero experience. Although I have used this script on 2 other servers without a problem. Unfortunately Dreamhost is not playing nice.

I have been stuck here for 3 days now. Any help would be very welcome.
Cheers
Rabbittracx


#2

I don’t like replies to questions where a completely alternative approach is recommended, compared to just answering the question. In this case I’ll suggest that I chose a specific approach simply because I was having similar problems with the approach you’re taking, so I’ll provide you with my solution.

Rather than using Mail.php, I use PHPMailer. The following code was copied from a working script and then stripped of details. This code works when you have PHPMailer properly referenced. You’ll need to modify for your own purposes. Note that I’m using Heredoc syntax (<<<) for PHP 5.2 because Nowdoc syntax only works when using PHP 5.3, which you may or may not be using. With this code and info you should be able to lookup PHPMailer and make productive use of this code. HTH

[php]<?php
require_once(‘class.phpmailer.php’);
$mail = new PHPMailer(true);
try {
$mail->IsSMTP();
$mail->Host = ‘mail.zzzzzzz.com’;
$mail->SMTPAuth = true;
$mail->Username = ‘zzz@zzzzz.com’;
$mail->Password = ‘zzzzzz’;
$mail->WordWrap = 0;
$mail->AddCustomHeader(‘X-Mailer: MyApp v1.2.3’);
$mail->SetFrom(‘zzz@zz.com’, ‘zzzzzzz (zzz)’);
$mail->AddAddress(‘zzzz@zzzzzz.com’);
$mail->Subject = ‘zzzzzzzzzzzzzzz’;
$mailbody =<<<UNIQUE_VALUE_HERE
this is your text

This message brought to you by zzzzzzzzzzzzzz
UNIQUE_VALUE_HERE;
$mail->Body = $mailbody;
if(!$mail->Send()) {
echo “FAIL:\n”;
echo $mail->ErrorInfo;
} else {
echo “OK\n”;
}
} catch (phpmailerException $e) {
echo “FAIL:\n”;
echo $e->errorMessage(); // from PHPMailer
} catch (Exception $e) {
echo “FAIL:\n”;
echo $e->getMessage(); // from anything else!
}
?>[/php]


#3

Thanks to AndyJ and Starbuck for their replies to my post. I was not aware of it but SMTP is a Windows method and will never work on a UNIX server. I was able to find a sample script using PEAR’s sendmail MTA, and I now have a functioning eamail confirmation program.

Cheers


#4

No, that’s not true at all — where did you hear that? SMTP definitely exists on UNIX servers as well, and is one of multiple ways that you can send mail. (sendmail is, indeed, another.)


#5

Thanks andrewf,
Sorry, I am definitely not interested in spreading misinformation. This was part of the contents in a google search I came across while trying to sort out why SMTP send was failing using my DH mailbox. As soon as I cam across this I worked on using the sendmail MTA and I was able to get emails sent. This after days of trying to get a solution out of DH Support. And this information seemed to address all the errors I was getting.

Your input does ring true. I have email accounts on unix servers the I send and receive using Outlook and SMTP authentication without a problem.

Currently I cannot use Outlook to send with my DH mail account. DH keep telling me there is nothing wrong with their mail server, but they cannot tell me how to fix my config settings. I have been all over the map with them, port 25 vs 587, authentication setting for login same as incoming vs seperate login in. Nother works. Incoming is no problem.

Cheers


#6

The statement that SMTP is specific to Windows or Unix is incorrect. SMTP works as client or server with any OS. I get real tired of Linux geeks just blaming Windows when they can’t get stuff to work. It doesn’t speak for their *nix prowess, just for their overall incompetence with technology in general. That doesn’t apply to you, you’re just passing on what you read, but that’s what happens when people spout techno-religion and passers-by start to pick up on the doctrine. (Different topic, sorry…)

It looks like your confusing client and server as well. If you have both a Windows client (MUA) and a Linux MTA/MUA that can’t connect to your server (DH SMTP), then you have config issues and maybe not code issues. For example, from your Windows PC through your ISP, as you know, you may need to use a different SMTP port than 25, and there may be another detail you’re missing, like checking “My server requires SMTP authentication”.

However, since you can use sendmail from Linux, you know the DH server is fine. So that confirms a config or network issue from your PC, and a code issue with your original code.

I’m glad you got sendmail working. The reason I use PHPMailer and the script I provided is that the exact same code works from both a Windows and a Linux client, not so with sendmail. If you need to use your code in Windows, you now need to switch to something like Blat.

You have more information. The record is set straight. Your code is working. We’re all happy.