Solved

PHP SMTP sending emails from script and AUTH

Posted on 2007-11-28
2
1,132 Views
Last Modified: 2013-12-13
I would like to ask how to set PHP.ini to be able to send emails from PHP scripts via MAIL() function - we do not have open relay SMTP, we use MS Server 2003 with regular default SMTP from MS Server 2003

and also can you give some easy sample example PHP script, which should work with the PHP.ini setting?

we would like also to be able to send emails from different email addresses from our PHP scripts hosted on our server

any ideas?

thanks!

Peter
0
Comment
Question by:pplan
  • 2
2 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 20364835
I don't think PHP natively support SMTPS.

Authentication needs to take place before any conversation.

I would recommend using a mailer class (I use htmlMimeMail5 from http://phpguru.org, though I'm sure phpmailer - another common class - has this capability also).

This class has the following setSMTPParams method ...

setSMTPParams($host = null, $port = null, $helo = null, $auth = null, $user = null, $pass = null)

Set $auth to true and set $user and $pass and all goes as planned.




0
 
LVL 40

Expert Comment

by:RQuadling
ID: 20364905
To send mail from different people, I use ...

ini_set('sendmail_from', 'address@site.com');

prior to any mail related activity.

You can also send SMTP to the recipients server rather than relaying it through your local server.

Using getmxrr() (Windows version attached as this is not supported natively on windows) you can find the actual SMTP server(s) for a particular email address and send it to that machine for processing.

So, something like ...

if (getmxrr('gmail.com', $a_MXHosts, $a_Weights)) {
 print_r($a_MXHosts);
 print_r($a_Weights);
}

outputs ...

Array
(
    [0] => gmail-smtp-in.l.google.com
    [1] => alt1.gmail-smtp-in.l.google.com
    [2] => alt2.gmail-smtp-in.l.google.com
    [3] => gsmtp163.google.com
    [4] => gsmtp183.google.com
)
Array
(
    [0] => 5
    [1] => 10
    [2] => 10
    [3] => 50
    [4] => 50
)

From this you can cycle through the MX Hosts, ini_set('SMTP', $a_MXHost[$i_Index]); and then send the email.

Or use the setSMTPParams($a_MXHost[$i_Index], ....) call.

If you wanted to AUTH yourself, YOU need to have an account on every server.

So, either use a local relay and you auth to it

OR

you send email directly to the recipients smtp server unauth'd, just like the rest of the internet does.




<?php

if (!function_exists('getmxrr')) {

	function getmxrr($s_HostName, array &$a_MXHosts = NULL, array &$a_Weights = NULL) {

		// Simulate all the required network activity by executing windows' NSLOOKUP.

		$s_NSLookup = shell_exec("nslookup -q=mx {$s_HostName} 2>nul");

		preg_match_all("'^.*MX preference = (\d{1,10}), mail exchanger = (.*)$'simU", $s_NSLookup, $a_MXMatches);
 

		// If there is something to return ...

		if (count($a_MXMatches[2]) > 0) {

			// Produce output arrays if they have been requested.

			$i_ArgCount = func_num_args();

			if ($i_ArgCount > 1) {

				array_multisort($a_MXMatches[1], $a_MXMatches[2]);

				switch ($i_ArgCount) {

					case 3 :

						$a_Weights = $a_MXMatches[1];

					case 2 :

						$a_MXHosts = $a_MXMatches[2];

				}

			}

			return True;

		} else {

			return False;

		}

	}

}

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simplest php form 3 59
ajax call to redirect to a different page with data from controller 7 44
Php pie charts 3 26
How do I put a session for a radio button? 4 12
Hello Friends, My friends and relatives always ask me how to delete all the various types of emails at once in our g-mail  or windows live account.  So I researched this topic to find a unique solution to this query.  Here it is for those who do …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

919 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

19 Experts available now in Live!

Get 1:1 Help Now