Solved

PHP Web App not sending e-mails to external domain

Posted on 2013-02-01
3
271 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now