| | |
smtp mail in php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
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...
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)
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 $to1" . $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); }
•
•
Join Date: Jan 2008
Posts: 62
Reputation:
Solved Threads: 6
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
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
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
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");
?>
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");
?>
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
vipin any other questions regarding this plz mail me anand@fourk.info
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:
To something like:
What that code does is first explode your $to address into an array, $addresses. Then each address is sent to the SMTP server like:
So now you can use the format:
You can extend it further to include the name associated with emails if you need.
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)
//email to fputs($smtpConnect, "RCPT TO: <$to>" . $newLine); $smtpResponse = fgets($smtpConnect, 4096); $logArray['mailtoresponse'] = "$smtpResponse";
To something like:
PHP Syntax (Toggle Plain Text)
//email to $addresses = explode(',', $to); foreach($addresses as $to) { fputs($smtpConnect, "RCPT TO: <".trim($to).">" . $newLine); $smtpResponse = fgets($smtpConnect, 4096); $logArray['mailtoresponse'] = "$smtpResponse"; }
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)
RCPT TO: <email1@example.com> RCTP TO: <email2@example.com>
So now you can use the format:
PHP Syntax (Toggle Plain Text)
$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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- SMTP mail settings with PHP (PHP)
- PHP mail() (Sendmail) Problem :( (PHP)
- PHP e-mail SMTP error ??? (PHP)
- PHP E-mail setting ??? (PHP)
- Need help with Mail php script!! (PHP)
- Sending E-mail Within PHP (PHP)
- Installing a SMTP for mail() (PHP)
Other Threads in the PHP Forum
- Previous Thread: PHP page_rewrie
- Next Thread: PHP Add GD Function
| Thread Tools | Search this Thread |
# .htaccess 5.2.10 access alexa apache api array beginner broken cakephp checkbox class clean clients cms code convert cron curl database date directory display dissertation dropdown dynamic echo$_get[x]changingitintovariable... email encode error fairness file folder form forms function functions google hack href htaccess html htmlspecialchars image include indentedsubcategory ip javascript joomla legislation limit link local login mail memberships menu methods multiple multipletables mysql mysqlquery network newsletters oop open passwords paypal pdf persist php provider query radio random redirect remote script search secure server sessions simple sockets source space spam sql system table tutorial upload url user variable video voteup web youtube






