FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


emailing Via PHP





turbosquid
I.m pretty much a noobie when it comes to PHP and am still mainly using PERL and HTML, i have started to have a play around with PHP to try and get to grips with it. My question is it possible to to send emails from a PHP page directly without the need for any server side programming? if so how would i go about it, i would be very grateful if anyone could post me a code snippet for a basic method of sending an email.

Thanks

TurboSquid
ncwdavid
Yes it is possible and it is very very easy to do it. Just use the mail() function. Here is a tutorial which will show you how to do it:

http://www.w3schools.com/php/php_mail.asp

Goodluck.
Diablosblizz
Code:

<?php
$user = $_POST['user'];
$useremail = $_POST['email'];
$subject = $_POST['subject'];
$message =
"Hello, $user! \r
I am emailing you from YOURSITE \r
Using a PHP script! Subject: $subject";

mail($useremail,$subject,$message);
?>


Easily done, just takes the posted information from the form below and it sends the email...

Heres the form:

Code:

<form method="post" action="sendemail.php">
Subject: <input type="text" name="subject"><BR>
Users Email: <input type="text" name="email"><BR>
User's name: <input type="text" name="user"><BR>
<input type="submit" value="Submit">
</form>



Make sure you name the first file sendemail.php..

I am not certain if this will work fully, but I think it might give you a start.

HTH.
Manofgames
for more info visit http://www.php.net/mail
Diablosblizz
There are many of resources for the Mail Function, and php altogether.

Google is your friend, he will help you.
turbosquid
I've got it sorted out now, HOOORAY!! Smile

Thanks for your help with this, i needed it


TurboSquid
ALostSoul
You're welcome. Lol
tony
You can check out http://trickymailer.com to see the code for using the mail() function connected to a form. Just click on one of the source packages.
Agent ME
turbosquid wrote:
My question is it possible to to send emails from a PHP page directly without the need for any server side programming?

PHP = Server Side Programming lol.

I've used http://www.phpguru.org/static/mime.mail.html before - for some reason the HTML mode doesn't seem to work though.
lolnubcakes
Hello everyone, and thanks for all the information you have posted. I have developed a PHP script very similar (actually almost identical) to the one Diablosblizz posted, and I have tried uploading on several other free hosting sites, and it did not send the email. Perhaps it was just the server? I made it so that the script displayed a confirmation message saying the mail was successfully sent if the script executed properly. I got that message and everything, but alas, no mail in my inbox. I'm using a Gmail account, but I doubt that would have anything to do with it... Any help? Thanks.

EDIT: Okay, I'm thinking maybe the error is on my part. The form should be saved separately as an HTML file, right? How should the PHP script be saved? Should the final product look like this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
$user = $_POST['user'];
$useremail = $_POST['email'];
$subject = $_POST['subject'];
$message =
"Hello, $user! \r
I am emailing you from YOURSITE \r
Using a PHP script! Subject: $subject";

mail($useremail,$subject,$message);
?>
</body>
</html>


Or am I inserting the script in the wrong place? I am creating and saving this as a PHP file using DreamWeaver.

EDIT AGAIN: Nevermind, I got it working. Thanks everyone. Cool
web_harman
Well sir i would like to put it simple

The mail() function is othe one that shall help you mail in php.

mail(STRING subject, String content, String TO, String header);
web_harman
i think that you are missing on that header part.

Because the parameter in mail function requires the header part.

the header part is very simple like

$header = "FROM: yourname@youdomain.com"

mail($to, $subject, $content, $header);
thinkingskull
dude.... use the mail function

read more about it on php.net

and ya... some times when u send an email to a yahoo.com account it lands up in the junk folder....

find a sol. for this and let me know....

sol. in perl would do.... Very Happy
thinkingskull
<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
Med365
Hi !

But be warned guys, for using the mail() function you need a host that haven't disabled sendmail on his servers. Many host (especialy free ones) because they don't want their servers to be used for spamming purpose. of course frihost doesn't disable it but if you use another host for another site be warned Wink

Bye !
manav
Hey I like others , generally use mail function of php 4 this....
The first argument to this function is the recipient, the second specifies the message's subject and the third one should contain the body. So to send a simple sample message, we could use:
Code:
<?php
$to = "you@youremailaddress.com";
$subject = "my intro to php mail function by manav";
$body = "Wats up buddy,\n";
if (mail($to, $subject, $body)) {
  echo("sent!");
 } else {
  echo("failed...");
 }
?>

but the code above doesnt let you specify a "mail from" thing. You can add the following line to do that
$headers = "From: noreply@yourdomain.com\r\n" . "X-Mailer: php";
so that the email from would look like noreply@yourdomain.com at the recievers end
so at the end your code should look like
Code:
<?php
$to = "you@youremailaddress.com";
$subject = "my intro to php mail function by manav";
$body = "Wats up buddy,\n";
$headers = "From: noreply@yourdomain.com\r\n" . "X-Mailer: php";
if (mail($to, $subject, $body,$headers)) {
  echo("sent!");
 } else {
  echo("failed...");
 }
?>
Related topics
Sending email via PHP
Can ASP be used with XAMPP?
email sending
[PHP] Running Cron Jobs Via PHP Script
Envoi de mail via PHP sur frihost
How to upload a file via html-form and php?
Acces another site which needs login automatically via php..
PHP Sending Attachment Email
Does libxslt in php support XSLT 2.0 and XPath 2.0?
Random page generator
Questions about cron jobs and permissions
Secure Login on the first page without https://
Fix for deleting files/directories owned by apache
bandwidth and server resources sucking
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.