Please help me I am New in PHP and since last 5 Hours i am try to semd mail and now really tired. Thanks.
Here is my code. I am using Gmail account.
include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$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 = "hussaintalha#gmail.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->AddReplyTo("hussaintalha#gmail.com","First Last");
$mail->From = "hussaintalha#gmail.com.com";
$mail->FromName = "First Last";
$mail->Subject = "PHPMailer Test Subject via gmail";
//$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("hussaintalha#gmail.com", "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
When I run my file I get This Error:
Warning: fopen(contents.html)
[function.fopen]: failed to open
stream: No such file or directory in
D:\xampplite\htdocs\WebEng\class.phpmailer.php
on line 1870
Warning: fsockopen()
[function.fsockopen]: unable to
connect to ssl://smtp.gmail.com:465
(Unable to find the socket transport
"ssl" - did you forget to enable it
when you configured PHP?) in
D:\xampplite\htdocs\WebEng\class.smtp.php
on line 122 Mailer Error: SMTP Error:
Could not connect to SMTP host.
First of all when you get error messages then that's great! Because in 90% of the cases you find that others have had them too and therefore you'll find plenty of information on the internet about this error message.
So step 1 when getting an error message you don't know yet is always open google and copy paste it there. But, take out any paths or other things which are uniquely connected to your system.
Then about your errors. Especially xampp light doesn't support SSL. Maybe you try an easier sendmail example first. Like a very small one and then increase it step by step.
That's what I always do, when I don't know why something doesn't work. I start with one line and see what it does, then I add another and so forth.
Let's say you start with this and see if it works:
<?php
include("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "hussaintalha#gmail.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->From = "hussaintalha#gmail.com";
$mail->Subject = "PHPMailer Test Subject via gmail";
$mail->Body = "Hi, this is a test";
$mail->AddAddress("hussaintalha#gmail.com", "Hussain");
$mail->send();
?>
oh, and btw. your mail from has a .com too many!
You PHP installation (XAMPP, by the looks of it) does not support SSL. Ensure that the line
extension=php_openssl.dll
is not commented out in your php.ini, restart Apache, and if that still doesn't work try overwriting (or copying) ssleay32.dll and libeay32.dll from your PHP directory into Apache's binary (.exe) directory then restart Apache.
Related
I have been trying to send a mail using php mailer on xampp and i do get this error saying
Message could not be sent. Mailer Error: The following From address failed: xxxx2#gmail.com : Called Mail() without being connected
please, i need help on how to fix this.
Here is my code;
<?php
require( 'class.phpmailer.php' );
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 25;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxxx";
//Sending the actual email
$mail->setFrom('xxxx2#gmail.com', 'Aaron');
$mail->addAddress('xxxx2#gmail.com', 'Aaron'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Calculation form results from ';
$mail->Body = 'testing...';
if(!$mail->send()) {
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
Literally copied from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS
$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');
I tried with Composer and github, but couldn't get the latest version of phpmailer to work with my test php email through xampp. When I ran the program on localhost, it kept crashing out, as the email code was looking for the php mailer class.
So I went back to google, ignored composer, and downloaded a plain ordinary zip of php mailer 5.2.0, and extracted this zip directly into my 'websiteX' testing folder which is located in htdocs in xampp.
In my 'websiteX' folder I have my testmail.php file along with my phpmailer unzipped folder and it contains the class.phpmailer which actually works in my case.
I have spent a week faffing around, but now I have xampp php emails going to my test gmail account perfectly. I use chrome and notepad++ as well.
Funnily enough I had php emails with the php mail() command working too, although Gmail hard bounced 'em, which isnt great. The first thing I did last week was to get Mercury mail (included in xampp) working. I followed this link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows and managed to get xampp communicating with gmail which was great!
Good luck with your coding.
I have been trying to send a mail using php mailer on xampp and i do get this error saying
Message could not be sent. Mailer Error: The following From address failed: xxxx2#gmail.com : Called Mail() without being connected
please, i need help on how to fix this.
Here is my code;
<?php
require( 'class.phpmailer.php' );
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 25;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxxx";
//Sending the actual email
$mail->setFrom('xxxx2#gmail.com', 'Aaron');
$mail->addAddress('xxxx2#gmail.com', 'Aaron'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Calculation form results from ';
$mail->Body = 'testing...';
if(!$mail->send()) {
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
Literally copied from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS
$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');
I tried with Composer and github, but couldn't get the latest version of phpmailer to work with my test php email through xampp. When I ran the program on localhost, it kept crashing out, as the email code was looking for the php mailer class.
So I went back to google, ignored composer, and downloaded a plain ordinary zip of php mailer 5.2.0, and extracted this zip directly into my 'websiteX' testing folder which is located in htdocs in xampp.
In my 'websiteX' folder I have my testmail.php file along with my phpmailer unzipped folder and it contains the class.phpmailer which actually works in my case.
I have spent a week faffing around, but now I have xampp php emails going to my test gmail account perfectly. I use chrome and notepad++ as well.
Funnily enough I had php emails with the php mail() command working too, although Gmail hard bounced 'em, which isnt great. The first thing I did last week was to get Mercury mail (included in xampp) working. I followed this link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows and managed to get xampp communicating with gmail which was great!
Good luck with your coding.
I have a hosting server where i have written a php code which is giving me an error, i cant access php.ini as its a hosting server to change anythng
Error : Fatal error: require(): Failed opening required 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/vhosts/evoting.freevar.com/vote/mailtest.php on line 2
PHP Code:
<?php
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 = 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 = "sample#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('sample#gmail.com', 'Evoting System');
//Set an alternative reply-to address
$mail->addReplyTo('sample#gmail.com', 'Evoting System');
//Set who the message is to be sent to
$mail->addAddress('azaz3#gmail.com', '');
//Set the subject line
$mail->Subject = 'EVoting Otp ';
//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('23bj34');
//Replace the plain text body with one created manually
$mail->AltBody = 'sddsfsd23';
//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!";
}
?>
I read related post on stackoverflow, but didnt had success yet. Thanks in advance!
You could check your current working directory, like this:
echo getcwd();
If you do so, you will see which is your active folder. The next thing you need to do is to check whether there is a PHPMailer-master folder inside your active folder (case-sensitive if you are using Linux). If it does not exist, then you will need to point to the right location instead or to create the folder and copy the file(s) into it. If it exists, check whether you have the necessary privileges and whether there is a file called PHPMailerAutoload.php inside that folder (again, case-sensitive if you are using Linux).
This isn't rocket science - the error message is very clear - you just need to use the correct path to the autoloader (and the rest of the PHPMailer files), wherever that is for your server. No need to edit php.ini.
I am trying to send mail using php. But it gave me error,
" SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
"Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" "
So that I searched alot to find the problem. There I got a solution, that I need to change $mail->IsMail(); from $mail->IsSMTP();
I did it and Mail was sent...
But when I checked my mail,
I got,
"This message may not have been sent by: sender#gmail.com"
Being a developer I understood that An email should not contain such lines or issues.
I want to know, Is it alright if Receiver is showing such line in Email? and if not that What should I do?
I mean what changes I should make in my code.
Here is my php code:
**
date_default_timezone_set('Etc/UTC');
include 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// $mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "sender#gmail.com";
$mail->Password = 'senderPassword';
$mail->setFrom("sender#gmail.com", 'sender name');
$mail->addReplyTo('sender#gmail.com', '');
$mail->addAddress($receiver, '');
$mail->Subject = 'Welcome';
$mail->Body = 'body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send())
{
return "Mailer Error: " . $mail->ErrorInfo;
}
else
{
return array('flag' => "1");
}
**
isMail and isSMTP use two different sending mechanisms. isMail submits messages through the PHP mail() function, which delivers mail to a local mail server via a sendmail binary. This local mail server then tries to deliver the message to its ultimate recipient. It's possible that this local server will accept a message which is later rejected, and that will be too late for your script to know about.
With isMail:
script -> local mail server -> gmail
With isSMTP:
script -> gmail
With isMail you don't need to authenticate (localhost is usually allowed to relay), and the message is sent from your server to gmail. With isSMTP your message is sent from gmail to gmail, and it does require authentication.
When sending directly through gmail, you need to authenticate with gmail, and that has its own set of problems (that will be why your script is not working) covered thoroughly in the PHPMailer docs, examples and questions here on SO.
When sending via your server, you're saying that you are sending from a gmail user, but it's being sent by your server, not by a server listed in gmail's SPF record. This is forgery, which is why you are seeing the "This message may not have been sent by..." message. It would not say that if you sent from an address in your own domain.
The solution is to fix your gmail authentication and send directly through gmail. Base your code on the gmail example provided with PHPMailer, not the old, obsolete code you're using, and read the docs.
Here is the code which is use for mailing purpose. Try setting the SMTPDebug mode 3 and check the output.
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxxxxx';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'xxxxxxxxxxxxxxxxx';
$mail->addAddress(xxxxxxxxxxxxxx);
$mail->addReplyTo('xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx');
$mail->isHTML(true);
$mail->Subject = '';
$mail->Body = "";
$mail->send();
I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "myEmail#gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail#gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail#gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail#me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail#gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail#gmail.com : Called Mail() without being connected
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
Obviously it's worth pointing out that I'm not using the literal "myEmail#gmail.com" but I've substituted my own email out for this post.
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "#gmail.com" from the Username property
- A number of other things I can't remember
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
Thanks
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
try to replace what you have
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$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
Firstly, use these settings for Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
sudo /etc/init.d/networking restart # for ubuntu
First, Google created the "use less secure accounts method" function:
https://myaccount.google.com/security
Then created the another permission:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.