I have been trying to send PHP emails via gmail account having non gmail domain but the email address is registered at gmail.com. The emails are not going via this account but when I give an account having #gmail.com, then the email is transmitted but it is not sending to non gmail email addresses. The code I have been using is php mailer code.
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->IsHTML(true);
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Can someone suggest what am I doing wrong.
Try use host:
$host = "ssl://smtp.gmail.com";
You should register the application who sends the mail in your gmail account:
Start session in your browser in the gmail account.
In the same browser with the session opened, go to https://accounts.google.com/b/0/DisplayUnlockCaptcha
Send a mail executing your php in the desired server you want to enable.
Related
hello i would like to send a confirmation email via my website when you create an account. unfortunately I can't get any further, can you help me?
Here is my code:
function send_activation_email($email,$usrnameemail,$code) {
mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "XXX";
$mail->Password = "XXX";
$mail->IsHTML(true);
$mail->AddAddress($email, $usrnameemail);
$mail->SetFrom(mail_from, mail_sender);
$mail->AddReplyTo(mail_from, mail_sender);
$mail->AddCC(mail_from, mail_sender);
$mail->Subject = 'Test Email';
$content = "<b>This is a Test Email sent via Gmail SMTP Server using PHP mailer class. </b><a href='$code'>Click here to activate your account</a>";
$mail->MsgHTML($content);
if(!$mail->Send()) {
echo "Error while sending Email.";
var_dump($mail);
} else {
echo "Email sent successfully";
}
}
Today You cannot use the email password you're using when accessing gmail.com to send emails using SMTP.
You should activate/create App Password. When done you will got a password that you can use for Your PHP Mailer.
its can help You too, if Your App Password has security breach, You can simply delete that App password and recreate new one.
for reference You can access:
https://support.google.com/accounts/answer/185833?hl=en
I'm guessing that this:
<?php
$emailTo = 'user1#gmail.com';
$subject = 'I hope this works!';
$body = 'Blah';
$headers='From: user#gmail.com'
mail($emailTo, $subject, $body, $headers);
?>
is not going to cut it. I searched for ways that I can send to email with SMTP auth and mail clients and programs such as PHPMailer, but I don't have a concise and direct answer yet thats helpful. Basically, I want a way to send emails to gmail, hotmail, etc (which will be my email) from another email (sender) through a form on my website
Questions:
Do I need to download a 3rd party library to this?
If not, how can I change the code above to make it work.
Thanks!
Use PHPMailer library.It is better than use mail native function because in PHPMailer you can use user authentication to avoid to send the email to spam.You can use this library without to configure a mail server. You can download in this link https://github.com/PHPMailer/PHPMailer
See an example:
$mail = new PHPMailer();
$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";
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 587; // SMTP port
$mail->Username = "yourusername#gmail.com"; // username
$mail->Password = "yourpassword"; // password
$mail->SetFrom('user#gmail.com', 'Test');
$mail->Subject = "I hope this works!";
$mail->MsgHTML('Blah');
$address = "test#test.com";
$mail->AddAddress($address, "Test");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Need help!!!
I am using php mailer in my current project. It works successfully, after sending a mail successfully to the other end it says the mail comes from the smtp server account not from the $from what i use here-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
My smtp server code here..
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "muradautorun";
$mail->Password = ""; //my passsword here
Here is my complete code-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
$from_name="S.M. Murad Hasan Tanvir";
$subject="A sending mail code";
$body ="This is a sending email code, the file attach here is about the coding.";
//here i use gmail as a smtp server
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "muradautorun";
$mail->Password = ""; //my passsword here
$mail->SetFrom($from, $from_name);
$mail->addAttachment('emailSend.php'); // Add attachment(just the file url)
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo 'Mail error: '.$mail->ErrorInfo;
}
else {
echo 'Message sent!';
}
?>
Where i doing wrong i can't find out please help me...
Thanks in advance.
You need to allow sending mail as "tanvir_cse0906#yahoo.com" on your "muradautorun" gmail account first.
You can do this by following the following steps:
Login with "muradautorun"
Click the gear in the top right and choose "Settings"
Click on the "Accounts and Import" tab
Click on link "Add another email address you own" in the "Send mail as" section, and follow the instructions.
I want to send a confirmation mail to an user who is trying to register. Iam using PHP Mailer for sending mails. Here is my code:
require_once("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;// turn on SMTP authentication
$mail->Username = "root";// SMTP username
$mail->Password = "";// SMTP password
$mail->SetLanguage("en","PHPMailer/language");
//compose mail
$mail->From = "admin#localhost.com";
$mail->FromName = "Cinema Admin";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo("admin#localhost.com", "Cinema Admin");
$mail->Subject = 'Your confirmation link here';
$mail->Body = "Your confirmation link\r\n";
$mail->Body .= "Click on this link to activate your sccount\r\n";
$mail->Body .= "http://localhost/user.login_plugin/confirm.php?passkey=$confirm_code";
//send mail
if (!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
When I execute the file,it says
Message was not sent
Mailer Error: SMTP connect() failed.
Could anyone suggest me for sending mails from localhost to the registered user's emailid.
I had searched with few titles regarding "ending emails" in stack overflow, but couldn't find any solution from any posts.
Thank You in advance
Could you try remove the smtp part? From your comment , you do not have mail server setup.
$mail = new PHPMailer();
$mail->SetLanguage("en","PHPMailer/language"); // remove smtp things
You must need a SMTP details in order to send an email using SMTP, For example if you want to setup SMTP using gmail,
$mail->IsSMTP(); // telling the class to use SMTP
$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
If you don't want use SMTP, you can called default mail function
$mail->IsMail(); //Sets Mailer to send message using PHP mail() function.
I am trying to use gmail smtp server to send emails via php mailer from a php form. i have opened the 465 port from windows firewall but the port doesn't appear in the cmd output when i type
netstat -an
command.the error that appears in the php page is:
Invalid address: SMTP Error: Could not connect to SMTP host.
there is no exact solution for my issue in the result of my searchs. my code is:
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
define('GUSER', 'from#gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}}
if(smtpmailer('from#gmail.com', 'to#gmail.com', 'name', 'test mail message', 'Hello World!')){
echo "sent";
if (!empty($error)) echo $error;}
?>
note: i am using localhost for my site
Try using port 25 or 587, also check your apache log.