Page 1 of 1

Few questions about email sending in php

Posted: Fri Nov 09, 2007 12:55 pm
by tanvirtonu
I have tried several ways to send mail but I hav failed and I m veryyyyyy upset. Now
I have few question about mail sending in php

1. I cant/dont use the smtp server of my ISP. Now which server I can send my mail thru?

2. I want to use yahoo smtp mail server. Now do I have to set yahoo smtp server name and port in php.ini? Cant I set these (both name and port)dynamically in php. if so, how?

3.If the server name I m using uses authenticated smtp, WHERE and how to put code for this in php .
If in mail header then in which format?

4. I hav come to know the following things from website of yahoo-

Outgoing Mail (SMTP) Server:
smtp.mail.yahoo.com (Use SSL, port: 465, use authentication)

I am also giving u my account details
My yahoo id- bhonest_4ever2007@yahoo.com (created 2day for the test)
password- abcdefg


Now would anybody pls pls tel me or write me a simple code by which I can send mail without using my Isp's smtp server. DO I need to use those info. in php.ini . Or is there any way to add them dynamically in php.

I once tried with the following code but nothing happened

Code: Select all

<?php
//mail($recepeint,$subject,$msg,$mailheader);
//define the receiver of the email
$to = 'bhonest_4ever@yahoo.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\r\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: tanvirtonu@yahoo.com\r\nReply-To: tanvirtonu@yahoo.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";

echo "Thank You <b>Tanvir</b> for ur message";
echo "<p>Your mail address is <p>$to </p>";
echo "<p>Your message was <p> $message </p>";

?>
//I set server=smtp.mail.yahoo.com and port=25 in php.ini and restart the server .

NB. If I configure php.ini in php code using ini_set(), wouldn't it be permanent next time. But what happens if I host these php file in another server. There can be a lot other php files trying to do the same thing. Isn't it a problem.
I can also send mail using a free smtp server and a mail client in my windows pc which requires default 25 port for smtp. Should I use this port-25 with the above server name smtp.mail.yahoo.com

Posted: Fri Nov 09, 2007 2:16 pm
by phpBuddy
I should first try with
that yahoo smtp server and port 25, without authentication.

If this does not work, you would have to use authentication. Username/password login.
I do not know if you can use port 25 with authentication or would have to use SSL port 465.

Have a look at this download:
Swift Mailer
http://www.swiftmailer.org/

Documentation.
http://www.swiftmailer.org/wikidocs/

The author has got own forum here at devnetwork.
Where you can get advice fromhim.
See the forum category at very bottom of forum list
viewforum.php?f=52

Posted: Fri Nov 09, 2007 2:46 pm
by phpBuddy
Using your details
I think something like this, using Swift Mailer could work:

Code: Select all

<?php
/*
Outgoing Mail (SMTP) Server:
smtp.mail.yahoo.com (Use SSL, port: 465, use authentication)
Account details (created 2day for the test)
Yahoo id: bhonest_4ever2007@yahoo.com
Password: abcdefg
*/
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

// Subject and message body
$subject = 'My Test subject';
$msgbody = 'Here is my text message';
// Email addresses
$sendto = 'bhonest_4ever2007@yahoo.com'; 
$from   = 'tanvirtonu@yahoo.com';

// User/pass
$user = 'bhonest_4ever2007@yahoo.com';
$pass = 'abcdefg';
// Setup connection with authentication at port 465
$smtp =& new Swift_Connection_SMTP("smtp.mail.yahoo.com", 465);
$smtp->setUsername($user);
$smtp->setPassword($pass);

// Make the connection 
$swift =& new Swift($smtp);

// Create the message
$message =& new Swift_Message($subject, $msgbody);
// Send and check if Swift actually sends it
if ($swift->send($message, $sendto, $from)){ 
    echo "Sent";
}
else{
    echo "Failed";
}

?>