943,071 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 26740
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 20th, 2008
0

smtp mail in php

Expand Post »
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...

PHP Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
galeej is offline Offline
11 posts
since Jan 2008
Jan 21st, 2008
0

Re: smtp mail in php

someone please help me out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
galeej is offline Offline
11 posts
since Jan 2008
Jan 21st, 2008
0

Re: smtp mail in php

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
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
hooray is offline Offline
62 posts
since Jan 2008
Jan 22nd, 2008
0

Re: smtp mail in php

i have tried all formats but i get delivery mail stating that wrong TO format...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
galeej is offline Offline
11 posts
since Jan 2008
Jan 22nd, 2008
0

Re: smtp mail in php

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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
galeej is offline Offline
11 posts
since Jan 2008
Jan 22nd, 2008
0

Re: smtp mail in php

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");
?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vipin_php is offline Offline
8 posts
since Jan 2008
Jan 22nd, 2008
0

Re: smtp mail in php

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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
galeej is offline Offline
11 posts
since Jan 2008
Jan 22nd, 2008
0

Re: smtp mail in php

vipin any other questions regarding this plz mail me anand@fourk.info
Reputation Points: 10
Solved Threads: 0
Newbie Poster
galeej is offline Offline
11 posts
since Jan 2008
Jan 22nd, 2008
0

Re: smtp mail in php

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:

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

To something like:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  1. RCPT TO: <email1@example.com>
  2. RCTP TO: <email2@example.com>

So now you can use the format:

PHP Syntax (Toggle Plain Text)
  1. $to = 'email1@example.com, email2@example.com';

You can extend it further to include the name associated with emails if you need.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jan 22nd, 2008
0

Re: smtp mail in php

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: XML file refresh issue
Next Thread in PHP Forum Timeline: pervent from crontab concurrent run





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC