php code - Sending mail with external SMTP server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting php code - Sending mail with external SMTP server
# 1  
Old 09-10-2011
php code - Sending mail with external SMTP server

Here is the code:

Code:
<?php 
//new function 
 
function loadini($path) { 
$fp = fopen($path, "r"); 
$fpcontents = fread($fp, filesize($path)); 
fclose($fp); 
return $fpcontents; 
    } 
 
 
$to = "test@test.com"; 
$nameto = "notme"; 
$from = "test"; 
$namefrom = "Who From"; 
$subject = "Hello World Again!"; 
$message = loadini("message"); 
 
 
//SMTP + SERVER DETAILS 
/* * * * CONFIGURATION START * * * */ 
$smtpServer = loadini("logfile");; 
$port = "25"; 
$timeout = "30"; 
$username = loadini("users"); 
$password = loadini("password"); 
$localhost = "user"; 
$newLine = "\r\n"; 
/* * * * CONFIGURATION END * * * * */ 
 
//Connect to the host on the specified port 
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout); 
$smtpResponse = fgets($smtpConnect, 515); 
echo 'Connection response was: ' . $smtpResponse; 
 
if(empty($smtpConnect)) 
{ 
$output = "Failed to connect: $smtpResponse"; 
return $output; 
} 
else 
{ 
$logArray['connection'] = "Connected: $smtpResponse"; 
 
} 
echo 'Response from connection attempt was: ' . $smtpResponse; 
 
//Say Hello to SMTP 
fputs($smtpConnect, "EHLO $localhost" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['heloresponse'] = "$smtpResponse"; 
echo 'Response from HELO was: ' . $smtpResponse; 
 
//Request Auth Login 
fputs($smtpConnect,"AUTH LOGIN" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authrequest'] = "$smtpResponse"; 
echo 'Response from AUTH LOGIN was: ' . $smtpResponse; 
 
 
//Send username 
fputs($smtpConnect, base64_encode($username) . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authusername'] = "$smtpResponse"; 
echo 'Response from USERNAME was: ' . $smtpResponse; 
 
//Send password 
fputs($smtpConnect, base64_encode($password) . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authpassword'] = "$smtpResponse"; 
echo 'Response from PASSWORD was: ' . $smtpResponse; 
 
//The Email 
fputs($smtpConnect, 'DATA MIME-Version: 1.0' . $newLine); 
fputs($smtpConnect, 'Content-type: text/html; charset=iso-8859-1' . $newLine); 
fputs($smtpConnect, 'To: ' . $nameto . '<' . $to . '>' . $newLine); 
fputs($smtpConnect, 'From: ' . $namefrom . '<' . $from . '>' . $newLine); 
fputs($smtpConnect, 'Subject: ' . $subject . $newLine . $newLine); 
fputs($smtpConnect, $message . $newLine); 
fputs($smtpConnect, '.' . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['data2response'] = "$smtpResponse"; 
echo 'Response from THE EMAIL was: ' . $smtpResponse; 
 
 
// Say Bye to SMTP 
fputs($smtpConnect,"QUIT" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['quitresponse'] = "$smtpResponse"; 
echo 'Response from QUIT was: ' . $smtpResponse; 
?>

When I manually connect to SMTP and send email it works but when I use this script ... fails. Any help?

---------- Post updated at 01:56 PM ---------- Previous update was at 03:29 AM ----------

I'm guessing the syntax is wrong but i cannot detect where. Anyone has figure it out yet?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

External SMTP server

Hi, We current use an email/hosted exchange server (provided by 3rd party company). Our production DNS (RH5) server has got the MX rec configured for this 3rd party mail relay server. So in order to resolve hostnames to send outbound mails an A record entry is also required on the external... (1 Reply)
Discussion started by: Duffs22
1 Replies

2. Solaris

OutBound SMTP Server Mail issue

Hi all I have a Live SMTP server for outbound mails ( Mail being sent from my organization to outside domains). A large chunk of mails are not being sent and reside in /var/spool/mqueue directory. The header of there mails are: From: Mail Delivery Subsystem <MAILER-DAEMON> How can I... (0 Replies)
Discussion started by: Junaid Subhani
0 Replies

3. Shell Programming and Scripting

Configuring smtp settings and then sending the mail through shell script

I have make an menu in which first option is to start and second is to stop the services echo "Please enter the appropriate choice for doing the operations" echo " 1) STOP Services 2) START Services case $choice in 1) ... (1 Reply)
Discussion started by: punpun66
1 Replies

4. UNIX for Dummies Questions & Answers

Sending through an external e-mail account

Hi guys. After several Google hours, i've failed to find a solution to my problem, maybe my google skills arent that good - but i hope someone could help me figure this out. I'm running a ubuntu server 11.04 with postfix installed. I have installed Request tracker 4 and configured it to read... (2 Replies)
Discussion started by: spirm8
2 Replies

5. UNIX for Dummies Questions & Answers

Can't configure sendmail to send mails to external SMTP Server

Hi all,I know, that this is very common issue, but I can't find where is the problem... I have Solaris 10 installed.I want to send mail to external SMTP server, like:echo "letter body"| mailx -s "subject" test@test.comBut I donno what kind of changes I have to do in /etc/hosts and sendmail.cf or... (5 Replies)
Discussion started by: nypreH
5 Replies

6. Shell Programming and Scripting

Getting error while sending mail via smtp

Hi All, I used before a simple routine code in tcl to send mail. package require smtp package require mime package require Tcl proc send_simple_message {recipient email_server subject body} { set token mime::setheader $token Subject $subject smtp::sendmessage $token \ ... (0 Replies)
Discussion started by: zivsegal
0 Replies

7. UNIX for Advanced & Expert Users

How to configure Linux box with my SMTP mail server

Hi all, I need to send mail to a particular email id using a local mail server.This mail shall contain the content of a file. I don't have any idea how to do it. Can anyone guide me ,where to start proceeding for it. Any guide or study material would appreciated. (1 Reply)
Discussion started by: amit_kv1983
1 Replies

8. UNIX for Dummies Questions & Answers

sending mail via smtp

Hi Gururs, I have a process on a middelware (SAP XI) that is runnung on HP-UX. This Process is creating a flatfile. Now I want to call a script within the process that sends the created flatfile as attachment via Mail using a smpt-server. The script should have the following input parameters:... (2 Replies)
Discussion started by: elko.hasse
2 Replies

9. SuSE

How to configure Suse Enterprise Linux 9.0 for sending mails to smtp server...???

Hi All, I have a system running with Suse Enterprise Linux 9.0. xyz# uname -a Linux xyz 2.4.21-291-smp #1 SMP Tue May 24 14:07:45 UTC 2005 i686 unknown I would like to configure postfix on this system for sending mails as a root user using "mail" command to smtp server (Windows System)... (5 Replies)
Discussion started by: jumadhiya
5 Replies

10. AIX

Problem sending mail to an external domain

On one of our AIX servers, I'm having problems sending email to an external domain. I'm not a SYSADM and not sure how to troubleshoot this issue. Any help will be greatly appreciated. The following mail components are installed on the server $ lslpp -l bos.mh Fileset ... (2 Replies)
Discussion started by: voorkey
2 Replies
Login or Register to Ask a Question