adding smtp authentication to PHP - php

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!';
}

Related

WordPress error in functions.php

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';

Changing form php sendmail to SMTP?

We created this contact form a long time ago, but we have recently had issues with our sendmail, now i need to change this to use SMTP and that i haven't done before. Is it much work or just a matter of changing few lines? Any tips are welcome.
You can see our whole script here, it's very simple...
<?php
if(!$_POST) exit;
function tommus_email_validate($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/#.+\./', $email);
}
$name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $comments = $_POST['comments'];
if(trim($name) == '') {
exit('<div class="error_message">You must enter your name.</div>');
} else if(trim($name) == 'Name') {
exit('<div class="error_message">You must enter your name.</div>');
} else if(trim($email) == '') {
exit('<div class="error_message">Please enter a valid email address.</div>');
} else if(!tommus_email_validate($email)) {
exit('<div class="error_message">You have entered an invalid e-mail address.</div>');
} else if(trim($comments) == 'Tell us what you think!') {
exit('<div class="error_message">Please enter your message.</div>');
} else if(trim($comments) == '') {
exit('<div class="error_message">Please enter your message.</div>');
} else if( strpos($comments, 'href') !== false ) {
exit('<div class="error_message">Please leave links as plain text.</div>');
} else if( strpos($comments, '[url') !== false ) {
exit('<div class="error_message">Please leave links as plain text.</div>');
} if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }
$address = 'hello#basicagency.com';
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name from your contact form, their additional message is as follows." . "\r\n" . "\r\n";
$e_content = "\"$comments\"" . "\r\n" . "\r\n";
$e_reply = "You can contact $name via email, $email (or by phone if supplied: $phone)";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
if(mail($address, $e_subject, $msg, $headers)) {
echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
You can use a package for handling e-mails, such as PHPMailer, just use the included methods to build your message, instead of the $headers variable. Once you have downloaded PHPMailer and have it somewhere accessable by your script, replace this:
$headers = "From: $email" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
if(mail($address, $e_subject, $msg, $headers)) {
echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
with something like this:
require '/path/to/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
// SMTP server details
$mail->Host = "mail.example.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "yourname#example.com";
$mail->Password = "yourpassword";
// message details
$mail->setFrom($email, $email);
$mail->addReplyTo($email, $email);
$mail->addAddress($address, $address);
$mail->Subject = $e_subject;
$mail->Body = $msg;
// send
if($mail->send()) {
echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}

How to implement reCAPTCHA in a current PHP email form

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.

Php is not sending mail to ''Bcc'' and "to"

My inquiry form was working properly but now it only send e-mail to Cc e-mail id
<?php
if($_REQUEST["name"])
{
?>
<?php
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['comment'])){
$email=$_POST['comment'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=xxx&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
?>
<?php
if($_REQUEST["emailid"] || $_REQUEST["phone"])
{
$to = "mail2#123.in";
$subject = "Enquiry on tour Packages from ".$_REQUEST["name"];
$message = "
<html>
<head>
<title>Enquiry from ".$_REQUEST["name"]."</title>
</head>
<body>
<p><b>Dear Sir</b>,<br> I am interested in Tour Packages, my details are given below</p>
<table>
<tr>
<td><b>Full Name : </b>".$_REQUEST["name"]."</td></tr>
<tr><td><b>Email ID : </b>".$_REQUEST["emailid"]."</td></tr>
<tr><td><b>Package : </b>".$_REQUEST["type"]."</td></tr>
<tr><td><b>Date of Arrival: </b>".$_REQUEST["date"]."</td></tr>
<tr><td><b>Country: </b>".$_REQUEST["country"]."</td></tr>
<tr><td><b>Phone: </b>".$_REQUEST["phone"]."</td></tr>
<tr><td><b>Adults Number : </b>".$_REQUEST["adults"]."</td></tr>
<tr><td><b>Kids Number: </b>".$_REQUEST["kids"]."</td></tr>
<tr><td><b>Comments: </b>".$_REQUEST["comments"]."</td></tr>
</table>
<br>
<p>Regards, <br> ".$_REQUEST["name"]."</p>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: mail#123.com' . "\r\n";
$headers .= 'Reply-To: no_reply#123.com' . "\r\n";
$headers .= 'Cc: mail1#123.com' . "\r\n";
$headers .= 'Bcc: mail#123.com' . "\r\n";
if (mail($to,$subject,$message,$headers))
{
echo " Hi ".$_REQUEST["name"]." ! Your enquiry Sent Successfully";
?>
<?php ; ?>
<?php
} else
{
echo "Mailer Error: Contact Reservation Department, call Tel: +91 484 2381122, 4144144 or mail to emailid#xxx.com";
}
}
}
?>
I hope this help you,
First:
Download this library (https://github.com/ivantcholakov/codeigniter-phpmailer --OR-- https://travis-ci.org/PHPMailer/PHPMailer )
Second:
Copy - Paste the library in Your Controller
Here's how to use it in your code,
private function send_forgot_password($data) {
require(APPPATH.'controllers/mail-master/PHPMailerAutoload.php');
// $email_encode=urlencode($data['email']);
$mail = new PHPMailer;
// $mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'mail-host.com';
$mail->SMTPAuth = true;
$mail->Username = 'testing-alert#mail-host.com';
$mail->Password = '12345';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
// Email body
$mail->From = 'testing-alert#mail-host.com';
$mail->FromName = 'Name';
$mail->addAddress($data['email']);
$mail->isHTML(true);
$mail->Subject = 'Forgot Password';
$mail->Body = ' email body ';
$mail->send();
}

Error with sending email

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

Categories