PHP Mailer Multiple email - php

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>";
}
}
?>

Related

PHPmailer not sending messages to gmail

I am trying to send messages from my website specialeducationnotes.co.in but not able to send the messages.
<?php
if(isset($_POST['query-submit'])) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
// $mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#gmail.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
$from = $_POST['email'];
$subject = $_POST['subject'];
$mail->From = $from;
$mail->FromName = $_POST['name'];
$mail->addAddress('specialeducationnotes1#gmail.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = '<h2 style="text-align: center;"s>Name: '.$_POST['name'].'<br>Email: '.$_POST['email'].'<br>Message: '.$_POST['query'].'</h2>';
$mail->AltBody = 'Hello! Hola! Namaste!';
if(!$mail->send()) {
//echo 'Message could not be sent.';
echo "<script>alert(' Your message could not be sent!');</script>";
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'Message has been sent';
echo "<script>alert(' Your message is sent!');</script>";
echo "<script>document.location.href='index.php'</script>";
}
}
?>
I just get a blank page with no error message or any text and email is also not sent. Can you tell what's wrong with my code?

SendGrip Emails Won't Send

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,

Getting trouble with SMTP connect with PHPMailer

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

PHPmailer Connect E-mail server with PHP

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;
}
?>

php mailer sender form user

i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.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 = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");

Categories