Pages: [1]   Go Down
Send this topic | Print
Author Topic: HOW TO: Send simple text mail with PHP fsockopen  (Read 1752 times)
fiate2000
Authority Member
****
Offline Offline

Posts: 682


WWW
« on: April 03, 2008, 08:15:43 PM »

After my domain's fsockopen was open open, I have been trying to get hold of a PHP script that can send simple text mails without any framework (PEAR etc). I could not find any on this forum.

The couple of scripts found on internet did not work. The following is a working script after some trial and error attempts with SMTP servers. I have also tried to indicate the reasons for failure of other scripts. Hope it helps.

Go to this page - http://fiate2000.110mb.com/work.php and then click on PHP Code - Send simple text mail with PHP fsockopen

While you are there, you can also look at other code examples.



Edit - code added to this post


Code:
<?php

$smtp_server
= "mail.mymailserver.com";
$mydomain = "any_text_here";


//get the following info from your mail server eg. au.yahoo.com

$port = 25; //SMTP PORT of mail.mymailserver.com
$username = "myusername"; $password = "mymail_password";


$sender = "myusername@mymailserver.com";

/*
Most respectable mail servers will reject outgoing mail
if $sender e-mail address does not belong to $username
*/

$sender_name = "My Name"; //Any name you fancy


$recipient = "your.friend@someother.com";
$subject = "the subject";
$content = "the message";

// SMTP connection

$handle = fsockopen($smtp_server,$port);
fputs($handle, "EHLO $mydomain\r\n");

// SMTP authorization
fputs($handle, "AUTH LOGIN\r\n");
fputs($handle, base64_encode($username)."\r\n");
fputs($handle, base64_encode($password)."\r\n");

// Send out the e-mail
fputs($handle, "MAIL FROM:$sender\r\n");
fputs($handle, "RCPT TO:$recipient\r\n");
fputs($handle, "DATA\r\n");
fputs($handle, "From: $sender_name<$sender>\r\n");

//you can use different e-maill address in above line, but most spam blockers will suspect this

fputs($handle, "To: $recipient\r\n");
fputs($handle, "Subject: $subject\r\n");
fputs($handle, "$content\r\n");
fputs($handle, ".\r\n");

//Don't ignore the period "." in line above. This indicates the end of your mail

// Close connection to SMTP server
fputs($handle, "QUIT\r\n");
?>

For more useful scripts  - you can visit my webpage.
« Last Edit: February 27, 2009, 11:29:29 PM by fiate2000 » Logged

andomray
Active Member
**
Offline Offline

Posts: 50



WWW
« Reply #1 on: April 06, 2008, 02:40:12 PM »

Very nice! Thanks for going through the whole trial and error system so we don't have to. It seems so often that the first thing one tries just doesn't accomplish what is done, so it's a darn good thing that the internet is so vast.
Logged

If you enjoy puns and being clothed, then check out my site of illustrated puns!
jonydread
Active Member
**
Offline Offline

Posts: 55


« Reply #2 on: May 14, 2009, 11:59:16 AM »

como funciona?
Logged
Løque
Hyper-Active Member
***
Offline Offline

Posts: 287



WWW
« Reply #3 on: May 14, 2009, 06:06:55 PM »

Thanks for this tutorial.  smiley
Logged

110MB ITALIAN SUPPORT MODERATOR
Pages: [1]   Go Up
Send this topic | Print
Jump to: