RMK
RMK

Reputation: 456

PHPMailer Send an email through a Local Domain but from a regular Email

I am trying to setup PHPMailer for a customer. He has his own mail server located at a certain IP address. When asked to give me the information to send email through the system, he gave the following:

Host: xx.xxx.x.x
Port: 25
Domain: mydomain.local
Username: [email protected]
Password: <myemailpassword>
From: [email protected]

(Which he confirmed is being used for external email sending)

I tried to setup PHPMailer by setting the parameters to the exact namings above.

$mail->IsSMTP();                      
$mail->SMTPDebug  = 1;                
$mail->SMTPAuth   = true;            
$mail->SMTPSecure = "ssl";          
$mail->Host       = "xx.xxx.x.x";
$mail->Port       = 25;              
$mail->Username   = "[email protected]";  
$mail->Password   = <myemailpassword>;         

$mail->SetFrom('[email protected]', 'Webname');
$mail->[...]

I got the following error:

Failed to connect to server (0)

So I try to send an email through telnet to check if it's the customer's email server or the PHPMailer settings:

 telnet xx.xxx.x.x 25

It goes through, I'm connected to the server.

 helo mydomain.local

I'm getting 'Hello' as a reply. This leads me to believe it might be the PHPMailer settings that are wrong here.

I also try not using SMTP:

$mail->Host       = "ssl://xx.xxx.x.x";
$mail->Port       = 25;              
$mail->Username   = "[email protected]";  
$mail->Password   = "password";         

$mail->SetFrom('[email protected]', 'Webname');
$mail->[...]

Again no go. Am I going about this wrong? I'm only familiar with setting up PHPMailer to use Gmail before so I'm at a loss as to what could be the issue because I'm using a 'personal' email server.

Upvotes: 1

Views: 10592

Answers (2)

RMK
RMK

Reputation: 456

Thanks Loadparts for your assistance.

I'm still not sure what the issue was but it seems it has resolved itself. It might have been from the email server side because coding wise, I didn't change anything. This is the final code I used.

    $mail = new PHPMailer(true); 

    $mail->IsSMTP(); 
    $mail->SMTPAuth   = true; 
    $mail->Port       = 25; 
    $mail->Host       = "xx.xxx.x.x"; // SMTP server
    $mail->Username   = "[email protected]";  
    $mail->Password   = <myemailpassword>;

    $mail->From       = "[email protected]";
    $mail->FromName   = <Web_Name>;
    $mail->AddAddress("[email protected]");

    $mail->Subject  = <Subject>;
    $mail->AltBody    = <Alt_Body>
    $mail->WordWrap   = 80; 

    $body             = "test message";
    $mail->MsgHTML($body);
    $mail->IsHTML(true);

    $mail->Send();

Upvotes: 2

Drace
Drace

Reputation: 699

I use a test function that I know works 100% to test the email servers when using PHPMailer.

I'm not sure why you are having your problem, but try to use the function I have ( I know it's messy but it does the trick). Just replace all the XXXX with your info and make sure you have both class.phpmailer.php and class.smtp.php in the same folder.

<?php
error_reporting(E_ALL);

$toemail = 'XXXX';
$toname = 'XXXX';
$subject = 'Testing Email Sending...';
$bodyhtml = '<H1>yeah</h1>';
$bodytext = 'heres Hoping it works';
$fromemail = 'XXXX';
$fromname = 'XXXX';

var_dump(sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname));


function sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname)
{
    require_once("class.phpmailer.php");

    $mail = new phpmailer();
    $mail->IsSMTP();
    $mail->From     = $fromemail;
    $mail->FromName = $fromname;

    $mail->Host     = "XXXX";  
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->Username = "XXXX";  // SMTP username
    $mail->Password = "XXXX"; // SMTP password
    $mail->Port="25";


    $mail->SMTPDebug=true;


    if(strlen($bodyhtml)>0) {
        $mail->Body  = $bodyhtml;
        $mail->IsHTML(true);
        }
    else if(strlen($bodytext)>0){ 
            $mail->Body = $bodytext;
    }

    if(strlen($bodytext)>0 && strlen($bodyhtml)>0){ 
        $mail->AltBody = $bodytext;
    }

    $mail->AddReplyto($fromemail,$fromname);
    $mail->Subject  =  $subject;

    // Check if multiple recipients
    if(preg_match("/;/",$toemail))
    {
        $tmp_email=preg_split("/;/",$toemail);
        $tmp_contact=preg_split("/;/",$toname);
        $mail->AddAddress($tmp_email[0], $tmp_contact[0]);
//      echo "<!-- multi email:".$tmp_email[0]." contact:".$tmp_contact[0]." -->\n";
        for($j=1;$j<count($tmp_email);$j++)
        {
            if(preg_match("/\@/",$tmp_email[$j]))
            {   $mail->AddCC($tmp_email[$j], $tmp_contact[$j]);
//              echo "<!-- multi email cc:".$tmp_email[$j]." contact:".$tmp_contact[$j]." -->\n";
            }
        }
    }
    else{ 
        $mail->AddAddress($toemail, $toname);
    }

    $error= false;
    if($mail->Send()){    
        $error =true;
    }

    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    return $error;

}

If this doesn't work, my first try would be using port 80 - which usually isn't blocked, then you can work on getting SSL to work.

PS: because it's a local domain, you may want to consider adding the domain to your /etc/hosts just to be sure.

Best of Luck!

Upvotes: 1

Related Questions