Send Mail using SMTP and PHP.
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



Updated: Send mail using Gmail SMTP using PHP

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
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


150 comments:

  1. Thanks. Very useful.

    ReplyDelete
  2. a piece of great code, thanks.

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

    ReplyDelete
  3. does this work for gmail smtp servers?

    ReplyDelete
  4. 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)

    ReplyDelete
  5. does it work for aol smtp server?

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

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

    ReplyDelete
  8. 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...

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

    ReplyDelete
  10. Thanks youuuuuuuuuuu.....

    ReplyDelete
  11. this not support html mail

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

    ReplyDelete
  13. Thankq very Much.
    God bless you

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

    ReplyDelete
  15. 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

    ReplyDelete
  16. 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";

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

    ReplyDelete
  18. 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

    ReplyDelete
  19. 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....

    ReplyDelete
  20. 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 !!!

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

    ReplyDelete
  22. 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;
    }
    }
    ?>

    ReplyDelete
  23. 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?

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

    ReplyDelete
  25. I am getting this error please help me to solve


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

    ReplyDelete
  26. 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?

    ReplyDelete
  27. 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

    ReplyDelete
  28. 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 ??

    ReplyDelete
  29. 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

    ReplyDelete
  30. Thank you

    it is excellent .

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

    ReplyDelete
  31. Nice job,

    this code worked beautifully for me to connect to godaddys smtp

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

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

    ReplyDelete
  34. 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 (^_^)

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


    any help?

    ReplyDelete
  36. 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. "

    ReplyDelete
  37. Thanks a lot. You are the man!

    ReplyDelete
  38. 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.

    ReplyDelete
  39. Very very thankful for your valuable post

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

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

    please help
    thank in ad..

    ReplyDelete
  42. Works like a charm thanks!

    ReplyDelete
  43. index.php downloads instead of opening....

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

    ReplyDelete
  45. 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.

    ReplyDelete
  46. 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 ?

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

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

    ReplyDelete
  49. 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

    ReplyDelete
  50. 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

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

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

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

    ReplyDelete
  54. 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

    ReplyDelete
  55. Notice: Undefined variable: HTTP_HOST

    ReplyDelete
  56. 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

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

    ReplyDelete
  58. 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

    ReplyDelete
  59. 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

    ReplyDelete
  60. 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!

    ReplyDelete
  61. Hi ,

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

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

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

    ReplyDelete
  64. 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 :

    ReplyDelete
  65. 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?

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

    ReplyDelete
  67. 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

    ReplyDelete
  68. 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!

    ReplyDelete
  69. Hi Diego,

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

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

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

    Thx !

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

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

    Please help.

    ReplyDelete
  73. but its not working for gmail smtp plz
    help me

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

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

    ReplyDelete
  76. 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??

    ReplyDelete
  77. 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

    ReplyDelete
  78. 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.

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

    ReplyDelete
  80. 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

    ReplyDelete
  81. 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?

    ReplyDelete
  82. Thanks ! Nice way to send mail

    -Anup

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

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

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

    Please help me!

    Thank you!!

    ReplyDelete
  86. Please Help me! It is very urgent! TT_TT

    ReplyDelete
  87. can you post full code to send as HTML mail

    ReplyDelete
  88. 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!!!

    ReplyDelete
  89. 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.

    ReplyDelete
  90. 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!!

    ReplyDelete

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

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

    ReplyDelete
  93. i tried this. nothing happens..

    ReplyDelete
  94. 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");

    ReplyDelete
  95. 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

    ReplyDelete
  96. 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?

    ReplyDelete
  97. 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...

    ReplyDelete
  98. Script is working Fine..

    How to change body of the content html template?

    ReplyDelete
  99. 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

    ReplyDelete
  100. really really TY man!!!!!!!!!

    ReplyDelete
  101. really really TY man!!!!!!! seriously

    ReplyDelete
  102. from where to buy SMTP server Can we get free

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

    ReplyDelete
  104. Its working perfectly. Thank you

    Now i just need to try the code for attachments

    ReplyDelete

  105. ( ! ) 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

    ReplyDelete
  106. can i send email on yahoo or gmail using this script

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

    ReplyDelete
  108. 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

    ReplyDelete
  109. Mail showing html tags, what is the error???

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

    ReplyDelete
  111. 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

    ReplyDelete
  112. How can we use recaptcha with this script?

    ReplyDelete
  113. how can to at a time all user send mail script

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

    ReplyDelete
  115. send two mail different email id on my smtp sendmail function

    ReplyDelete
  116. 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

    ReplyDelete
  117. code not working sir...in my server. in above code i given echo "message sucess.."; here its show's only echo message..but not send my mail what i do?
    please help me..

    ReplyDelete
  118. How come this works on xampp on Windows, but does not work on httpd on CentOS?

    ReplyDelete
  119. Is there any way to include multiple email id in cc

    ReplyDelete
  120. How do i use Multiple to addresses
    like $to = 'abc@example.com,xyz@example.com';

    ReplyDelete
  121. Please any one help me my mails are not sent i don't have any error...........

    ReplyDelete
  122. I implemented your script just entered my gmail account details and google smtp details port 465
    i didn't get any error b ut mail is still not sent
    [From] => 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH

    [To] => 250-ENHANCEDSTATUSCODES

    [data] => 250-PIPELINING

    [send] => 250-CHUNKING

    ReplyDelete
  123. 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:\Program Files\xampp\htdocs\mail\SMTPClass.php on line 36

    ReplyDelete
  124. thank u from development. what is this error:
    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:\wamp\www\mailgood\SMTPclass.php on line 28

    ReplyDelete
  125. Hi , I used this code.no error.but mail not sending..Pls help me.

    ReplyDelete
  126. same Error:
    I used this code.no error.but mail not sending..Pls help me.
    how can i get confirmation message either mail sent or not

    ReplyDelete
  127. Nice post...you saved mah day..thanku a lotsss

    ReplyDelete
  128. array(8) { ["hello"]=> string(81) "220-md-in-31.webhostbox.net ESMTP Exim 4.86 #2 Sat, 23 Jan 2016 09:45:06 +0000 " ["res"]=> string(75) "220-We do not authorize the use of this system to transport unsolicited, " ["user"]=> string(25) "220 and/or bulk e-mail. " ["pass"]=> string(77) "250-md-in-31.webhostbox.net Hello md-in-31.webhostbox.net [111.118.215.254] " ["From"]=> string(19) "250-SIZE 52428800 " ["To"]=> string(14) "250-8BITMIME " ["data"]=> string(16) "250-PIPELINING " ["send"]=> string(22) "250-AUTH PLAIN LOGIN " }

    ReplyDelete
  129. : Undefined variable: HTTP_HOST in C:\xampp\htdocs\SMTPmail\SMTPClass.php on line 38

    fputs ($SMTPIN, "HELLO " .$HTTP_HOST."\r\n");

    in this line

    ReplyDelete
  130. hello srinivash I am facing problem in godaddy mailer its work perfect but when I try customize it throught error I want filed as name,email mobile and message when


    please watch my code

    given below

    ReplyDelete
  131. when i include the file the mails are not going any solution??

    ReplyDelete
  132. Since it doesn't work with gmail, what email service should I use to receive the emails?
    thank you in advance !

    ReplyDelete
  133. Thank you. I was frantically searching for solving smtp issue in mailing with php. Your program just worked, making life easy in setting up a website.

    ReplyDelete
  134. not showing me from email id

    ReplyDelete
  135. how can we add bcc in this script....please help

    ReplyDelete
  136. i can't able to send html content can you any help me

    ReplyDelete
  137. so what will be the html code to connect those files?

    ReplyDelete

Make in India