I have a form in my web site. When user enters data it should reach to my gmail id. I tried many things but it is not working.
http://pastebin.com/xABBduyB
This is the settings in php.ini
SMTP = smtp.gmail.com
sendmail_from = myname#gmail.com
SMTP_PORT = 465
Any help would be appreciated.
PHP's mail function works only with standard SMTP, but to access the Gmail server, you need to use SMTP over SSL. You can find an example of how to do this over here.
The simplest solution for you could be to use phpmailer library
This is the sample googled code to do that:
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
Related
Please tell me how to make the phpmailer module work for siteground hosting?
I noticed that if I comment or remove $mail->isSMTP(); this line, sending mail starts working. But this breaks the logic of my code, because I cannot display success message
Also works if i replace $mail->isMail(); instead of $mail->isSMTP();
Does this mean that SMTP is blocked by the hosting?
My PHPMailer SMTP Config is:
<?php
MAIL_FROM = 'MAIL_ACC_CREATED_ON_SITEGROUND.com',
MAIL_SMTP_HOST = 'mail.mydomain.com',
MAIL_SMTP_PORT = 465;
MAIL_SMTP_USERNAME = 'MAIL_ACC_CREATED_ON_SITEGROUND.com',
MAIL_SMTP_PASSWORD = 'password';
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = MAIL_SMTP_HOST;
$mail->Port = MAIL_SMTP_PORT;
$mail->Username = MAIL_SMTP_USERNAME;
$mail->Password = MAIL_SMTP_PASSWORD;
$mail->SMTPSecure = 'ssl';
I would be grateful for any tip and help!
I don't know how, but I fixed this problem. The problem was my code, not the hosting.
I'm using PHPMailer in a Simple Script For Send Email's Through Gmail, and I'm getting an this error (I'm sure that the email and password combination is correct):
!-- 2020-12-02 14:13:16 CLIENT -> SERVER: EHLO localhost
2020-12-02 14:13:16 CLIENT -> SERVER: STARTTLS
2020-12-02 14:13:17 CLIENT -> SERVER: EHLO localhost
SMTP Error: Could not authenticate.
2020-12-02 14:13:17 CLIENT -> SERVER: QUIT
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Allow less secure apps is ON
This is the way I implement the phpMailer
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require './mailer/autoload.php';
$msg = "";
$mail = new PHPMailer();
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
$mail->isSMTP();
$mail->Host = 'smtp.google.com';
$mail->SMTPAuth = true;
$mail->Username = '********#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = "tls";
$mail->Port = 25;
$mail->CharSet= 'UTF-8';
$mail->setFrom('*******#gmail.com', 'Mailer');
$mail->addAddress($_POST["mail"]);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = '<h2>E-mail</h2>';
$mail->AltBody = $_POST["content"];
$mail->send();
} catch (Exception $e) {
$msg = "An Error has Ocurr";
}
How can I solve this issue?
SMTP port 25 is not used with TLS, you should use port 587 for TLS/STARTTLS or 456 for SSL. And it seems that you've also used the incorrect host URL, which should be smtp.google.com. The required configuration is stated here: https://support.google.com/mail/answer/7126229.
So you should probably change:
...
$mail->Host = 'smtp.google.com';
$mail->Port = 25;
...
To:
...
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
...
Depending on your situation, it might not be best practice to use Google's default SMTP. It is OK for personal use, but if you want to send more automated emails, you should look for other options. The default Google SMTP is strictly rate limited for example.
First of all, Google SMTP Relay yields a little more configurability if you need it.
When you really want to send automated or bulk emails, you should look into a provider specifically for this. It is not what the Google SMTP servers are made for and you will quickly notice by emails not being sent out or delivered properly.
It would really help if you actually read the error message and took the advice it gives you, by reading the guide it links to.
First of all, you're only showing client debug output, so you can't see what the server is saying, and so you can't tell what's going on, as the docs say. Do this:
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
Without seeing what that says, you're working blind.
That said, you get kicked out immediately after EHLO, and the only thing you have said is:
2020-12-02 14:13:17 CLIENT -> SERVER: EHLO localhost
Unfortunately this is untrue, and I'd guess that gmail is calling you out on it. localhost is by definition not an internet routable address, and any reverse lookup on the name will never match the IP you are connecting from, which is not localhost. If that is happening automatically, override it manually by setting the client host explicitly:
$mail->Helo = 'myhost.example.com';
While RFCs mandate port 587 for SMTP+STARTTLS, gmail supports it on port 25 too, and you can see that your STARTTLS command is working successfully, so that's not the problem here.
use port 587
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = '********#gmail.com';
$mail->Password = '********';
I have hosted my website on Plesk Hosting and was working on submitting the contact form.
Installed PHP Mailer using composer.
First
I tried to send email using Gmail SMPT server
it worked fine
Second
I tried to send email using my webhosting SMTP Server
it is not working for me
$mail->Host = 'webmail.abc.in'; //host
$mail->SMTPAuth = false;
$mail->Username = '******#abc.in';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
I tested the SMPT server using SMTPER . it could send email using same credentials.
i dont know where the issue is..
is there any other library other then phpmailer??
This is a example of of code I'm using with gmail. Tested it with webhosting SMTP and worked as well.
` $mailMsg = ADD_MAIL_MESSAGE_HERE;
$mailto = ADD_TO_ADDRESS_HERE;
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSmtp();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Username = ADD_USERNAME_HERE;
$mail->Password = ADD_PASSWORD_HERE;
$mail->SetFrom(ADD_FROM_ADDRESS_HERE);
//-------------------------------------------
$mail->Subject = ADD_MAIL_SUBJECT_HERE;
$mail->Body = $mailMsg;
$mail->AddAddress($mailto);
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
`
$mail->SMTPAuth = true;
As simple as that, I think.
You said you have tested the credentials on SMTPer,
I'm sure you checked the "Use authentication" checkbox.
Maybe you thought you could make it false because you are not using SSL,
But this is about user authentication, not about encrypted communication.
Are you using a Shared Hosting with Plesk? If yes, then this can probably be a port block issue (exact reason you will get only in the maillog). Looking at your code, I can see that in case of your local SMTP testing you are using port 25 while in case of Gmail it's 465.
By default, most of the shared hosting providers block the outgoing SMTP connection on port 25. This is done in order to protect the network and infrastructure from Spamming. If this is the case, then you need to contact their support to unblock the port or use some port free mode of email sending. Means instead of connecting over SMTP, connect over HTTP API for sending emails.
I currently have a website which uses PHPmailer to send emails. I am hosting it with 1&1.fr, but cannot find the information in order to actually send emails. Here is the following information that I need:
$mail->SMTPDebug = 2; // 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
On the 1&1.fr website, they give out the following information:
In the image, they specify multiple ports as well as an entrance/exit server; which ones am I supposed to pick and enter into my PHP file.
The rest of my code works fine (it works when I use my gmail account using 000webhost).
Any help would be greatly appreciated.
// define the $mail // just in case you miss it as it is missing in your code.
$mail = new PHPMailer();
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2; // this is to enable debug if there are errors
$mail->isSMTP(); //Tell PHPMailer to use SMTP
//Set the hostname of the mail server
$mail->Host = 'auth.smtp.1and1.fr';
// Enable authentication so you must provide username and password for SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Here you are telling to use a secure connection with TLS/SSL
//Set the SMTP port number
$mail->Port = 587; // if specified tls. try also 465 as defined in the picture you post
// TCP port for secure connections. 465 is the secure port for outgoing
// emails and 993 is for incoming email using IMAP. If you use POP3 the
//incoming emails are received on 995 port number.
Hope now is more clear.
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.