• (2 Pages)
  • +
  • 1
  • 2

Send Emails Using Php (SMTP Direct) Rate Topic: ***** 3 Votes

#1 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5167
  • View blog
  • Posts: 26,850
  • Joined: 10-May 07

Post icon  Posted 25 October 2007 - 10:12 AM

Send Emails Using Php (SMTP Direct)

This is an alternative way to generate & send an email under php, rather than using the pre-defined mail() function.

Reasons:
In what situations would you not want to use something as simple as the mail function?

1.) Your php/web server does not offer mail.
On our internal network, the mail server exists on a server other than the web server.
Therefore, mail() will fail to send since
a.) port 25 is blocked
b.) sendmail is disabled

2.) Your mail server doesn't use the default port 25 for sending email.
If you are running a server on non-commercial service, you know what I'm talking about =-)
This can, however, be changed via the php.ini configuration file.

3.) Because you can.
It allways feels good (maybe even better) when you know the how's & why's to your equipment & software. So why just send an
e-mail when you can build one?!

4.) Your sendmail_from value needs to change on the fly.
Maybe you host multiple accounts, & one sendmail_from value is not enough. Maybe you are hosting something much more
advanced & this single setting just isn't enough.
Auto-responders, Auto-forwards, the list is endless.

The setup:
Since I use qmail as my MTA (mail transfer agent) that's the example you'll get. Please refer to the documentation that came with
your MTA, should you be setting up something different.

1st, we need to define the function to use instead of mail().
<?php

function authgMail($from, $namefrom, $to, $nameto, $subject, $message) {



As you can see, we've allready got a few more arguments than mail() has to offer.

authgMail <- my chosen function name, you can alter this as you please. Just make sure that it isn't a keyword or existing function.

Now to setup those values...
$smtpServer = "192.168.xxx.xxx";   //ip address of the mail server.  This can also be the local domain name
$port = "25";					 // should be 25 by default, but needs to be whichever port the mail server will be using for smtp 
$timeout = "45";				 // typical timeout. try 45 for slow servers
$username = "sales@mydomain.com"; // the login for your smtp
$password = "myPA$$";			// the password for your smtp
$localhost = "127.0.0.1";	   // Defined for the web server.  Since this is where we are gathering the details for the email
$newLine = "\r\n";			 // aka, carrage return line feed. var just for newlines in MS
$secure = 0;				  // change to 1 if your server is running under SSL



Those should be the only values that you need to change to match your own internal settings.
The rest is the functionality of the server & those values. The variable $smtpResponse is going to let you know what's going on.
You can use it to show you every step along the way, or just show errors reported back from the mail server.
Now, lets build that e-mail!

//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
   $output = "Failed to connect: $smtpResponse";
   echo $output;
   return $output;
}
else {
   $logArray['connection'] = "<p>Connected to: $smtpResponse";
   echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}

//you have to say HELO again after TLS is started
   fputs($smtpConnect, "HELO $localhost". $newLine);
   $smtpResponse = fgets($smtpConnect, 4096);
   $logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";

//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";

//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";

//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";

//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";

//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}




Ok, that wasn't so bad... Now we can use php to error check the values passed in from a form, to verify that the server is not going
to get some bogus value, or empty records & freak out.
$err=0;  // so far, so good
$err_msg="";

if($_POST['name_']!="") { echo $_POST['name_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include your name";
}

if($_POST['day_phone_']!="") {echo $_POST['day_phone_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include a daytime phone number.";
}
if($_POST['add_']!="") { echo $_POST['add_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include your address.";
}
if($_POST['city_']!="") { echo $_POST['city_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include the city.";
}
// Check for the existence of an AT symbol inside the email.
if (strpos($_POST['email'],"@")) { echo $_POST['email']."<br>"; } 
else {
  $err=1;
  $err_msg="You must include a current email address.";
}
if($_POST['email']!="") { echo $_POST['email']."<br>"; }
else {
  $err=1;
  $err_msg="You must include your e-mail address.";
}

echo $err_msg;

if($err<=0) {
  $from="sales@mydomain.com";
  $namefrom="Internal Sales Dept. of mydomain.com";
  $to = "internal_user@mydomain.com";
  $nameto = "internal_user";
  $subject = "Email from My Domain";
  $message = "Email from My Domain";
  // this is it, lets send that email!
  authgMail($from, $namefrom, $to, $nameto, $subject, $message);
}
else {
  echo "<p /> This form was not filled out correctly, please correct any mistakes.";
}

?>



In Conclusion:
As you can see, it very easy to send an email using php & connecting directly to your smtp server.
In addition you can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers.
If you don't receive an email using this script, then you may have installed PHP incorrectly, you may not have permission to send emails, or you may have misconfigured your MTA.
Before running this script, verify that your current MTA is working. That way you can trouble shoot this script, knowing that your MTA is set to recieve incomming connections.

Is This A Good Question/Topic? 3
  • +

Replies To: Send Emails Using Php (SMTP Direct)

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Posted 28 October 2007 - 02:36 PM

Thanks.
It's Clear, Simple, quick, and ...
I love it.
Was This Post Helpful? 0
  • +
  • -

#3 yuzz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-November 07

Posted 19 November 2007 - 10:16 AM

Hi! Nice article, only one question: Isn't using an IP address in the HELO wrong? I thought it had to be a hostname (FQDN)?

This post has been edited by yuzz: 19 November 2007 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5167
  • View blog
  • Posts: 26,850
  • Joined: 10-May 07

Posted 19 November 2007 - 11:09 AM

View Postyuzz, on 19 Nov, 2007 - 11:16 AM, said:

Hi! Nice article, only one question: Isn't using an IP address in the HELO wrong? I thought it had to be a hostname (FQDN)?

When the mail recipient receives the email, they will either resolve the FQDN to an ip address, or accept the ip address. If the recipients mail server issues a reverse DNS lookup, it'll use the IP address (resolved or given) to lookup the FQDN. I'm sure for security sake, that it's possible to not accept email from a server issues an IP Address in the HELO. The example I used here is sending the email inside the network, so it isn't an issue. Good point though, I'll continue to check into it further.
Was This Post Helpful? 0
  • +
  • -

#5 Allaboutdatingsites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-January 08

Posted 14 January 2008 - 02:32 AM

Hello, Really Great Tutorial. I am wondering if you could give an example of how to "set" the bcc header? You mentioned it at the end of the article but I don't have a clear understanding of how to do it and the five different ways I have tried don't work. I do have it working as written, though I had to use EHLO rather than the HELO listed in the post, something about the AUTH requirement... Thanks in advance for any pointers on how to get the bcc piece to work.

Warmest regards,

allaboutdatingsites
Was This Post Helpful? 0
  • +
  • -

#6 muhammad0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 08

Posted 21 May 2008 - 12:58 AM

:^: that's really nice tutorial and helped me loads. however i am more interested in tutorial which help me in retrieving mails from mail server using imap functions. also provide tutotial about 'user agen' and MTA. :D thankyou
Was This Post Helpful? 0
  • +
  • -

#7 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Posted 06 January 2009 - 12:10 AM

wow good tut thanks alot
Was This Post Helpful? 0
  • +
  • -

#8 renaldo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-December 08

Posted 23 October 2009 - 01:25 PM

Awesome! This is my first project working with internal server clusters and a less than adequate information sources. I think this may solve my problem. Also, good job on the clarity of the information.
Was This Post Helpful? 0
  • +
  • -

#9 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Posted 09 November 2009 - 04:12 PM

Great tutorial if I didn't already have my own webmail php set up this would be the way that I would go. Very well written!
Was This Post Helpful? 0
  • +
  • -

#10 Guest_xingbing*


Reputation:

Posted 07 June 2010 - 03:06 AM

hi,

How i can send 2 mails on 2 e-mail adress?

because i have a form and befor i send, the script show in the database.

i come from germany.
sry for my bad english^^
Was This Post Helpful? 1

#11 Guest_Dadah*


Reputation:

Posted 27 July 2010 - 11:37 PM

Sir, i tried using your script I was able to connect to the smtp server, but i'm not receiving any mail.. hmm.. any idea? or do i have to install something in order to finally send a mail? like php mailer or etc? help please sir..
Was This Post Helpful? 1

#12 chris1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 12

Posted 04 June 2012 - 11:44 PM

I am a newbie and i would like to make a automatic mail system which is really difficult with less knowledge. I would appreciate it if you no2pencil could work with me a php system out. I want to create for my members a newsletter system which it supposed to keep them updated.

What the script/php should look:

1. Server limit 15 mails per min which i can't send 100 mails at once. I have 60 members and everyone wants the newsletter. There are also new members who join.
2. The newsletter should land in the Inbox not in the spam box.

3. The system need to have security so that the 60 member e-mails don't show to the others.
Was This Post Helpful? 0
  • +
  • -

#13 chris1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 12

Posted 04 June 2012 - 11:55 PM

Hey no2pencil,
Are the 4 php codes supposed to be in one PHP file or not? Like all in one?

What is SMTP or where do i get those?
4	$username = "sales@mydomain.com"; // the login for your smtp
5	$password = "myPA$$";           // the password for your smtp

Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5167
  • View blog
  • Posts: 26,850
  • Joined: 10-May 07

Posted 05 June 2012 - 12:00 AM

View Postchris1234, on 05 June 2012 - 02:55 AM, said:

What is SMTP or where do i get those?
4	$username = "sales@mydomain.com"; // the login for your smtp
5	$password = "myPA$$";           // the password for your smtp

SMTP is simple mail transfer protocol

If you don't have those values, then you don't have the ability to send email over the SMTP server.
Was This Post Helpful? 0
  • +
  • -

#15 gnsnomos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-November 12

Posted 14 November 2012 - 02:51 AM

Hello,

I've tried your code but all I get is:
connection accepted

Continuing

Array ( [connection] =>
Connected to: [heloresponse2] => [authrequest] => [authusername] => [authpassword] => [mailfromresponse] => [mailtoresponse] => [data1response] => [data2response] => [quitresponse] => [quitcode] => )



I've figured out that in this line:
 $smtpResponse = fgets($smtpConnect, 4096); 
$smtpResponse doesn't get any value...

- Server is running under ssl

Can you please help me?

Thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2