I got problem in email send. When i use domain email in my php script i got mail but when i use gmail it not send and it show error.
Here is code (http://pastebin.com/XTCB3mch):
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="error_message">Attention! Please enter a valid phone number.</div>';
exit();
} else if(!is_numeric($phone)) {
echo '<div class="error_message">Attention! Phone number can only contain digits.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have entered an invalid e-mail address. Please try again.</div>';
exit();
}
if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
exit();
} else if(trim($comments) == '') {
echo '<div class="error_message">Attention! Please enter your message.</div>';
exit();
} else if(!isset($verify) || trim($verify) == '') {
echo '<div class="error_message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($verify) != '4') {
echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
//$address = "example#example.net";
$address = "mdali_siddique#yahoo.com";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'You have been contacted by ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email or via phone $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: ali#uparrowconsulting.com" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
In line number 81: $headers = “From: ali#uparrowconsulting.com” . PHP_EOL;
When i change it to gmail id it show error. What is the solution.
I also try PHPmailer Here is code (http://pastebin.com/BaDVLxch):
<?php
$name= $_REQUEST['name'] ;
$email= $_REQUEST['email'] ;
$comments= $_REQUEST['comments'] ;
require("/home/uparw/public_html/demo6/14/PHPMailer-master/class.phpmailer.php");
$mail = new PHPMailer;
// For Useing Domian mail
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 0;
$mail->Host = 'localhost'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'ali#uparw.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
// For Gmail
//$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
//$mail->Host = "smtp.gmail.com";
//$mail->Port = 465; // or 587
//$mail->IsHTML(true);
//$mail->Username = "aliuparrow#gmail.com";
//$mail->Password = "password";
$mail->From = 'ali#uparw.com';
$mail->FromName = $name;
$mail->addAddress('alisiddique2011#gmail.com', 'Ali Siddique'); // Add a recipient
//$mail->addAddress('alisiddique2011#gmail.com'); // Name is optional
//$mail->addReplyTo('alisiddique2011#gmail.com', 'Ali');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->WordWrap = 50; // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Query From healthjourney ';
$mail->Body = 'Email: ' . $email . '<br />' . 'Comment: ' . $comments;
$mail->AltBody = 'Email: ' . $email . '<br />' . 'Comment: ' . $comments;
//if(!$mail->send()) {
//echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo;
// exit;
//}
$output = '';
if(!$mail->Send())
{
$output .= "Mailer Error: ". $mail->ErrorInfo;
}
else
{
ob_clean();
header('Location: thankyou.html');
exit();
}
echo $output;
?>
Line number 30: $mail->From = ‘ali#uparw.com’;
Same Problem is here also. If I change it gmail or other ac mail not sending. But if i use domain mail mail sending well.
Need Some Help.
If you need to send email using your GMail account then you will need to use SMTP. Please check the following link for more details
http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server
If all your details are correct and you are doing everything as suggested in the above link then your email should be sent out
Related
I'm trying to send an php mail in a website. I want to add smtp authentication. Below is the php code for send email. How can I add smtp authentication to the below script. Is there any installer I have to download. Please help me with the simplest way to implement this. the smtp details that I have are $host, $username, $password
<?php
if(!$_POST) exit;
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($email) == '') {
echo '<div class="error_message">enter a valid email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">invalid e-mail address, try again.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">enter your message.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "example#example.com";
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name, their message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h4 class='highlight'>Thank you <strong>$name</strong>, your message has been submitted to us.</h4>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
Thank you
One alternative is to use PHPMailer from https://github.com/PHPMailer/PHPMailer
Authentication Sample
Download or install with Composer
Add these code at beginning of your file
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
Then to send your email
...
if (get_magic_quotes_gpc())
{
$comments = stripslashes($comments);
}
$e_body = "You have been contacted by {$name}, their message is as follows.\r\n\r\n";
$e_content = "\"{$comments}\"\r\n\r\n";
$e_reply = "You can contact $name via email, $email";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->Port = 25;
$mail->From = "from#email.com";
$mail->SMTPAuth = true;
$mail->Username = "SMTP_username";
$mail->Password = "SMTP_password";
$mail->setFrom($email, $name);
$mail->addReplyTo($email, $name);
$mail->addAddress("to#email.com");
$mail->Subject = "You've been contacted by {$name}.";
$mail->setWordWrap(70);
$mail->Body = $e_body . $e_content . $e_reply;
$mail->isHTML(false);
$mail->CharSet = 'UTF-8';
if ($mail->send())
{
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h4 class='highlight'>Thank you <strong>$name</strong>, your message has been submitted to us.</h4>";
echo "</div>";
echo "</fieldset>";
}
else
{
echo 'ERROR!';
}
When I try to link up this file in functions.php of WordPress, my site has been blank, I would like to link up the file in functions.php because I want to make the $address variable as dynamic by redux framework, but I can't do that, because redux value is not getting on that file,
Thanks in advance
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
if(trim($name) == '') {
echo '<div class="alert alert-error">You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="alert alert-error">You must enter email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="alert alert-error">You must enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="alert alert-error">Please fill all fields!</div>';
exit();
}
else if(trim($comments) == '') {
echo '<div class="alert alert-error">You must enter your comments</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
//$address = "example#themeforest.net";
$address = 'yourmail#gmail.com';
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Contact Form';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<div class='alert alert-success'>";
echo "<h3>Email Sent Successfully!</h3><br>";
if ($sender_name==1) : ?>
<?php echo '<p>';
echo "<strong>$name</strong>"; ?>
<?php endif; ?>
<?php echo $mailcomments; ?>
<?php echo "</p>";
echo "</div>";
} else {
echo 'ERROR!';
}
You missed a single quote:
$address = 'yourmail#gmail.com;
change to:
$address = 'yourmail#gmail.com';
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm a bit of an amateur when it comes to PHP and so need some help please. I have made a contact form on my website which can be seen here:
http://babylace.co.uk/thegardenmedic.co.uk/contact.html
I am using a contact.php file to handle the form submission. When I fill in the form it submits and says successful (feel free to try) but I am not receiving the email in my inbox. I have changed the email in this question to example to keep my email private. My code for contact.php is as follows:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$comments = $_POST['comments'];
if(trim($name) == '') {
echo '<div class="alert alert-error">You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="alert alert-error">You must enter a valid email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="alert alert-error">You must enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="alert alert-error">You must enter your phone number.</div>';
exit();
} else if(trim($address) == '') {
echo '<div class="alert alert-error">You must enter your post code.</div>';
exit();
} else if(trim($comments) == '') {
echo '<div class="alert alert-error">You must include a message.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "example#example.com";
//$address = "example#example.com"";
$address = "example#example.com"";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Contact Form';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email $email or via Phone $phone, $email";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<div class='alert alert-success'>";
echo "<h3>Message Sent Successfully.</h3><br>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";
} else {
echo 'ERROR!';
}
Your script seems fine so far. Though, it might not be your PHP code which is wrong. Maybe you need to setup your SMTP Auth, see here:
Sending email with PHP from an SMTP server
I am new to PHP, but know my way around HTML. I have been asked to implement a reCaptcha form into our site's PHP contact email form. I'm coming up empty. I have signed up and have my site key and secret key, just no idea how to integrate it into our form.
Here is our sites PHP form:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
if(trim($name) == '') {
echo '<div class="error_message">Please enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Please enter a valid email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">You have entered an invalid e-mail address, try again.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Please enter your message.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "yourname#yourdomain.com";
$address = "contact#########.com";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'You\'ve been contacted by ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name from your website, their message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name by email, $email or by phone $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h2>Email Sent Successfully.</h2>";
echo "<p>Thank you <strong>$name</strong>, your message has been sent to us.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
Any help is appreciated. Thanks!
First, as a part of your form elements, include reCaptcha:
<?php
require_once("location/of/recaptchalib.php");
$publickey = "yourPublicKeyGoesHere";
echo recaptcha_get_html($publickey);
?>
in your send_email.php file you can do this:
<?php
session_start();
require_once("location/of/recaptchalib.php");
$timezone = "Asia/Kuwait";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$privatekey = "YourPrivateKey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]
);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
else {
$sender_name = $_POST['sender_name'];
$sender_email = $_POST['sender_email'];
$query_type = $_POST['query_type'];
$ip = $_SERVER["REMOTE_ADDR"];
$date = date("d M Y H:i:s");
$sender_message = "Thank You for your message, ".$sender_name."\r\n"."Message was sent from ".$ip."on ".$date."\r\n".$_POST['sender_message'];
$headers = "From: Your Website" . "\r\n" .
"BCC: you#yourwebsite.com";
mail($sender_email,'Email from yourwebsite guest',$sender_message,$headers);
echo "Message Sent!";
}
?>
This worked for me, and the form looks like this:
I hope this helps.
Please I need help with this. My contact form is not sending message to my gmail. I have checked my spam also and nothing came. Not sure if it's something to do with my code. Thanks in advance. I have realized that the message is going into my godaddy cpanel. The subject is
mail failure - malformed recipient address
The message body says
A message that you sent contained a recipient address that was incorrectly constructed:
from: missing or malformed local part (expect word or "<")
The message has not been delivered to any recipient.
Please also ignore the $ip field that part of my code wasn't added.
<?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
//declares variable
$email = $_POST['email'];
$comment = $_POST['comment'];
if(empty($_POST['email'])){
$emailErr = "Please enter your email";
}
if(empty($_POST['comment'])){
$commentErr = "comment field can't be empty";
}
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
// Send the email
$to = "myname#gmail.com";
$email = "From: $email";
$comment = "Message: $comment";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";
$headers = "From: $email,";
$send_contact = mail($to,$email,$comment,$message,$headers);
header("Location: index.php");
}
?>
This should work.
<?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
//declares variable
$email = $_POST['email'];
$comment = $_POST['comment'];
if(empty($_POST['email'])){
$emailErr = "Please enter your email";
}
if(empty($_POST['comment'])){
$commentErr = "comment field can't be empty";
}
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
// Send the email
$to = "myname#gmail.com";
$title = "Message from my website";
$message = "Comment: {$comment}" . "\r\n";
$message .= "Sent from the website with IP Address: {$ip}" . "\r\n";
$headers = "From: " . strip_tags($email) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
mail($to,$title,$message,$headers);
header("Location: index.php");
}
?>
I Advice you learn how to use the mail function here http://php.net/manual/en/function.mail.php
Heyy Good Morning,
If you are working in offline mode maybe you need phpmailer.
First you need download phpmailer from here https://github.com/PHPMailer/PHPMailer/archive/master.zip
Then paste in your folder. If my coding doesn't clear you, you can check from
https://github.com/PHPMailer/PHPMailer
<?php
require 'PHPMailerAutoload.php'; // Your Path
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // Your mail
$mail->Password = 'secret'; // Your mail password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//Check Condition
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Second way.
If you working testing in online mode(Have own domain and hosting), you can just randomly copy and paste.
Doesnt required phpmailer.
<?php
$error = [];
$receipientName="Fido";
$receipientEmail ="receipientmail.gmail.com";
$ccEmail ="";
//declares variable
if(isset($_POST['name'])) $name = $_POST['name'];
else $name = "";
if(isset($_POST['email'])) $email = $_POST['email'];
else $email = "";
function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$myname."\" <".$myemail.">\r\n";
return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers));
}
if(isset($Submit) && $Submit=="Go") {
$emailContent ='';
$sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent);
if($sent) {
echo $emailContent;
header('Location: contact.php');
}else{
echo "Failed";
exit;
}
}
?>