Send Email In Php When Form Is Submitted
I have this code for a form
Solution 1:
Disclosure: I'm one of the developers behind AlphaMail
I would recommend that you use a Transactional Email Service such as:
- AlphaMail
- Mailgun
- SendGrid
Why?
- You don't have to think that much about email delivery.
- Statistics. Let's you track Total Sent/Clicks/Opens/Bounces.
- Often web service-based instead of SMTP. I.e. easier to handle.
- Cleaner code (at least if you use AlphaMail that separates data from presentation).
- Scalable and future proof.
If you choose to go with AlphaMail you could use the AlphaMail PHP-client.
Example:
include_once("comfirm.alphamail.client/emailservice.class.php");
$email_service = AlphaMailEmailService::create()
->setServiceUrl("http://api.amail.io/v1")
->setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");
$person = newstdClass();
$person->userId = "1234";
$person->firstName = "John";
$person->lastName = "Doe";
$person->dateOfBirth = 1975;
$response = $email_service->queue(EmailMessagePayload::create()
->setProjectId(12345) // Your AlphaMail project (determines template, options, etc)
->setSender(new EmailContact("Sender Company Name", "from@example.com"))
->setReceiver(new EmailContact("Joe Doe", "to@example.org"))
->setBodyObject($person) // Any serializable object
);
Another advantage with AlphaMail is that you can edit your templates directly in the AlphaMail Dashboard, and you can format your emails using the Comlang template language.
<html><body><b>Name:</b> <# payload.firstName " " payload.lastName #><br><b>Date of Birth:</b> <# payload.dateOfBirth #><br>
<# if (payload.userId != null) { #>
<ahref="/sign-up">Sign Up Free!</a>
<# } else { #>
<ahref="/login?id=<# payload.userId #>">Sign In</a>
<# } #>
</body></html>
Solution 2:
Use gmail as your mail server.
require_once('class.phpmailer.php');
include_once('class.smtp.php');
$mail = new PHPMailer(); // defaults to using php "mail()"$body = "<html></html>"; //html stuff$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication$mail->SMTPSecure = "ssl"; // sets the prefix to the server$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server$mail->Port = 465; // set the SMTP port$mail->Username = "id@gmail.com"; // GMAIL username$mail->Password = "password"; // GMAIL password$mail->From = "id@gmail.com";
$mail->FromName = "Admin";
$mail->Subject = "Welcome";
$mail->WordWrap = 50; // set word wrap$mail->AddReplyTo("id@gmail.com","Admin");
$mail->AddAddress($email_id); //receiver's id$mail->IsHTML(true); // send as HTML$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
if(!$mail->Send()){
$msg = "Mailer Error: ".$mail->ErrorInfo;
header("Location: http://{$_SERVER['HTTP_HOST']}/site/index.php?msg=$msg");
}else{
$msg="Message sent successfully!";
header("Location: http://{$_SERVER['HTTP_HOST']}/site/index.php?msg=$msg");
}
Download class.phpmailer.php and class.smtp.php and keep them in your root
EDIT:
In register-exec.php,
$to = $_POST['login'];
$name = $_POST['fname'];
$password = $_POST['password']; // you've given the name as 'password' in your form
You can use these variables..like
$body = "<div>Hi '.$name.'!<br>Your id:'.$to.',<br>Your Password:'.$password.'</div>";
Post a Comment for "Send Email In Php When Form Is Submitted"