Page 1 of 1

Send Emails Using Php (basic) Rate Topic: -----

#1 apg88  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-December 04

Posted 21 January 2005 - 02:50 PM

Send Emails Using PHP (Basic). by apg88

Sending emails with PHP is a lot easier than you would think.

First of all, we start with our PHP tags <?php and ?>

Then we use a simple function called mail() (Duh!).
The syntax for the mail function is
mail(string to, string subject, string message, string additional_headers);


For example, if I wanted to send an email to somebody@email.com, I would write:
mail("somebody@email.com", "Test E-Mail (This is the subject of the E-Mail)", "This is the body of the Email");


The first parameter tells the function who to send the email to.
The second parameter is the subject of the email.
The third parameter is the body of the email.
The fourth parameter is for more advanced uses, so we will ignore it for now.

So you save your PHP file and access it, but aah! You get a blank page. Was the email sent? Who knows...? That’s why we have the if-then function!

If you want to know if your email was sent or if an error occurred, you would type this in.
if(mail("somebody@email.com", "Test E-Mail (This is the subject of the E-Mail)", "This is the body of the Email")){
	echo "The email was successfully sent.";
} else {
	echo "The email was NOT sent.";
}


This function, like every other function also works with variables, here is an example of a fully functional email script.

<?php 
$email_to = "somebody@email.com";
$email_subject = "Test E-Mail (This is the subject of the E-Mail)";
$email_body = "This is the body of the Email \nThis is a second line in the body!";

if(mail($email_to, $email_subject, $email_body)){
	echo "The email($email_subject) was successfully sent.";
} else {
	echo "The email($email_subject) was NOT sent.";
}
?>


That’s all there is to it!

www.apg88.com

Is This A Good Question/Topic? 0
  • +

Replies To: Send Emails Using Php (basic)

#2 h0meles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 10-September 07

Posted 10 September 2007 - 01:20 PM

What about specifying a from or return addy? Would that go as the fourth string?
Was This Post Helpful? 0
  • +
  • -

#3 NeoGreen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 243
  • Joined: 28-April 06

Posted 24 September 2007 - 07:56 PM

Awesome tutorial man. :)
Was This Post Helpful? 0
  • +
  • -

#4 nitestryker  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 15-May 08

Posted 16 May 2008 - 02:35 PM

Here my code
you can use variables from a form such
<input type="text" name="to" size=25">
as $to = $_POST['to']; etc etc....


<?php
$to = "blah@blah.com";
$subject = "mmmmm coffee";
$message = "mmmmm I love Coffee";
$from = "Iluvcoffee@lol.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent ";
?>


This post has been edited by nitestryker: 16 May 2008 - 02:42 PM

Was This Post Helpful? 1
  • +
  • -

#5 nitestryker  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 15-May 08

Posted 16 May 2008 - 02:58 PM

Here is an example from one i wrote

[ index.html ] <--- HTML FILE
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
<center>
  <table border="1" bgcolor="red">
  <tr>
  <td>
 <form name="form" method="post" action="mail.php">
<center>To: <input type="text" name="to"><br></center>
 <center>From: <input type="text name="from"><br></center>
 <center>Subject: <input type="text" name="subject"><br></center>
 <center>Message:<br><textarea name="message" rows=4 cols=25></textarea></center><br>
<center><input type="submit" value="send"></center>
</td>
</tr>  
</body>
</html>



[mail.php] <---- Php Code
<?php
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST ['from'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
// more php n echo or include whatever you want
?>


This post has been edited by nitestryker: 16 May 2008 - 03:01 PM

Was This Post Helpful? 1
  • +
  • -

#6 sai shoba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-May 13

Posted 04 May 2013 - 01:08 AM

I know how to send mail using mail function but the main problem is attaching a file and sending a mail so that reciever mailer can see that resume....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1