I have following piece of code. It returns message "send successfully " but we won't receive email at given gmail, Is this code right or have I missed something?
<?php
require("mailer/PHPMailerAutoload.php");
require("mailer/class.smtp.php");
require("mailer/class.phpmailer.php");
$name=$_POST['Your_Name'];
$email=$_POST['Your_Email'];
$message=$_POST['Your_Msg'];
echo $name ;
$mail = new PHPMailer();
// set mailer to use SMTP
//$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = "465";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mandarpatil0003#gmail.com"; // SMTP username
$mail->Password = "Con#Soumitra1"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("mandarpatil0003#gmail.com", "Mandar");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody ="Name : {$name}\n\nEmail : {$email}\n\nMessage : {$message}";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
else
{
echo "Thank you for contacting us. We will be in touch with you very soon.";
}
?>
Does this code have any problems or do I have to try a different approach?
<?php
include 'library.php'; // include the library file
include "classes/class.phpmailer.php"; // include the class name
include "classes/class.smtp.php";
if(isset($_POST["send"])){
$email = $_POST["email"];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; //Hostname of the mail server
$mail->Port = 465; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPDebug = 1;
//$mail->SMTPAuth = false; //Whether to use SMTP authentication
$mail->Username = "****#gmail.com"; //Username for SMTP authentication any valid email created in your domain
$mail->Password = "****"; //Password for SMTP authentication
$mail->AddReplyTo("****#gmail.com", "Reply name"); //reply-to address
$mail->SetFrom("****#gmail.com", "Your Name"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received."); //Put your body of the message you can place html code here
//Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
}
else{
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server by Asif18</title>
<meta name="keywords" content="send mails using smpt in php, php mailer for send emails in smtp, use gmail for smtp in php, gmail smtp server name"/>
<meta name="description" content="Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server"/>
<style>
.as_wrapper{
font-family:Arial;
color:#333;
font-size:14px;
}
.mytable{
padding:20px;
border:2px dashed #17A3F7;
width:100%;
}
</style>
<body>
<div class="as_wrapper">
<h1>Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server</h1>
<form action="" method="post">
<table class="mytable">
<tr>
<td><input type="email" placeholder="Email" name="email" /></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Send via SMTP" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Related
i want to send mail to the client from my site. i am using php sendmail() to send mail.
following is my code :-
<?php
include('Connection.php');
include 'smtp/Send_Mail.php';
if(isset($_POST['submit']))
{
$name = $_POST['userName'];
$mob = $_POST['mobileNo'];
$email = $_POST['emailId'];
$fed = $_POST['feedback'];
$_SESSION['unm']=$name;
$_SESSION['mbl']=$mob;
$_SESSION['eml']=$email;
$_SESSION['fdbk']=$fed;
$sql=" Call addUser('$name','$email','$mob','$fed')";
$result = mysql_query($sql);
$to=$email;
$subject="Thank you for getting in touch.";
$body=' <h1 style="color:blue;"> Dear '. $name.', </h1> <br/>
Thank you for getting in touch';
Send_Mail($to,$subject,$body);
}
?>
and the Send_Mail.php file look like this:
<?php
function Send_Mail($to,$subject,$body)
{
require 'class.phpmailer.php';
$from = "ashmeerafurnishingxxx#gmail.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.gmail.com"; // Amazon SES server, note "tls://" protocol
$mail->Port = 465; // set the SMTP port
$mail->Username = "ashmeerafurnishingxxx#gmail.com"; // SMTP username
$mail->Password = "xxxxxxx"; // SMTP password
$mail->SetFrom($from, 'Ashmeera Furnishing Decor');
$mail->AddReplyTo($from,'Ashmeera Furnishing Decor');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
Its working on localhost bt when i upload the same code on server then the mail was not send.
any configuration settings change required or anything else.
need help ..
thank in advance.
Im trying to use PHPMailer and been getting an error that the Message body empty or the screen is blank and does not print that the message has been sent. My folder structure is everything is in the same folder for testing in case anyone needs to know. I have also followed this post but did not seem to work. PHPMailer Mailer Error: Message body empty
<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
echo $body;
//$body = preg_replace('/[\]/','',$body);
//$body = "To view the message, please use an HTML compatible email viewer!";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "no-reply#yourdomain.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('no-reply#yourdomain.com', 'First Last');
//$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->IsHTML(true);
$mail ->MsgHTML($body);
$mail->Body = $body;
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
//$mail->IsHTML(true);
$address = "someemail#yourdomain.com";
$mail->AddAddress($address, "John Doe");
//$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!";
}
?>
</body>
</html>
Contents.html:
<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<br>
<br>
This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a <div> tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost (codeworxtech#users.sourceforge.net)<br />
Author: Marcus Bointon (coolbru#users.sourceforge.net)<br />
</div>
</body>
I have put the echos in the code for troubleshooting to see if it is getting the data or not. I think this is honestly something to do with Gmail for the mail hosting. But its what they use. Thank you in advanced with any direction you can provide.
Chris
try this
<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
//error_reporting(E_ALL);
date_default_timezone_set('America/Toronto');
include "PHPMailer_5.2.4/class.phpmailer.php";
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$message='<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<br>
<br>
This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a <div> tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost (codeworxtech#users.sourceforge.net)<br />
Author: Marcus Bointon (coolbru#users.sourceforge.net)<br />
</div>
</body>';
$mail = new PHPMailer();
//$body = file_get_contents('contents.html');
echo $message;
//$body = preg_replace('/[\]/','',$body);
//$body = "To view the message, please use an HTML compatible email viewer!";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "no-reply#yourdomain.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('no-reply#yourdomain.com', 'First Last');
//$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->IsHTML(true);
$mail ->MsgHTML($message);
$mail->Body = $message;
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
//$mail->IsHTML(true);
$address = "someemail#yourdomain.com";
$mail->AddAddress($address, "John Doe");
//$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!";
}
?>
</body>
</html>
I tried using summernote
and PHP Mailer to sent the contents to my e-mail.
But the e-mail does not appear to be in HTML form eventhough I put :
$mail->IsHTML(true); //set email format to HTML
Below is my code for sending e-mail:
require '../PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP(); //set mailer to use SMTP
$mail->Host = "mail.xxxyy.com"; //specify SMTP mail server
$mail->Port = "2525"; //specify SMTP Port
$mail->SMTPAuth = true; //turn on SMTP authentication
$mail->Username = "xxxxx#xxxxx.com"; //Full SMTP username
$mail->Password = "xxxxx"; //SMTP password
$mail->From = "xxxxx#xxx.com";
$mail->FromName = "xxxxx";
$mail->AddAddress("xxxxxx", "Mei Yi");
$mail->WordWrap = 50; //optional, you can delete this line
$mail->IsHTML(true); //set email format to HTML
$mail->Subject = "RE:". $_POST[txtFeedbackSubject2];
$mail->Body = htmlspecialchars($_POST['content']);
if(!$mail->Send())
{
echo "Message could not be sent.
";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
The e-mail content appears to be like this:
<span style=\"font-weight: bold; font-style: italic; text-decoration: underline;\">Aadadssdasadsad.</span>
Please advice.
Thanks.
You're applying htmlspecialchars to the body, which will prevent the HTML from being rendered. Don't do that.
Apart from that, your message body will be whatever is in $_POST['content'], over which PHPMailer has no control.
This is my email.php page coding. Now i'm getting the mail from contact form. But, i can't able to change Reply-To email id. Here, I've added $mail->AddReplyTo($email, 'Reply');. But, it doesn't shows the emailid(contact form sender emailid) in Reply-To option.
I've posted an image here. In quick reply option i can see the receiveing end email id. How can i add a reply to email id(contact form sender emailid)?
<?php
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer;
$name = trim($_POST["name"]);
$typeofmotor = trim($_POST["typeofmotor"]);
$email = trim($_POST["email"]);
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = 587;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email#gmail.com'; // SMTP username
$mail->Password = '1234567890'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->FromName = $name;
$mail->AddAddress('email#gmail.com', ''); // Add a recipient
$mail->AddReplyTo($email, 'Reply');
$mail->AddBCC('');
$mail->WordWrap = 50;
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'subject name';
$mail->Body = "<!DOCTYPE html>
<html lang='en-us'>
<head>
<meta charset='utf-8'>
<title></title>
</head>
<body>
<table width=550 style=\"border: 1px solid #333333;\" border=0 align=center cellpadding=0 cellspacing=0>
</table>
</body>
<html>";
if(!$mail->Send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
else
{
echo '<script type="text/javascript"> alert("Message has been sent"); </script>';
echo '<script type="text/javascript"> window.location = "contact.php"; </script>';
}
?>
Instead of this:
$mail->FromName = $name;
$mail->AddAddress('email#gmail.com', ''); // Add a recipient
$mail->AddReplyTo($email, 'Reply');
Try to use this:
$mail->AddReplyTo($email, 'Reply');
$mail->SetFrom('mailbox#name.com', 'Mailbox name');
$mail->AddAddress('email#gmail.com', '');
Notice:
the reply to addresses needed to be added before the from address
I am trying to send mails using php. I am using wampserver2.2 . And my php version is 5.4.3
Here is the code I tried:
<?php
if(isset($_POST['submit']))
{
$YourName = mysql_real_escape_string($_POST['YourName']);
$From = mysql_real_escape_string($_POST['Email']);
$Subject = mysql_real_escape_string($_POST['Subject']);
$Message = mysql_real_escape_string($_POST['message']);
$myEmailAdr = "umvss01#gmail.com";
if (preg_match('/^[a-zA-Z][a-zA-Z0-9_-]+#[a-zA-Z]+[.]{1}[a-zA-Z]+$/', $From))
{
$headers = 'From: '.$From."\r\n".'Reply-To: '.$From;
$Message = str_replace("\n.", "\n..", $Message);
try
{
ini_set("SMTP","ssl:smtp.gmail.com" );
ini_set("smtp_port","465");
$send = mail($myEmailAdr, $Subject, $Message, $headers ); // line 24
}
catch(PDOException $e)
{
$error = $e->getMessage();
echo $error;
}
}
else
{
echo "Error in Mail Address Format";
}
}
?>
The problem is I am getting the following error when I try to use it. Line 24 means the mail function.
Warning: mail(): Failed to connect to mailserver at
"ssl:smtp.gmail.com" port 465, verify your "SMTP"
and "smtp_port" setting in php.ini or use ini_set() in
C:\wamp\www\UMVSS_Website\ContactScript.php on line 24
The PHP mail() function doesn't use authentication to the SMTP server, which generally results in an error when connecting to public servers like the one you are trying to connect to. You might want use a library like PHPMailer which you can autheticate with.
So, GMail requires SMTP authentication. Mail does not provide that interface.
Check out PHPMailer. Here is an example for connecting to GMail
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../class.phpmailer.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->SetFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->AddReplyTo('replyto#example.com','First Last');
//Set who the message is to be sent to
$mail->AddAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
$mail->MsgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->AddAttachment('images/phpmailer_mini.gif');
//Send the message, check for errors
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>