I am working on a project and in my registration form when the user registers I want to E-mail them an activation link. the registration form works, but I am having issues with the E-Mail server. This is my php code using Phpmailer that I have but it is not working and I do not know why !
require 'include/class.phpmailer.php';
require 'include/PHPMailerAutoload.php';
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 = jalilmotaz;
$mail->Password = *****;
$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;
}
Any sugesstions on how to work this out by gmail or some other way?
Multiple mistakes in your code.
1). SSL is deprecated. Use tls.
2). Put your full gmail email and password, and within double quoted ".
<?php
require 'include/class.phpmailer.php';
require 'include/PHPMailerAutoload.php';
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$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->Username = "youremail#gmail.com";
$mail->Password = "passwordhere";
$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;
}
?>
Related
I'm using PHPMailer and am unable to send emails when configuring it to use my SendGrip credentials. Here is my sendEmail function:
public function sendEmail($to, $subject, $message)
{
$mail = new \PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.sendgrid.net';
$mail->SMTPAuth = true;
$mail->Username = 'apikey';
$mail->Password = 'removed';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('removed#email.com');
$mail->addAddress($to);
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->send()) {
throw new MailerException();
}
}
Is there anything obviously wrong with this function?
Thanks,
I am trying to send SMTP secured mail with attachment using PHPMailer.
I made this function with PHPMailer library
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tester#mydomain.com';
$mail->Password = '**************';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Now if I comment the line $mail->isSMTP(); it's working fine . but I think it's not SMTP secured. Else I am getting this message: "Mail error: SMTP connect() failed. "
I searched for this problem but didn't get the proper answer to what I am looking for. Please help me.
I am working in a dev server with HTACCESS protection
Well I have found the details to fix the issue here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting .
I have just Turned on less secure apps https://www.google.com/settings/security/lesssecureapps from my gmail account.
This takes few minutes or an hour to active.
My current code :
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
//$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'mymail#gmail.com';
$mail->Password = 'mypass';
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Else you have to Setup a app from console.developers.google.com
Follow this guide : https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
I have a form in my website. When the user fills out the form, I want an email to be sent to me with data entered in the form and also a thank you email for the person filling the form.
This is the code I am using:
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress('data#domain.org');
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress(''.$_POST['emailAddr'].''); // **
emailAddr is the name for email field in the form and I wish to send email to this email address.
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>
I am trying to build a mail function with php, but it's justing showing "Invalid address:" after execute. Pleas help.
Below is my code:
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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 = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->From = "myemail#gmail.com";
$mail->FromName = "Web Site";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addAddress('myfriend#gmail.com');
$mail->AddReplyTo('myfriend#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Try This
Please write the below line.
$mail->CharSet = "UTF-8"; // To support special characters in SMTP mail
//$mail->SMTPDebug = 2; Comment this line of code
I hope this will help you. Good Luck
This is my php code to send a confirmation email to a new user and the problem is, i am not receiving it(i have tried different ones, no luck)
function send_email($info)
{
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('noreply#localhost' => 'localhost'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
I get no errors
Install phpmailer.
Here is a sample on how to send mail with PHP mailer
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 your destination address is #hotmail.com it may never income because they have a strong anti-spam management.