use PHP mail() to send out mail via remote SMTP server

Hi,
I"m not a scripter/web developer. I'm more likely a system admin guy.
Due to our demand to send out large amount of mails using php mail(). I need to setup a remote SMTP server so that it won't interfere with the main mail server which is also used by other businesses. I've done that also.
However, i will need to also test whether this would work by writing a PHP script to use that remote SMTP server to send out email.
I'm stuck here. i went over php mail() manual the the function does not have any parameters that allows me to state smtp host and port. Editing php.ini will interfere with other accounts which are hosted currently.

Can you please help me?

THanks so much!
valleytechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BenMorelCommented:
Hi,

The simplest way is to use the pear Mail class.
To install it from a console :
# pear install Mail

Then you can tell it to use the smtp server of your choice.
More parameters can be used to communicate with the smtp server, doc here :
http://pear.php.net/manual/en/package.mail.mail.factory.php

Regards,
Ben
<?php
    include('Mail.php');
    $mail = Mail::factory('smtp', array('host' => 'smtp.yourdomain.tld'));
    $mail->send('recipient@email.com', array('From' => 'sender@email.com', 'Subject' => 'Test Mail'), 'This is a test');
?>

Open in new window

0
valleytechAuthor Commented:
Thanks millions!
Is there another alternative not using pear Mail class! Coz I'm using php installed my cpanel :) quite hesitate to disturb current configuration

thanks!!!
0
hieloCommented:
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

BenMorelCommented:
Here is a VERY basic way to communicate with the smtp & send a sample mail.
Ben
<?php
 
while (ob_get_level()) ob_end_flush();
 
function BasicSMTP($Server, $Sender, $Recipient, $Subject, $Message)
{
	$Sock = fsockopen($Server, 25);
	
	if (is_resource($Sock))
	{
			echo fread($Sock, 1024);flush();
		fwrite($Sock, "HELO localhost\r\n");
			echo fread($Sock, 1024);flush();
		fwrite($Sock, "MAIL FROM: <$Sender>\r\n");
			echo fread($Sock, 1024);flush();
		fwrite($Sock, "RCPT TO: <$Recipient>\r\n");
			echo fread($Sock, 1024);flush();
		fwrite($Sock, "DATA\r\n");
			echo fread($Sock, 1024);flush();
		fwrite($Sock, "From: <$Sender>\r\n");
		fwrite($Sock, "To: <$Recipient>\r\n");
		fwrite($Sock, "Subject: $Subject\r\n");
		fwrite($Sock, "\r\n");
		fwrite($Sock, "$Message\r\n");
		fwrite($Sock, ".\r\n");
			echo fread($Sock, 1024);flush();
		fwrite($Sock, "QUIT\r\n");
			echo fread($Sock, 1024);flush();
			
		fclose($Sock);
 
		return true;
	}
 
	return false;
}
 
BasicSMTP('smtp.yourdomain.tld', 'sender@email.com', 'recipient@email.com', 'subject', 'message');
 
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hernst42Commented:
On unix you can use sendmail_path to call a script instead of the default sendmail which will send all mail from that one virtual server to the remote smtp-server without usinf local mail resources

/usr/bin/remotemail:
#!/bin/bash
env MAILRC=/dev/null host=remoteserver /usr/bin/nail -n

This requires nail to be installed. See man nail at the end for more options (like auth on the remote server)
0
phpintheusaCommented:
You can use this to send via smtp-auth:

http://phpmailer.sourceforge.net
0
phpintheusaCommented:
To get past being blacklisted for having open relays, you will need smtp-auth.
0
valleytechAuthor Commented:
i've been using qmail-smtpd. However i have not found anything useful regarding implementation smth authentication with qmail
Can you please help?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Protocols

From novice to tech pro — start learning today.