Hi Friends,
How can I send email by using the php code.
please help me for this.
Thanks in advance.
Hi Friends,
How can I send email by using the php code.
please help me for this.
Thanks in advance.
PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise.
Code:<?php //define the receiver of the email $to = 'youraddress@example.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!\n\nThis is my first mail."; //define the headers we want passed. Note that they are separated with \r\n $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.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"; ?>
Sending emails via PHP is rather simple with the mail() function.
mail() function has the following function prototype:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Here is an example usage:
The above code will send out an email to recipient@domain.com with subject “Example Subject” and the message stored in $MESSAGE variable.Code:$EMAIL_HEADER = “From: Sender Name <sender@example.com>\r\n”; $EMAIL_HEADER .= “MIME-Version: 1.0\r\n”; $EMAIL_HEADER .= “X-Priority: 1\r\n”; $EMAIL_HEADER .= “X-MSmail-Priority: High\r\n”; $MESSAGE=” Hello there, This is a test message. From Admin.”; mail(”recipient@domain.com”, “Example Subject”, $MESSAGE, $EMAIL_HEADER);
The $EMAIL_HEADER determines that this email will be sent from Sender Name <sender@example.com> and is high priority.
Can you send a mail without the need of a smtp server?
If you do not have access to an SMTP server or you are having trouble connecting to it, you can use the PHPmailer module as detailed and explained in this Drupal support forum article: http://drupal.org/node/54952
or using the SMTP class: http://drupal.org/node/29888
In theory, if you went to the trouble of finding the recipiants mx record/smtp server, you could use their server to send mail to them, just as a normal smtp server does. Even though you'd have to find the recip's info (their smtp server) on your own, almost any smtp class should be able to handle the actual mail sending.
In short, the way SMTP works, with relay checks, is that the smtp servers have an open connection. This connection is for both sending mail to other smtp servers and receiving mail from its users and other smtp servers. As long as your email passes the relay checks (using a valid recip email), the remote smtp server should not be able to tell the difference between your script and any other basic smtp server trying to send mail to its user. Some servers may add other checks as well, like aol requires a reverse ip lookup to resolve to a name, but for the most part it should work fine.
You would just have to figure out the smtp server for each recip on your own before processing the email.
Bookmarks