sending mail through php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shail_csp
Forum Newbie
Posts: 6
Joined: Mon Apr 06, 2009 6:23 am

sending mail through php

Post by shail_csp »

Hi, all
can any one suggest me, how to send mail through php.. I m using following code

$to = 'mr.shailendra.shail@gmail.com';
$subject = 'Activation link from IHTIM ';
$message = 'hello, Mr. shail<br> click here';
$headers = 'From: IHTIM@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

but its giving some error, How to configure smtp or pop3 to send it.. Many Many thanks in advance..
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: sending mail through php

Post by Apollo »

1. what happens if you leave the $headers parameter out? (just for now)
2. what error message do you get?
shail_csp
Forum Newbie
Posts: 6
Joined: Mon Apr 06, 2009 6:23 am

Re: sending mail through php

Post by shail_csp »

Apollo wrote:1. what happens if you leave the $headers parameter out? (just for now)
2. what error message do you get?
if i leave header parameter out
"sendmail_from" not set in php.ini or custom "From:" header missing in C:\wamp\www\IHTIM\Trash\sendMailTest.php on line 8

with header parameter
SMTP server response: 550 5.7.1 Unable to relay for mr.shailendra.shail@gmail.com in C:\wamp\www\IHTIM\Trash\sendMailTest.php on line 9


Any way thank for reply.. your any suggestion is welcome..
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: sending mail through php

Post by Apollo »

Apparently your SMTP server is configured not to allow sending emails from random addresses such as @gmail.com. See if you can change your server's SMTP settings, or use another SMTP server.

If neither of those are possible, you could also use an alternative mail function or class, such as PHPMailer (personally had good experience with that one, using gmail's SMTP server) or Pear or Swift (no personal experience with the latter two, but heard good things about 'em).
shail_csp
Forum Newbie
Posts: 6
Joined: Mon Apr 06, 2009 6:23 am

Re: sending mail through php

Post by shail_csp »

Apollo wrote:Apparently your SMTP server is configured not to allow sending emails from random addresses such as @gmail.com. See if you can change your server's SMTP settings, or use another SMTP server.

If neither of those are possible, you could also use an alternative mail function or class, such as PHPMailer (personally had good experience with that one, using gmail's SMTP server) or Pear or Swift (no personal experience with the latter two, but heard good things about 'em).
can you give me a small example(code) so that i can get more help.. Many many thanks..
shail_csp
Forum Newbie
Posts: 6
Joined: Mon Apr 06, 2009 6:23 am

Re: sending mail through php

Post by shail_csp »

Apollo wrote:Apparently your SMTP server is configured not to allow sending emails from random addresses such as @gmail.com. See if you can change your server's SMTP settings, or use another SMTP server.

If neither of those are possible, you could also use an alternative mail function or class, such as PHPMailer (personally had good experience with that one, using gmail's SMTP server) or Pear or Swift (no personal experience with the latter two, but heard good things about 'em).
how to configure gmail's SMTP. If you have any idea..!!!
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: sending mail through php

Post by Apollo »

shail_csp wrote:can you give me a small example(code) so that i can get more help.. Many many thanks..
Using the phpmailer class:

Code: Select all

require_once('class.phpmailer.php');
 
$msg = new PHPMailer();
 
$msg->IsSMTP();
$msg->Host = "ssl://smtp.gmail.com:465";
$msg->SMTPAuth = true;
$msg->Username = 'gmail_username';
$msg->Password = 'gmail_password';
 
$msg->From = 'john@example.com';
$msg->FromName = 'John Doe';
$msg->AddAddress('cindy@somewhere.com');
$msg->Subject = 'Testing';
$msg->Body = 'Hello there';
$msg->Sender = $msg->From; // for Return-path
 
if (!$msg->Send()) die('epic fail...');
shail_csp wrote:how to configure gmail's SMTP. If you have any idea..!!!
In your server's PHP configuration is an 'SMTP' value. But you can ignore that if you use the example from above.
shail_csp
Forum Newbie
Posts: 6
Joined: Mon Apr 06, 2009 6:23 am

Re: sending mail through php

Post by shail_csp »

Apollo wrote:
shail_csp wrote:can you give me a small example(code) so that i can get more help.. Many many thanks..
Using the phpmailer class:

Code: Select all

require_once('class.phpmailer.php');
 
$msg = new PHPMailer();
 
$msg->IsSMTP();
$msg->Host = "ssl://smtp.gmail.com:465";
$msg->SMTPAuth = true;
$msg->Username = 'gmail_username';
$msg->Password = 'gmail_password';
 
$msg->From = 'john@example.com';
$msg->FromName = 'John Doe';
$msg->AddAddress('cindy@somewhere.com');
$msg->Subject = 'Testing';
$msg->Body = 'Hello there';
$msg->Sender = $msg->From; // for Return-path
 
if (!$msg->Send()) die('epic fail...');
shail_csp wrote:how to configure gmail's SMTP. If you have any idea..!!!
In your server's PHP configuration is an 'SMTP' value. But you can ignore that if you use the example from above.
Wow it Cool, Thanks.. U r really master of php..
Is there any way to send sms to any mobile.. through php. Basically I have to send the result message if some one is sending his roll number to my account/number.. How to detect that msg or number and responce to same.. If you have any idea.. !!!!!!
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: sending mail through php

Post by Apollo »

shail_csp wrote:Is there any way to send sms to any mobile.. through php.
No idea really, never done anything with SMS. But you better open a new topic for that, as it's pretty unrelated, and put "SMS" in the title to draw people's attention who may be into this sort of thing.
Post Reply