smtp mail in php

Reply

Join Date: Jan 2008
Posts: 11
Reputation: galeej is an unknown quantity at this point 
Solved Threads: 0
galeej galeej is offline Offline
Newbie Poster

smtp mail in php

 
0
  #1
Jan 20th, 2008
Hi,
I have an application which sends mail wen a new post is made in my blog.my blog is setup in local server using xampp.wen i send mail to a single address its working fine and wen i tried to send mail to multiple address i get an error saying that invalid address format in TO field.Below is the code i have used..plz help me out...

  1. function authgMail($from, $namefrom, $to, $nameto, $subject, $message)
  2. {
  3.  
  4. $smtpServer = "tls://smtp.gmail.com"; //does not accept STARTTLS
  5. $port = "465"; // try 587 if this fails
  6. $timeout = "45"; //typical timeout. try 45 for slow servers
  7. $username = "user@gmail.com"; //your gmail account
  8. $password = "pass"; //the pass for your gmail
  9. $localhost = $_SERVER['REMOTE_ADDR']; //requires a real ip
  10. $newLine = "\r\n"; //var just for newlines
  11.  
  12. /* you shouldn't need to mod anything else */
  13.  
  14. //connect to the host and port
  15. $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
  16. //echo $errstr." - ".$errno;
  17. $smtpResponse = fgets($smtpConnect, 4096);
  18. if(empty($smtpConnect))
  19. {
  20. $output = "Failed to connect: $smtpResponse";
  21. //echo $output;
  22. return $output;
  23. }
  24. else
  25. {
  26. $logArray['connection'] = "Connected to: $smtpResponse";
  27. //echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
  28. //echo $tadr;
  29. }
  30.  
  31. //you have to say HELO again after TLS is started
  32. fputs($smtpConnect, "HELO $localhost". $newLine);
  33. $smtpResponse = fgets($smtpConnect, 4096);
  34. $logArray['heloresponse2'] = "$smtpResponse";
  35.  
  36. //request for auth login
  37. fputs($smtpConnect,"AUTH LOGIN" . $newLine);
  38. $smtpResponse = fgets($smtpConnect, 4096);
  39. $logArray['authrequest'] = "$smtpResponse";
  40.  
  41. //send the username
  42. fputs($smtpConnect, base64_encode($username) . $newLine);
  43. $smtpResponse = fgets($smtpConnect, 4096);
  44. $logArray['authusername'] = "$smtpResponse";
  45.  
  46. //send the password
  47. fputs($smtpConnect, base64_encode($password) . $newLine);
  48. $smtpResponse = fgets($smtpConnect, 4096);
  49. $logArray['authpassword'] = "$smtpResponse";
  50.  
  51. //email from
  52. fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
  53. $smtpResponse = fgets($smtpConnect, 4096);
  54. $logArray['mailfromresponse'] = "$smtpResponse";
  55.  
  56. //email to
  57. fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
  58. $smtpResponse = fgets($smtpConnect, 4096);
  59. $logArray['mailtoresponse'] = "$smtpResponse";
  60.  
  61. //the email
  62. fputs($smtpConnect, "DATA" . $newLine);
  63. $smtpResponse = fgets($smtpConnect, 4096);
  64. $logArray['data1response'] = "$smtpResponse";
  65.  
  66. //construct headers
  67. $headers = "MIME-Version: 1.0" . $newLine;
  68. $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
  69. $headers .= "To: $nameto $to1" . $newLine;
  70. $headers .= "From: $namefrom <$from>" . $newLine;
  71.  
  72. //observe the . after the newline, it signals the end of message
  73. fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
  74. $smtpResponse = fgets($smtpConnect, 4096);
  75. $logArray['data2response'] = "$smtpResponse";
  76.  
  77. // say goodbye
  78. fputs($smtpConnect,"QUIT" . $newLine);
  79. $smtpResponse = fgets($smtpConnect, 4096);
  80. $logArray['quitresponse'] = "$smtpResponse";
  81. $logArray['quitcode'] = substr($smtpResponse,0,3);
  82. fclose($smtpConnect);
  83. //a return value of 221 in $retVal["quitcode"] is a success
  84.  
  85.  
  86.  
  87. $a='Mail Sent Sucessfully';
  88. return($a);
  89. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: galeej is an unknown quantity at this point 
Solved Threads: 0
galeej galeej is offline Offline
Newbie Poster

Re: smtp mail in php

 
0
  #2
Jan 21st, 2008
someone please help me out
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 62
Reputation: hooray is an unknown quantity at this point 
Solved Threads: 6
hooray hooray is offline Offline
Junior Poster in Training

Re: smtp mail in php

 
0
  #3
Jan 21st, 2008
you say you have problems sending it to multiple addresses, but its fine for one. Maybe theres a format error, like you didnt put a , after each email or wrap the emails in "":

To: "user@address.com", "other@address.com"

Only a simple idea, but its all i could come up with. Apart from maybe you need to increase the $timeout length for more emails
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: galeej is an unknown quantity at this point 
Solved Threads: 0
galeej galeej is offline Offline
Newbie Poster

Re: smtp mail in php

 
0
  #4
Jan 22nd, 2008
i have tried all formats but i get delivery mail stating that wrong TO format...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: galeej is an unknown quantity at this point 
Solved Threads: 0
galeej galeej is offline Offline
Newbie Poster

Re: smtp mail in php

 
0
  #5
Jan 22nd, 2008
i need a method to send mail through smtp in php by which i have to send mail to atleast 200 recepients at a time and it should be fast...thats my requirement now..plz help me out..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: vipin_php is an unknown quantity at this point 
Solved Threads: 0
vipin_php vipin_php is offline Offline
Newbie Poster

Re: smtp mail in php

 
0
  #6
Jan 22nd, 2008
hello, i just named the script as mail.php and tried it but i am receiving the following error


Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in c:\program files\easyphp1-7\www\main.php on line 15

Warning: fsockopen(): unable to connect to tls:\smtp.gmail.com:587 in c:\program files\easyphp1-7\www\main.php on line 15

Warning: fgets(): supplied argument is not a valid stream resource in c:\program files\easyphp1-7\www\main.php on line 17




The whole script of mine looks like this but i made changes regarding user_name and password and even enabled the POP service in my gmail.Please help regarding this because the same script was working for U, thanks in advance.

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

$smtpServer = "tls://smtp.gmail.com"; //does not accept STARTTLS
$port = "465"; // try 587 if this fails
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = "user@gmail.com"; //your gmail account
$password = "pass"; //the pass for your gmail
$localhost = $_SERVER['REMOTE_ADDR']; //requires a real ip
$newLine = "\r\n"; //var just for newlines

/* you shouldn't need to mod anything else */

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

//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

$a='Mail Sent Sucessfully';
return($a);
}
authgMail("me@localhost.com", "vvv", "xxx@gmail.com","mmm","mail","message");
?>
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: galeej is an unknown quantity at this point 
Solved Threads: 0
galeej galeej is offline Offline
Newbie Poster

Re: smtp mail in php

 
0
  #7
Jan 22nd, 2008
hey,i just used this code with corresponding arguments...its working fine for me...just check whether ur firewall block this port????bcoz i had same problems and i disabled my antivirus and seems to be working fine..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: galeej is an unknown quantity at this point 
Solved Threads: 0
galeej galeej is offline Offline
Newbie Poster

Re: smtp mail in php

 
0
  #8
Jan 22nd, 2008
vipin any other questions regarding this plz mail me anand@fourk.info
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,050
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 65
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: smtp mail in php

 
0
  #9
Jan 22nd, 2008
The problem is that your function does not support sending to multiple email addresses.

With SMTP, multiple addresses are sent one after the other, and the client has to wait for a response before writing the next recipient. So in your function change:

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

To something like:

  1. //email to
  2.  
  3. $addresses = explode(',', $to);
  4. foreach($addresses as $to) {
  5. fputs($smtpConnect, "RCPT TO: <".trim($to).">" . $newLine);
  6. $smtpResponse = fgets($smtpConnect, 4096);
  7. $logArray['mailtoresponse'] = "$smtpResponse";
  8. }

What that code does is first explode your $to address into an array, $addresses. Then each address is sent to the SMTP server like:

  1. RCPT TO: <email1@example.com>
  2. RCTP TO: <email2@example.com>

So now you can use the format:

  1. $to = 'email1@example.com, email2@example.com';

You can extend it further to include the name associated with emails if you need.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,050
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 65
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: smtp mail in php

 
0
  #10
Jan 22nd, 2008
BTW: I'd recommend the SWIFT mailer library if you plan to incorporate sending multipart mails (HTML, Attachments etc.).

The function you have only sends a single part mail, text/html. Some mail clients cannot read html mail.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC