?
Solved

PHP Web App not sending e-mails to external domain

Posted on 2013-02-01
3
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
The viewer will learn how to dynamically set the form action using jQuery.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

771 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