Solved

PHP Web App not sending e-mails to external domain

Posted on 2013-02-01
3
278 Views
Last Modified: 2013-02-04
Hi,

I have a PHP Helpdesk web app installed on our Intranet server. Part of the job of the Helpdesk is to send e-mails to Users to keep them up to date with calls. The setup is quite simple and you have to enter an SMTP address into the call in order to send these updates.

We have two domains. The SMTP Server (Exchange 2010) is a different server to the web app server, which is running IIS.

When a call is added/updated and the e-mail address of the User is on the same domain the e-mail works OK. But when we're trying to send to the other domain (for which mail must go out before it comes into the second domain) it fails to be relayed.

i.e. joe.bloggs@samedomain.com works, but joe.bloggs@otherdomain.com does not.

I don't know how to set up PHP Mail and have no idea if it would break things anyway.

I know that the web server can send SMTP to joe.bloggs@otherdomain.com because an ASPX page that sends out passwords works OK to that domain.

I've looked around, but I can't figure out where the issue may lie! :(
0
Comment
Question by:winstalla
  • 2
3 Comments
 
LVL 11

Expert Comment

by:mcnute
ID: 38844313
Why don't you simply take the working code out of the aspx page which sends the password to build the php Mail function for your other page?
0
 

Author Comment

by:winstalla
ID: 38850297
The SMTP for the ASPX page is set in the web.config file with the following details:
      <smtp deliveryMethod="Network" from="webmaster@mydomain.com">
        <network host="mailserver.domain.local" port="25"/>

Open in new window

but I have no idea how to replicate this within PHP. I have looked, briefly, at the PHP.ini file, but changes made to this file do not seem to have helped. Is it here that I should be looking?
0
 
LVL 11

Accepted Solution

by:
mcnute earned 500 total points
ID: 38850316
After you make changes to your php.ini a restart of your webserver (apache, iis, ngnx) is necessary for changes to be effective.

In php you would do it like this:
<?php
 require_once "Mail.php";
 
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 
 $host = "mail.example.com";
 $username = "smtp_username";
 $password = "smtp_password";
 
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 
 $mail = $smtp->send($to, $headers, $body);
 
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?>

Open in new window


This is done with the pear package Mail which is installed by default in many cases and almost for sure in any php5 installation.

If you do not have this installed, then consider swiftmailer, which is a powerful mail class which doesn't need complicated installation. You just download it, put it in a webfolder of your choice and reference to it in a require_once() function accordingly.

With swiftmailer you would do something like this:
require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);

Open in new window

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question