9lessons programming blog
Loading Search
9lessons Bring Friends
Wall Script
Follow Me:
Monday, October 12, 2009

Send Mail using SMTP and PHP.

This post about "Sending Mail using SMTP and PHP". Now you can send emails with SMTP authentication using this script. Every mail needed server authentication, So you have to buy mail server. It's very useful you can implement this on your web projects.

Send Mail using SMTP and PHP
This tutorial contains three files.

- Index.php
- SMTPconfig.php // SMTP Server Cofiguration
- SMTPClass.php // SMTP Mail Sending Class


Download Script



SMTPconfig.php
You have to change SMTP server details.
<?php
//Server Address
$SmtpServer="127.0.0.1";
$SmtpPort="25"; //default
$SmtpUser="username";
$SmtpPass="password";
?>

SMTPclass.php
SMTP mail sending class.
<?php
class SMTPClient
{

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{

$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;

if ($SmtpPort == "")
{
$this->PortSMTP = 25;
}
else
{
$this->PortSMTP = $SmtpPort;
}
}

function SendMail ()
{
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
{
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");
$talk["hello"] = fgets ( $SMTPIN, 1024 );
fputs($SMTPIN, "auth login\r\n");
$talk["res"]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpUser."\r\n");
$talk["user"]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpPass."\r\n");
$talk["pass"]=fgets($SMTPIN,256);
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");
$talk["From"] = fgets ( $SMTPIN, 1024 );
fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");
$talk["To"] = fgets ($SMTPIN, 1024);
fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
$talk["send"]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT ...
fputs ($SMTPIN, "QUIT\r\n");
fclose($SMTPIN);
//
}
return $talk;
}
}
?>

index.php
<?php
include('SMTPconfig.php');
include('SMTPClass.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['sub'];
$body = $_POST['message'];
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
}
?>
<form method="post" action="">
To:<input type="text" name="to" />
From :<input type='text' name="from" />
Subject :<input type='text' name="sub" />
Message :<textarea name="message"></textarea>
<input type="submit" value=" Send " />
</form>

Latest 20 Articles
Comments
{ 126 comments }
Anonymous said...

Thanks. Very useful.

scvinodkumar said...

Nice coding...

Anonymous said...

a piece of great code, thanks.

by the way, is there method to default the coding of the email as unicode(UTF8)?

gustavo said...

how can i add cc: ???

beezy said...

does this work for gmail smtp servers?

Srinivas Tamada said...

@beezy

No

Anonymous said...

You need to use unsecure mail servers that use port 25. Check your IP for details (if you have an email account with them, look under SMTP configuration for Outlook, Thunderbird, etc)

Anonymous said...

does it work for aol smtp server?

Premasis said...

Notice: Undefined variable: HTTP_HOST in C:\wamp\www\1010\SMTPclass.php on line 30

Anonymous said...

Thanks,
I really search for this script form last 1 week & i fount this script work fine for me.
Thanks again.

Sayf said...

Thank you very much for this useful class, how can we use it to send HTML emails?


@gustavo

It's possible to add cc.

1- You need to change first you index.php:

You've got to add a new field called cc in your form. Then recover it to $cc from POST Array (within the other variables as $from, $to ...). Finally, you must add $cc between $to and $subject in the SMTPClient function call.

2- Then you go to change SMTPClass.php:

You must add $cc to SMTPClient function definition the same way you did in the function call. THen add a new variable called $this->cc where you put the actual $cc.

Duplicate the RCPT TO command using your new variable $this->cc and get the response in the $talk[...] array:

fputs ($SMTPIN, "RCPT TO: <".$this->cc.">\r\n");
$talk["Cc"] = fgets ($SMTPIN, 1024);

Finally, put your variable on the mail header, by adding the following line right after its "To:" similar line under DATA command:
Cc: <".$this->cc.">\r\n

The final DATA command is:

fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, "To: <".$this->to.">\r\nCc: <".$this->cc.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
$talk["send"]=fgets($SMTPIN,256);

That's it! You can do it the same way with BCc too...

Anonymous said...

i'm getting this error:
Notice: Undefined variable: HTTP_HOST in /var/www/vhosts/cutrin.lt/httpdocs/evelita/mailtest/SMTPClass.php on line 30

Anonymous said...

Thanks youuuuuuuuuuu.....

Anonymous said...

this not support html mail

alhakimi said...

problem when I send subject in arabic
How i solve it?

narasimha. said...

Thankq very Much.
God bless you

Anonymous said...

SMTPClass.php is not SMTPclass.php
Your script and include files names do not match,

Md. Ziaul said...

Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in D:\xampp\htdocs\NetBill Manager\admin\SMTPClass.php on line 28

Fatal error: Maximum execution time of 60 seconds exceeded in D:\xampp\htdocs\NetBill Manager\admin\SMTPClass.php on line 28

Anonymous said...

please help me to include headers
Ex: $headers = "Content-Transfer-Encoding: 8bit\r\n";
$headers .= 'Content-Type: text/html; charset="iso-8859-1"'."\r\n";

คอนโด said...

Thanks so much.

Anonymous said...

Notice: Undefined variable: HTTP_HOST in .../SMTPclass.php on line 30

Anonymous said...

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\SMTPclass.php on line 31

Warning: fsockopen() [function.fsockopen]: unable to connect to 192.168.1.3 :25 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in C:\wamp\www\SMTPclass.php on line 31

Anonymous said...

i copied the three php programs and wen i try to run the index.php file it in d wamp server,
its gives d following error:
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\SMTPclass.php on line 31

Warning: fsockopen() [function.fsockopen]: unable to connect to 192.168.1.3 :25 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in C:\wamp\www\SMTPclass.php on line 31

//i hav no clue wts wrong....m a newbie....i hav been tryn dis for a while but widout success....

Smit Shah said...

Nice post..

You can set header by this:

fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n".$this->headers."\r\n\r\n".$this->body."\r\n.\r\n");

And pass the header in constructor as an argument and than set that like:
$this->headers = $headers;

And that's all !!!

Anonymous said...

Reza Said:
how can I send email to more than one person on one textbox ?

Anonymous said...

Thanks Srinivas for this nice post. I reformatted your SMTPClient class and added a header variable so one has the option to send HTML emails.


SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
$this->newLine = "\r\n";

if ($SmtpPort == "") {
$this->PortSMTP = 25;
} else {
$this->PortSMTP = $SmtpPort;
}
}

function SendMail (){
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) {
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");
$talk["hello"] = fgets ( $SMTPIN, 1024 );

fputs($SMTPIN, "auth login\r\n");
$talk["res"]=fgets($SMTPIN,1024);

fputs($SMTPIN, $this->SmtpUser."\r\n");
$talk["user"]=fgets($SMTPIN,1024);

fputs($SMTPIN, $this->SmtpPass."\r\n");
$talk["pass"]=fgets($SMTPIN,256);

fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");
$talk["From"] = fgets ( $SMTPIN, 1024 );

fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");
$talk["To"] = fgets ($SMTPIN, 1024);

fputs ($SMTPIN, "RCPT TO: \r\n");
$talk["Bcc"] = fgets ($SMTPIN, 1024);

fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );

//Construct Headers
$headers = "MIME-Version: 1.0" . $this->newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $this->newLine;
$headers .= "From: <".$this->from.">". $this->newLine;
$headers .= "To: <".$this->to.">". $this->newLine;
$headers .= "Bcc: newLine;
$headers .= "Subject: ".$this->subject. $this->newLine;

fputs($SMTPIN, $headers."\r\n\r\n".$this->body."\r\n.\r\n");

$talk["send"]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT ...
fputs ($SMTPIN, "QUIT\r\n");
fclose($SMTPIN);
//
}
return $talk;
}
}
?>

Anonymous said...

Anyone know a way to make this code work with Gmail? Dont you just need to allow SSL somehow and use port 465 or 587?

Actual Soft Solutions said...

Mails are going to Spam box. can you help me resolve it.

ali said...

I am getting this error please help me to solve


Undefined variable: HTTP_HOST in C:\wamp\www\smtp\SMTPclass.php on line 39

Zoinks! Graphics said...

Does this have any special server-end requirements to run? My host (DreamHost) is saying that SMTP like this requires PEAR and the fact that I can't get it to work seems to back that up. Not getting any errors like some users above, just no emails coming thru. Any thoughts?

Brad said...

heh.. I had this working last weekend.. I think my CPanel server either upgraded exim or something because this script is now sending out emails but when it hits the exim server im getting errors:
2011-09-10 08:17:13 H=area (localhost) [127.0.0.1] rejected MAIL notifications@email.tld: Authentication failed

Thoughts on that? I know its not the php issue, or is it? Running 5.3.8 of PHP

chansi said...

thnx for this class ,
but how can i get a confirmation message
if mail fails to b delivered then how can i get to know about that ??

Judhisthria Sahoo said...

Hi,

All..It is best code which help me very bad time...Thanks you all guys..and for your valuable posting..

I have seen one post is no pending for help..

Undefined variable: HTTP_HOST in C:\wamp\www\smtp\SMTPclass.php

Who are waiting for the above error code help..
plz see the bellow

----------
Please replace the "$HTTP_HOST" to "$this->SmtpServer".

Then enjoy the fun with you..

judhisthira sahoo
India

Anonymous said...

Thank you

it is excellent .

But how to set server inside config file to send Email to yahoo ?

Steven said...

Nice job,

this code worked beautifully for me to connect to godaddys smtp

YISHIyish said...

I have tried this and its working properly. Is it possible to add attachment when sending an email using smtp? Thanks. :)

Nafees Mansoor said...

nice script. but can u plz help me out on the attachment issue?

Anonymous said...

to solve problem of "Undefined variable: HTTP_HOST"
replace it with "getenv('MyHost')", it worked well with me...


and i am reallly grateful for this post..thank you soooo much (^_^)

Anonymous said...

regarding the SMTPconfig.php... ca we put mail.domain.com at $SmtpServer? something like this:


any help?

5e02b01c-21bf-11e1-ab39-000bcdca4d7a said...

The Bcc or Cc seem to work however the received email also shows who the Bcc/Cc is. For Bcc this is a problem b/c it's not BLIND. The following is what my hosting company said about this:

"It looks as your script is adding the BCC header of the message. at the wrong place. We would recommend that you check the configuration of the script that you are using to send the message. "

Anonymous said...

Thanks a lot. You are the man!

Anonymous said...

the line

$talk["hello"] = fgets ( $SMTPIN, 1024 );

Would be better as

$talk["hello"] = stream_get_line( $SMTPIN, 1024 );

as the server can return a multi line greeting which can still be returned in many of the following fget commands rather than getting what you would expect.

Anonymous said...

Very very thankful for your valuable post

Anonymous said...

Thank you so much!! That's what i need.. very3 thank you...

Anonymous said...

I copy and paste code and make some change the script run successfully but email not received by client.

please help
thank in ad..

Anonymous said...

Works like a charm thanks!

Elite Web Services said...

index.php downloads instead of opening....

Anonymous said...

@Elite Web Services: you have to open the file on a web server

Anonymous said...

Hello. Nice class. How can i send mail to multiple users? i use such codes: fputs ($SMTPIN, "RCPT TO: , , \r\n");
But it deliveres only to first mail, to mail-1.
I open delivered email in mail-1, and in TO section all mails are shown. To: you,mail-2,mail-3.
But mail-2 and mail-3 don't get email.

Ju Work said...

Hello,i used reformatted SMTPClient class,which is send HTML email, But my email not in HTML format and did not found content-type on message source. could anyone help ?

Ju Work said...

Sorry, i did some mistaken on header. now already work in accordingly. Thank you !

manojyadav said...

Sorry, i did some mistaken on header. now already work in accordingly. Thank you !

Anonymous said...

Okay, my solution for HTML and a Bcc which you could change to Cc easily.

index.php

top of the page:
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $bcc, $subject, $body, $headers);

You will also use this structure later on in index.php when calling the function.

SMTPClass.php

Replaces the function line
function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $bcc, $subject, $body, $headers)

Add this a few lines down under the rest of them

$this->headers = $headers;
$this->bcc = $bcc;

Add this below the same one call Data
fputs ($SMTPIN, "RCPT TO: <".$this->bcc.">\r\n");
$talk["Bcc"] = fgets ($SMTPIN, 1024);

Then replace this a few lines below:
fputs($SMTPIN, "To: <".$this->to.">\r\nBcc: <".$this->bcc.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n".$this->headers."\r\n\r\n".$this->body."\r\n.\r\n");

Then just set the variables up in the index.php with your values however you like

Anonymous said...

Alongside that, I forgot to mention in index.php you'll need to add this just before the function is called:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

This will send it as HTML

Anonymous said...

Thanks alot it still pending when i send email to gmail !

Anonymous said...

problem when I send subject in arabic
How to solve it?

Anonymous said...

[data] => 500 Syntax error, command unrecognized [send] => 250 OK.
This is what am getting in my array SMTPChat and cant understand why?

Anonymous said...

Warning: fsockopen() [function.fsockopen]: unable to connect to mail.bumaindonesia.com:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\Program Files\xampp\htdocs\Email\SMTPclass.php on line 28

Fatal error: Maximum execution time of 60 seconds exceeded in C:\Program Files\xampp\htdocs\Email\SMTPclass.php on line 28

Anonymous said...

Notice: Undefined variable: HTTP_HOST

Anonymous said...

Thanks!

Anonymous said...

Great PHP I love it.
canyou plase show us how to send a attachment like a picture orother file.
this make the cookies better.
best regards
Gil

Anonymous said...

How Can BCC part can be added to this and email can be sent group of people at once?

Anonymous said...

Notice: Undefined variable: HTTP_HOST in C:\xampp\htdocs\SMTPclass.php on line 30

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\SMTPclass.php on line 31 error

adil said...

hi all i am getting an SMTP Error: Could not connect to SMTP host. my mail function is


function sendMailSmtp($email, $meaasge, $subject, $attachment='') {
$mail = new PHPMailer();
$body = $meaasge;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.a3spl.com";
$mail->Port =25;
$mail->Username = "xyz@a3spl.com";
$mail->Password = "xyzxyz";
$mail->From = "XYZ";
$mail->FromName = "XYZ";
$mail->Subject = $subject;
$mail->AltBody = "This is the body when user views in plain text format";
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);

if(trim($attachment) != ''){
$mail->AddAttachment("uploadUserData/download/".$attachment);
}
$mail->AddAddress($email, "First Last"winking smiley;

$mail->IsHTML(true);

if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
return FALSE;
} else {
return true;

}
}

plz help mee soon

Innokentiy said...

Ok, after hours of struggling i have working solution with attachments...

Here we go (SMTPClass.php):

copy/paste this:

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body, $filename)
{
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers = "MIME-Version: 1.0" . "\r\n"."Content-Type: multipart/mixed;" . "\r\n"." boundary=\"{$mime_boundary}\"";

$fileatt_name = "$filename"; // Filename that will be used for the file as the attachment
$fileatt_type = "application/octet-stream"; // File Type

$handle = fopen($fileatt_name,'rb');
$data = fread($handle,filesize($fileatt_name));
fclose($handle);
$data = chunk_split(base64_encode($data));

$body_with_att = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $body . "\n\n" . "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";

Instead you current one and just before there "$this..." starts

change: "$this->body = $body;" to: "$this->body = $body_with_att;"

check that you have exact contents for this line:
fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n".$this->headers."\r\n\r\n".$this->body."\r\n.\r\n");

(index.php):

Copy/paste this instead of "$SMTPMail = new SMTPClient ...":

$filename = "mpsbox.tgz";
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body, $filename);

remove any headers from index.php, if added one's for html support.

Good luck!

Anonymous said...

Hi ,

I`m getting 530 error .
Reason: Remote host said: 530 SMTP authentication is required.

Anonymous said...

i cannot send mail...please help..
tried the sript.. changed the server setails etc.. but mail not received

Sonu said...

Undefined variable: HTTP_HOST in C:\wamp\www\smtp\SMTPclass.php on line 39

nav yug said...

Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:25 (No connection could be made because the target machine actively refused it. ) in C:\wamp\www\check\mail\SMTPmail\SMTPClass.php on line 35
Call Stack
# Time Memory Function Location
1 0.0012 369880 {main}( ) ..\Index.php:0
2 0.0022 395552 SMTPClient->SendMail( ) ..\Index.php:11
3 0.0022 395584 fsockopen ( ) ..\SMTPClass.php:35

( ! ) Notice: Undefined variable: talk in C:\wamp\www\check\mail\SMTPmail\SMTPClass.php on line 68
Call Stack
# Time Memory Function Location
1 0.0012 369880 {main}( ) ..\Index.php:0
2 0.0022 395552 SMTPClient->SendMail( ) ..\Index.php:11
To :

Unknown said...

Hi guys. I am new to php coding and creating contact form for the first time.
I tried sending mail from my domain to gmail through above code but that didnt work for me.

I think i am confused because i actually dont know how people generally use contact form?

How contact form should work ideally..it should send email like info@mydomain.com to which emailid? I am confused who will be sender and receiver here?

Anonymous said...

if ('SMTPconfig.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Direct File Access Prohibited');
add above into head of SMTPconfig.php to prevent direct access.

Anonymous said...

Thanks for Srinivas for the nice article and code snippet. I did exactly what you have mentioned but i am not able to send mail using SMTP option.
Do i need to install any mail package?

VP

Diego said...

Hello Srinivas Tamada
First of all, thanks a lot for this great script!!
It works like a charm!!
I need to add something very simple, but really don't know how to do it since my strong is not php...
I need to perform an echo check to know if the email has been sent correctly or not, for example:

$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['sub'];
$body = $_POST['message'];
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
}

if (SendMail)
{
echo "success";
}
else
{
echo "error";
}

I know this doesn't work...echos success always, but I need something like this...any ideas?
Thanks a lot in advance!!
Cheers!

Anonymous said...

Hi Diego,

After $SMTPChat = $SMTPMail->SendMail(); put something like:
$sent = true;

then instead of if(SendMail)use if($success == true) {

Anonymous said...

Thanks man, had this problem with cent os + mantis (new installation wich I didn't made). You helped me good !

Thx !

Zeihan Aulia said...

i can connect smptp,but I can't receive the email I sent, why?
whats the problem?

Imtiyaz Ahmad said...

unable to use ,getting warning as
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in XXXXX

Please help.

Anonymous said...

but its not working for gmail smtp plz
help me

Anonymous said...

gd coding but it is not working....so plz help me

Anonymous said...

i can connect smptp,but I can't receive the email I sent, why?
whats the problem?

Anonymous said...

I am having the same problem as Zeihan Aulia:

"i can connect smptp,but I can't receive the email I sent, why?
whats the problem?"

I have put debug code throughout and found that everything appears to execute successfully, but I never actually receive any emails. Any thoughts??

Anonymous said...

i am entered in textarea maximum 729 characters to enter button click success message and received the mail. but 730 or more than characters entered mail send but not received the mail. why this reason not send on mail. please help

Arun Kumar said...

First of all thanks for your nice codings. I used your emailing code and its working fine.

Please also help me how to mention FromName in the email coding.

currently if David Clark is mailing from david@gmail.com then FromName is comming as "david" But I want its name "David Clark".

so please help me.

Anonymous said...

Notice: Undefined variable: HTTP_HOST in E:\wamp\www\lastmail\SMTPClass.php on line 38

Anonymous said...

Hello, very nice and useful. Thanks for the great script.
1.How can i send mail to multiple users?
2.I am able to do CC or BCC.
3.I would like to add multiple users in TO: , not using cc or bcc.
Help me. thanks much

Ioannis Sarantopoulos said...

Hello,

At the end of index.php I've added this: print_r($SMTPChat);
All I get is Array ( [hello] => [res] => [user] => [pass] => [From] => [To] => [data] => [send] => ) why is this happening?

Anonymous said...

Thanks ! Nice way to send mail

-Anup

Amith said...

Very useful thx

Anonymous said...

Notice: Undefined variable: HTTP_HOST in C:\wamp\www\SMTPmail\SMTPClass.php
aidez moi svp

Anonymous said...

its still. I have tried your code for sending html. it is not sending as html email

Icare said...

Hello!
I'm french and i want to sent mail with accent!
How can I do this?

Please help me!

Thank you!!

Icare said...

Please Help me! It is very urgent! TT_TT

Anonymous said...

can you post full code to send as HTML mail

Icare said...

I have found alone...^^

You can had this code next to $this->subject\r\n : Content-Type: text/plain; charset={UTF-8}\r\n

So finnaly, it is (for example):
fputs($SMTPIN, "To: <".$this->to.">\r\nCc:<".$this->cc.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n"."Content-Type: text/plain; charset={UTF-8}\r\nComments :".$this->body."\r\n.\r\n");

So, enjoy it! And really thank you!!!

parshant aggarwal said...

i'm getting this error:
Warning: fsockopen() [function.fsockopen]: unable to connect to 173.194.79.108:465 (Connection refused) in /home/content/61/4521261/html/SMTPmail/SMTPClass.php on line 35

please help me.

Icare said...

Hello! There is a fault!

You must add this code: $this->subject\r\n : Content-Type: text/plain; charset=UTF-8\r\n
(There is no accolade, i don't know if you understand...)
So finally, it is:

fputs($SMTPIN, "To: <".$this->to.">\r\nCc:<".$this->cc.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n"."Content-Type: text/plain; charset=UTF-8\r\nComments :".$this->body."\r\n.\r\n");

So it's all, I think!! Bye!!

phpspider said...

Very nice script.

Krinal patel said...


Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\test\SMTPmail\SMTPClass.php on line 39

Anonymous said...

Error :Notice: Undefined variable: HTTP_HOST in C:\xampp\htdocs\SMTPclass.php on line 30
plzz help

gudz said...

i tried this. nothing happens..

Anonymous said...

For the Error:

Error :Notice: Undefined variable: HTTP_HOST in

Please change:
fputs ($SMTPIN, "EHLO ".HTTP_HOST."\r\n");

for
fputs ($SMTPIN, "EHLO ".$_SERVER["HTTP_HOST"]."\r\n");

Khanani maswanganyi said...

I cannot seem to get my SMTPworking on a server it only works when I am using a local host, how do I configure it to work on any server that I can upload my php files ?

Here is my working script

Anonymous said...

Very nice script.

When I receive the email the from field is mail@domain.com how can I change this to use an alias like php mail() headers. eg. From: Gordon .

Also the sent field in the email received says none instead of the date?

Please can anyone help?

Anonymous said...

Mr.admin im getting this error..

Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\wamp\www\SMTPmail\SMTPClass.php on line 35

Notice: Undefined variable: talk in C:\wamp\www\SMTPmail\SMTPClass.php on line 68

please help me...

magi said...

Script is working Fine..

How to change body of the content html template?

senthily88 said...

Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:25 (No connection could be made because the target machine actively refused it. ) in C:\wamp\www\check\mail\SMTPmail\SMTPClass.php on line 35


i got this error on smtp mail


plz any one send the correct smtp mail code on my mail

senthily88@gmail.com

plzzzzzzzzzzzzzzz help me

Gates BP said...

really really TY man!!!!!!!!!

Gates BP said...

really really TY man!!!!!!! seriously

rhea maynite said...

Thumbs up!

Anonymous said...

from where to buy SMTP server Can we get free

Anonymous said...

data are not specified using the SMTP server, or you need to edit something for which SMTP server to use

Anonymous said...

Thanks a lot

Amu Nkombyani said...

Its working perfectly. Thank you

Now i just need to try the code for attachments

Anonymous said...


( ! ) Notice: Undefined variable: HTTP_HOST in C:\wamp\www\smtp
i am new to the php.. i am using same code but below error is coming so what to do.....

mail\SMTPClass.php on line 38
Call Stack
# Time Memory Function Location
1 0.0010 144576 {main}( ) ..\Index.php:0
2 0.0130 157920 SMTPClient->SendMail( ) ..\Index.php:11

Anonymous said...

can i send email on yahoo or gmail using this script

Anonymous said...

Hi... plz someone help for secured SSL email using gmail SMTP.

Anonymous said...

fsockopen(): unable to connect to 127.0.0.1:25 (No connection could be made because the target machine actively refused it. ) in C:\xampp\htdocs\mail\SMTPClass.php on line 35 why this warning is coming

Dinesh Sharma said...

Mail showing html tags, what is the error???

Anonymous said...

I have a problem
When input user & password is error.
How get error info & send success or fail

Anand Rawat said...

Hello Experts I am fresher in php. I need your help. Please guide me.

I m using same script for sending form mail but getting below error.

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\inetpub\vhosts\leminarindustries.com\httpdocs\formsend\SMTPClass.php on line 30

Warning: fsockopen() [function.fsockopen]: unable to connect to smpt.gmail.com:465 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in E:\inetpub\vhosts\leminarindustries.com\httpdocs\formsend\SMTPClass.php on line 30

Anonymous said...

How can we use recaptcha with this script?

rohit moradiya said...

how can to at a time all user send mail script

sivap455 said...

Thanks a lot

BHVK PATEL said...

PHP Fatal error: Class 'SMTPClient' not found in /home/banlabco/public_html/SMTP/index.php on line 10

Saurav Sen said...

send two mail different email id on my smtp sendmail function

manikandan p said...

Warning: fsockopen(): unable to connect to 127.0.0.1:25 (No connection could be made because the target machine actively refused it. ) in C:\xampp\htdocs\SMTPmail (1)\SMTPClass.php on line 35

Notice: Undefined variable: talk in C:\xampp\htdocs\SMTPmail (1)\SMTPClass.php on line 68

Post a Comment