Getting a warning message after opening email using PHP - php

I am using PHPMailer for sending an email. Emails are getting proper but whenever I am opening email which I was sent using PHP Mailer I am getting a warning message.
Note: If I remove anchor tag from $phpMailerText then I am not getting any warning.If I add anchor tag then I am getting warning.Would you help me in this?
require 'mail/PHPMailerAutoload.php';
$to = $email;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Headers
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-mailer: smtp.gmail.com" . "\r\n" // this will identify the real sender
. "Precedence: bulk" . "\r\n" // this will say it is bulk sender
. "List-Unsubscribe:abc#gmail.com\r\n" // this will reveal the OPT-OUT address
. "Reply-To: $to\n"
. "To: $to\n"
. "From: $to\n";
//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 = "abc#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "****";
//Set who the message is to be sent from
$mail->setFrom('abc#gmail.com', 'code');
//Set an alternative reply-to address
$mail->addReplyTo('abc#gmail.com', 'code');
//Set who the message is to be sent to
$mail->addAddress($to, 'Customer');
//Set the subject line
$mail->Subject = 'code';
$phpMailerText="<!DOCTYPE HTML><html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
$mail->msgHTML($phpMailerText);
//Replace the plain text body with one created manually
$mail->AltBody = ' ';
//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else {
}

Try the following code and do the other steps.
Create reverse dns record
Configure SPF records
$to = $email;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Headers
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-mailer: YOUR_SITE_DOMAIN Server" . "\r\n" // this will identify the real sender
. "Precedence: bulk" . "\r\n" // this will say it is bulk sender
. "List-Unsubscribe:info#YOUR_SITE_DOMAIN\r\n" // this will reveal the OPT-OUT address
. "Reply-To: $email\n"
. "To: $email\n"
. "From: $email\n";
$mail->addCustomHeader( $headers );
//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 = "me#companydomain.com";
//Password to use for SMTP authentication
$mail->Password = "****";
// Because html is being used
$mail->isHTML(true);
//Set who the message is to be sent from
$mail->setFrom('me#companydomain.com', 'code');
//Set an alternative reply-to address
$mail->addReplyTo('me#companydomain.com', 'code');
//Set who the message is to be sent to
$mail->addAddress($to, 'Customer');
//Set the subject line
$mail->Subject = 'code';
$phpMailerText="<!DOCTYPE HTML><html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
$mail->msgHTML($phpMailerText);
//Replace the plain text body with one created manually
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else {
}

Please use following code and tell me what happends.
$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("email#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}

Related

PHPMailer is not sending mails

Guys I have read many answers on this question but the problem still persists.
The PHPMailer is not sending emails giving the following error.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Following is the code:
date_default_timezone_set('Etc/UTC');
require 'PHPMailer-master/PHPMailerAutoload.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 = 0;
//Ask for HTML-friendly debug output
//$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.domain.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port =25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
//Username to use for SMTP authentication
$mail->Username = "mail#mail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom("mail#mail.com","Contact Form");
//Set an alternative reply-to address
$mail->addReplyTo($_POST['user_mail'], $_POST['user_name']);
//Set who the message is to be sent to
$mail->addAddress("mail#mail.com", "Contact Form");
//Set the subject line
$mail->Subject = $_POST['subject'] . " - Contact Form";
//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("Message from ". $_POST['user_name']." : <br/>".$_POST['message'] . "<br/> Contact Details : <br/> Email :" . $_POST['user_mail'] . "<br/> Contact Number : " . $_POST['user_number']);
//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.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<script>window.location.href='thankyou.html';</script>";
}

SMTP Mail Not Working ON Server Working Fine On Localhost

Hi i am using smtp latest files and check all the solution availble on net, My SMTP php mail working on localhost not working on server my server's web mail working Fine.First of all i am inserting data into my database then those data i want to sent admin email id my data are inserting in database also mail sending working fine on localhost but given error on server it give's failed to connect with server. i contact with my hosting server support forum but they are saying this your code problem
Here is my PHP Code
<?php
if(isset($_POST['Isubmit'])){
$rname= $_POST['Iname'];
$remail= $_POST['Iemail'];
$rcontact= $_POST['Icontact'];
$rmessage= $_POST['Icomment'];
$rcourse= $_POST['Icourse'];
$date=date("Y-m-d");
$sql=mysqli_query($connect,"INSERT INTO enquiry(`NAME`,`EMAIL_ID`,`MOBILE_NO`,`COMMENT`,`SUBJECT`,`IS_DELETED`,`DATE`)VALUES('".$rname."','".$remail."','".$rcontact."','".$rmessage."','".$rcourse."','1','".$date."')");
//Mail Send Code
$ToEmail = 'admin#gmail.com';
$EmailSubject = 'Site contact form';
$MESSAGE_BODY = "Dear Sir/Mam,"."\n" ."\n"."New Contact Details are - "."\n"."\n";
$MESSAGE_BODY .= "Full Name: ".$rname."\n"."\n";
$MESSAGE_BODY .= "Email: ".$remail."\n"."\n";
$MESSAGE_BODY .= "Phone: ".$rcontact."\n"."\n";
$MESSAGE_BODY .= "Message: ".$rmessage."\n"."\n";
$MESSAGE_BODY .= "Course: ".$rcourse."\n"."\n";
//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'thave access to that date_default_timezone_set('Etc/UTC');
require 'PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging // 0 = off (for production use)
$mail->SMTPDebug = 0;
//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 = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//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";//"lino.cspace#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";//"lino#123";
//Set who the message is to be sent from
$mail->setFrom('admin#gmail.com', 'Admin');
$mail->addAddress($ToEmail);
//Set the subject line
$mail->Subject = 'Enquiry Mail';
$mail->Body = $MESSAGE_BODY;//'this is test msg on 25-jun.';
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
}
Enable smtp debugging
$mail->SMTPDebug = 1;
OR
$mail->SMTPDebug = 2;
Then the error info will give you more info on the error
if (!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!<br>";
}

mail() is not working in php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am using mail() function to sent mails when an event happening. But it is not working as I expected. I tried to get the return of the function also. Some one please suggst what may be the issue.
$msg = "Your password has been changed.is'".$data['password']."'";
$to = $data['email'];
$subject = "password changed";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info#hia.com';
$send = mail($to, $subject, $msg, $headers);
if($send){
echo "successful";
}
else{
echo "error";
}
Problems arising from the php setting. Closing function for security by some service provider. You must send mail with SMTP.
Example several SMTP class on github;
hujuice/smtp - snipworks/php-smtp - PHPMailer/PHPMailer
Example code a PHPMailer according to your code;
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#hia.com'; // SMTP username
$mail->Password = 'yourmailpassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('info#hia.com', 'Mailer');
$mail->addAddress($data['email']); // Name is optional
$mail->addReplyTo('info#hia.com', 'Information');
$mail->addCC('cc#hia.com');
$mail->addBCC('bcc#hia.com');
$mail->CharSet = 'ISO-8859-1';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'password changed';
$mail->Body = 'Your password has been changed.is "'.$data['password'].'"';
if(!$mail->send()) {
echo 'error';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'successful';
}
Don't use simple mail(), i prefer for PHPmailer function.
Here is a Example.
First - download phpmailer in your project directory.
second - create send_mail.php in your directory(you can change you name).
send_mail.php file code.
require_once "PHPMailer/PHPMailerAutoload.php";
$mail = new PHPMailer(true);
if (!isset($_POST['send'])) {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the enquiry form!";
die;
}
$to="abc#xyz.com";
$senderName = "xyz";
//send the mail
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$zcode = $_POST["zcode"];
$email = $_POST["email"];
$phone = $_POST["phone"];
//Enable SMTP debugging.
$mail->SMTPDebug = 0;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "abc#xyz.com";
$mail->Password = "########";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = $to;
$mail->FromName = $senderName;
$mail->addAddress($to, $senderName);
$mail->addReplyTo($email, $name);
$mail->isHTML(true);
$mail->Subject = "bfuiebfiaif";
$mail->Body = "as per your needs";
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
die;
}
header('Location: index.php');
die;

How to send email with SMTP in php

I want to send email with SMTP in my project, previously i write php mail() in my project but now my client want that i should use SMTP. I search about this but i get nothing any proper solution for this.
In my php mail() i send name, subject and comment, so how can i do this in SMTP.
Here is my code:
$payer_email = "Your Email";
$subject = "Your Subject";
$message = 'Dear '.$name.',
Thank you for your purchase from '.$site_url.'. The details of your purchase are below.
Transaction ID: '.$txn_id.'
Item Name: '.$item_name.'
Payment Amount: '.$payment_amount.'
Payment Amount: '.$payment_status.'
Paid to: '.$receiver_email.'
Thanks and Enjoy!';
$headers .= 'From: ' .$from. "\r\n" .'Reply-To: ' .$from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1 ";
//mail to buyer
mail( $payer_email , $subject, $message, $headers );
Please give me some suggestions or simple and nice tutorials.
Take a look at PHP Mailer:
https://github.com/PHPMailer/PHPMailer
Example from that page:
<?php
require 'PHPMailerAutoload.php';
$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'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$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->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 = '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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
Please try this
Create library file for the SMTP Settings 'library.php':
<?php
error_reporting(0);
define("SMTP_HOST", "SMTP_HOST_NAME"); //Hostname of the mail server
define("SMTP_PORT", "SMTP_PORT"); //Port of the SMTP like to be 25, 80, 465 or 587
define("SMTP_UNAME", "VALID_EMAIL_ACCOUNT"); //Username for SMTP authentication any valid email created in your domain
define("SMTP_PWORD", "VALID_EMAIL_ACCOUNTS_PASSWORD"); //Password for SMTP authentication
?>
Make the form post and do the below actions:
<?php
include 'library.php';
include "classes/class.phpmailer.php"; // include the class file name
if(isset($_POST["send"])){
$email = $_POST["email"];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->AddReplyTo("reply#yourdomain.com", "Reply name"); //reply-to address
$mail->SetFrom("from#yourdomain.com", "Asif18 SMTP Mailer"); //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. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>"); //Put your body of the message you can place html code here
$mail->AddAttachment("images/asif18-logo.png"); //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;
}
}
?>
Please edit your email and password correctly.
You may see the demo and source code on Click here
you can employ the use of the phpmailer library since the mail() function has limitations in its use. so I prefer using PHPmailer it is easier to work with than the mail() function.
I also used the mail() function it requires a lot of settings here and there and cannot be used to send remote mail messages.
so in short, the mail() function is only suitable when working with the local server but not suitable when you want to work with a remote server.

How to configure mail().php, so I can use it with the gmail SMTP

define('SITE_EMAIL', 'example#mydomain.la');
$to1= SITE_EMAIL;
$subject1 = "Contacto Web";
$message1 = 'Hi!';
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers1 .= "From: ".$_POST['name']." <".$_POST['email'].">\r\n";
mail($to1, $subject1, $message1, $headers1);
print "message send!";
I'm using this code to send an email from a contact form. But mostly goes to spam or even the mail is not sent. What they recommended was to validate the header. The form and the php file is in my domain but I use google apps, so I think I have to use the google smtp. But I really don't know how...
I don't think there's an easy way to use mail() with gmail.
Give phpmailer a try.
This is a sample of using it with gmail:
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');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.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!";
}

Categories