Solved

PHP Web App not sending e-mails to external domain

Posted on 2013-02-01
3
274 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
how to add IIS SMTP to handle application/Scanner relays into office 365.

895 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

14 Experts available now in Live!

Get 1:1 Help Now