{"id":20,"date":"2011-04-01T23:27:54","date_gmt":"2011-04-01T17:27:54","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=20"},"modified":"2011-04-01T23:27:54","modified_gmt":"2011-04-01T17:27:54","slug":"sending-mail-from-php","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=20","title":{"rendered":"Sending mail from php"},"content":{"rendered":"<p>Today I am going to post a simple method to send mail from php code.To this we must use SMTP [Simple Mail Transfer protocol]which uses default port that is 25.Before sending mail,SMTP server name must be included in php.ini .To make our job easier I will use three php files.<br \/>\nThey are<br \/>\n01: SMTPconfig.php<br \/>\n02: SMTPclass.php<br \/>\n03: index.php<\/p>\n<p><strong>01: SMTPconfig.php<\/strong><\/p>\n<p>\/\/Server Address<br \/>\n$SmtpServer=&#8221;gmail.com&#8221;;<br \/>\n$SmtpPort=&#8221;25&#8243;; \/\/default<br \/>\n$SmtpUser=&#8221;yourgmailid@gmail.com&#8221;;<br \/>\n$SmtpPass=&#8221;yourgmailpassword&#8221;;<\/p>\n<p>Here,SMTP server setup is completed.<\/p>\n<p><strong>02: SMTPclass.php<\/strong><\/p>\n<p>class SMTPClient<br \/>\n{<\/p>\n<p>function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)<br \/>\n{<\/p>\n<p>$this-&gt;SmtpServer = $SmtpServer;<br \/>\n$this-&gt;SmtpUser = base64_encode ($SmtpUser);<br \/>\n$this-&gt;SmtpPass = base64_encode ($SmtpPass);<br \/>\n$this-&gt;from = $from;<br \/>\n$this-&gt;to = $to;<br \/>\n$this-&gt;subject = $subject;<br \/>\n$this-&gt;body = $body;<\/p>\n<p>if ($SmtpPort == &#8220;&#8221;)<br \/>\n{<br \/>\n$this-&gt;PortSMTP = 25;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n$this-&gt;PortSMTP = $SmtpPort;<br \/>\n}<br \/>\n}<\/p>\n<p>function SendMail ()<br \/>\n{<br \/>\nif ($SMTPIN = fsockopen ($this-&gt;SmtpServer, $this-&gt;PortSMTP))<br \/>\n{<br \/>\nfputs ($SMTPIN, &#8220;EHLO &#8220;.$HTTP_HOST.&#8221;rn&#8221;);<br \/>\n$talk[&#8220;hello&#8221;] = fgets ( $SMTPIN, 1024 );<br \/>\nfputs($SMTPIN, &#8220;auth loginrn&#8221;);<br \/>\n$talk[&#8220;res&#8221;]=fgets($SMTPIN,1024);<br \/>\nfputs($SMTPIN, $this-&gt;SmtpUser.&#8221;rn&#8221;);<br \/>\n$talk[&#8220;user&#8221;]=fgets($SMTPIN,1024);<br \/>\nfputs($SMTPIN, $this-&gt;SmtpPass.&#8221;rn&#8221;);<br \/>\n$talk[&#8220;pass&#8221;]=fgets($SMTPIN,256);<br \/>\nfputs ($SMTPIN, &#8220;MAIL FROM: from.&#8221;&gt;rn&#8221;);<br \/>\n$talk[&#8220;From&#8221;] = fgets ( $SMTPIN, 1024 );<br \/>\nfputs ($SMTPIN, &#8220;RCPT TO: to.&#8221;&gt;rn&#8221;);<br \/>\n$talk[&#8220;To&#8221;] = fgets ($SMTPIN, 1024);<br \/>\nfputs($SMTPIN, &#8220;DATArn&#8221;);<br \/>\n$talk[&#8220;data&#8221;]=fgets( $SMTPIN,1024 );<br \/>\nfputs($SMTPIN, &#8220;To: to.&#8221;&gt;rnFrom: from.&#8221;&gt;rnSubject:&#8221;.$this-&gt;subject.&#8221;rnrnrn&#8221;.$this-&gt;body.&#8221;rn.rn&#8221;);<br \/>\n$talk[&#8220;send&#8221;]=fgets($SMTPIN,256);<br \/>\n\/\/CLOSE CONNECTION AND EXIT &#8230;<br \/>\nfputs ($SMTPIN, &#8220;QUITrn&#8221;);<br \/>\nfclose($SMTPIN);<br \/>\n\/\/<br \/>\n}<br \/>\nreturn $talk;<br \/>\n}<br \/>\n}<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/p>\n<p><strong>03: index.php<\/strong><\/p>\n<p>include(&#8216;SMTPconfig.php&#8217;);<br \/>\ninclude(&#8216;SMTPClass.php&#8217;);<br \/>\nif($_SERVER[&#8220;REQUEST_METHOD&#8221;] == &#8220;POST&#8221;)<br \/>\n{<br \/>\n$to = &#8220;to_mailid&#8221;;<br \/>\n$from = &#8220;from_mailid&#8221;;<br \/>\n$subject = &#8220;subject&#8221;;<br \/>\n$body = &#8220;message&#8221;;<br \/>\n$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);<br \/>\n$SMTPChat = $SMTPMail-&gt;SendMail();<br \/>\n}<\/p>\n<p><strong>Thanks for visiting this&#8230;<\/strong><br \/>\nFor more info <a href=\"http:\/\/www.9lessons.info\/2009\/10\/send-mail-using-smtp-and-php.html\">Click Here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I am going to post a simple method to send mail from php code.To this we must use SMTP [Simple Mail Transfer protocol]which uses default port that is 25.Before sending mail,SMTP server name must be included in php.ini .To make our job easier I will use three php files. They are 01: SMTPconfig.php 02: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=20\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sending mail from php&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":0,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}