php mailer function not working on aws - php

I have used the PHP mailer function to send a mail. This code has worked on XAMPP and 000webhost.com, but its not working on aws.
<?php
require("\PHPMailer\PHPMailer\class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "****#gmail.com"; // Enter your SMTP username
$mail->Password = "************"; // SMTP password
$webmaster_email = $_POST['email']; //Add reply-to email address
$email= "***#gmail.com"; // Add recipients email address
$name= "Recipient's name"; // Add Your Recipient’s name
$mail->From = $webmaster_email;
$mail->FromName = $_POST['name'];
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,$_POST['name']);
$mail->WordWrap = 500; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "website inquiry - Support";
$mail->Body = nl2br($_POST['message']);
$mail->AltBody = nl2br($_POST['message']); //Plain Text Body
if($mail->Send())
echo 1;
else
echo 0;
?>
Please tell me where I am going wrong.
When I am executing this code on aws..its echoing 0
Thanks in advance!

Try with throwing errors by adding "true" to constructor:
try {
new PHPMailer(true);
...your code here...
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
So perhaps there are some more informations about the error.

Related

How to make "PHPmailer" work in a function

I don't know how to send a mail using PHPmailer in a function.
I want to do something similar to this - when I'll want to send a mail, I'll just call a function (like send::mail()). here is my code:
<?php
require '.\vendor\autoload.php';
//PHPMailer Object
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsSendmail();
//From email address and name
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->setFrom('my.bot.mail#gmail.com', 'Bot name');
$mail->Password = "########"; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
//To address and name
$mail->addAddress("mymail#gmail.com"); //Recipient name is optional
//Address to which recipient will reply
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
$mail->Mailer = "sendmail";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
can someone help me put it into a function? when I tried, the "use" command didn't work.
First, you must determine the data.
$mailHost = '';
$mailSMTPAuth = '';
$mailUsername = '';
$mailPassword = '';
$mailSMTPSecure = '';
$mailPort = '';
$mailSenderAddress = '';
$mailSenderName = '';
Then you can generate a function that contains only the recipient, subject and message, and you can send the mail easily by pull the settings into this function:
function sendMail($sendMailAdress, $sendMailName, $sendMailSubject, $sendMailMessage) {
global $mailHost;
global $mailSMTPAuth;
global $mailUsername;
global $mailPassword;
global $mailSMTPSecure;
global $mailPort;
global $mailSenderAddress;
global $mailSenderName;
require_once('class/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mailHost; // Specify main and backup SMTP servers
$mail->SMTPAuth = $mailSMTPAuth; // Enable SMTP authentication
$mail->Username = $mailUsername; // SMTP username or mail
$mail->Password = $mailPassword; // SMTP password
$mail->SMTPSecure = $mailSMTPSecure; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mailPort; // TCP port to connect to
$mail->CharSet = 'utf-8';
$mail->setFrom($mailSenderAddress, $mailSenderName);
$mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sendMailSubject;
$mail->Body = $sendMailMessage;
if(!$mail->send()) {
return false;
} else {
return true;
}
}
The function will work like this:
sendMail('hello#test.com', 'Test User', 'Subject of test mail', 'And your mail message);

SMTP: CLIENT: 535 5.7.3 Authentication Unsuccessful

I'm trying to send an email every time someone inputs a form on my website but the SMTP isn't working. It keeps on showing me this error. The credentials of the email account are correct. Also, I have already tried SMTPAuth = False; but still the same result.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'TLS';
$mail->SMTPAuth = true;
$mail->Username = 'sender#test.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
//Recipients
$mail->setFrom('test#test.com', ' Services');
$mail->addAddress('recipient#test.com', $name); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Message';
$mail->Body = 'Sent a message, This is the message :';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header('Location: tempage.php');
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
What is going wrong? Thanks for your help.
I'm also struggling 2 days with same issue.
After i find this solution. It's working fine.
Try this.
$mail->Host = 'smtp.office365.com';
change to
$mail->Host = 'smtp.live.com';

How to send eFax fax through PHP?

I am trying to send a fax via the eFax service. As I understand, this should be as easy as sending a simple email to the specific address.
I use PHPMailer for emails. Ordinary emails are sending fine. Even when I send to an efax address $mail->send returns true, that means that the email was sent seccessfully, but the recipient didn't receive the fax.
Any suggestions?
Thank you.
function sendFax($fax, $msg) {
$efax_number = $fax . "#efaxsend.com";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = EMAIL_HOST; // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
//From email address and name
$mail->From = "orders#orderapp.com";
$mail->FromName = "Test";
//To address and name
$mail->addAddress($efax_number); // SEND EMAIL TO USER
//Send HTML or Plain Text email
$mail->isHTML(false);
$mail->Subject = ' New Order Fax ';
$mail->Body = $msg;
$mail->AltBody = "Sample";
$result = $mail->send();
if (!$result)
{
...
}
else
{
...
}
}

php mailer could not send email

I am trying to send an email from localhost with PHP. Here is my code:
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require_once('class.phpmailer.php');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->Username = "TTTTTT#gmail.com"; // SMTP username
$mail->Password = ""; // SMTP password
$mail->Port = 25;
$mail->SMTPSecure = '';
$mail->From = $email;
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
When I run this code, it shows, "message has been sent" but it does not actually send the message. What is my problem?
change this code you have:
$mail->Username = "TTTTTT#gmail.com"; // SMTP username
$mail->Password = ""; // SMTP password
$mail->Port = 25;
$mail->SMTPSecure = '';
to this:
$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
In addition to what Lelio Faieta posted, enable less secure apps in gmail
and to prevent your mail going to your spam
change
$mail->From = $email;
to your email and you can put $email in body of your message

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