0

I would like to know how I would send an email to a user after they submit a form to my website..

Background: I am relaunching a site that my father shut down in 2002, we currently have a welcome/splash page that says the site is going to coming back soon. This page asks the user to input their email to be added to the launch list. This form uses a "web form mailer" as below:

<form action="/webformmailer.php" method="post">

Their email is sent to a inbox that we chose. What I would like to do is send this user an email that says thanks, look forward to updates, thanks again, etc..

How would I go about this? I have no idea and any help would be much appreciated!!

hobbs
  • 223,387
  • 19
  • 210
  • 288
CFitz
  • 25
  • 1
  • 7

4 Answers4

1

If you want to send a very simple plain text e-mail then you can use PHP mail() function as it's quick to do. (That's assuming that your web host has sendmail set up). Otherwise the SMTP functiosn are an option (answer above). But make sure you verify all user-submitted input to ensure that your mail can't be used by spammers - most importantly, strip out line feeds from email addresses and user names etc.

But what is a much better option is to use a pre-written library as this takes away much of the questions/problems. SwiftMailer is currently probably one of the best as it can cope with a variety of sending methods (SMTP, sendmail) and formats (text, HTML etc) andwill help prevent tour form being used to relay spam. It's well documentent too (if you don;t use IE - the documentation pages don't work in IE!)

If you want to send attachments or HTML e-mails then it's also possible through mail(), but a bit of a fight getting it to work across all platforms. Again, SwiftMailer (and other libraries) make this simple. So may as well start with a library.

Robbie
  • 17,605
  • 4
  • 35
  • 72
0

Try this

SMTPconfig.php

<?php
//Server Address
$SmtpServer="127.0.0.1";
$SmtpPort="25"; //default
$SmtpUser="username";
$SmtpPass="password";
?>

SMTPclass.php

<?php
class SMTPClient
{

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{

$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;

if ($SmtpPort == "") 
{
$this->PortSMTP = 25;
}
else
{
$this->PortSMTP = $SmtpPort;
}
}

function SendMail ()
{
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
{
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n"); 
$talk["hello"] = fgets ( $SMTPIN, 1024 ); 
fputs($SMTPIN, "auth login\r\n");
$talk["res"]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpUser."\r\n");
$talk["user"]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpPass."\r\n");
$talk["pass"]=fgets($SMTPIN,256);
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n"); 
$talk["From"] = fgets ( $SMTPIN, 1024 ); 
fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n"); 
$talk["To"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
$talk["send"]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT ... 
fputs ($SMTPIN, "QUIT\r\n"); 
fclose($SMTPIN); 
// 
} 
return $talk;
} 
}
?>

index.php

<?php
include('SMTPconfig.php');
include('SMTPClass.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['sub'];
$body = $_POST['message'];
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
}
?>
<form method="post" action="">
To:<input type="text" name="to" />
From :<input type='text' name="from" />
Subject :<input type='text' name="sub" />
Message :<textarea name="message"></textarea>
<input type="submit" value=" Send " />
</form>
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
0

You can use this code to send email in PHP. :

<?php

   require_once "Mail.php";

    $from = "<from.gmail.com>";
    $to = "<to.yahoo.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "myaccount@gmail.com";  //<> give errors
    $password = "password";

    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
     } else {
      echo("<p>Message successfully sent!</p>");
     }

?>  <!-- end of php tag-->

And if you want to send mass email using PHP then you can find solution over HERE.

Community
  • 1
  • 1
Bhavik Patel
  • 752
  • 1
  • 5
  • 20
0

You can use php's mail() function or use a class like PHPmailer (download from here). The latter gave me better results.

<?php

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

This is very easy to use and requires minimum setup. You can use $_POST to set the receivers address etc. You may want to validate the email address first. Use javascript of PHP validation

Sid
  • 1,255
  • 2
  • 22
  • 45