PHPMailer send the messages with my server email - php

as written on the subject, PHPMailer send the messages with my server email, not the email of the actual sender.
for example:
from: sendername myservername#server.com
the sender email doesn't show up on the email from section
here is my code
$result = mysql_query($insert_query, $connection) or die(mysql_error());
if($result)
{
require_once('../se482/class.phpmailer.php');
//$mail->SMTPDebug = 3; // Enable verbose debug output
//$mail->IsSMTP(true);
$mail = new PHPMailer; // 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 = 'mypassowrd'; // SMTP password
$mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';
$mail->addAddress('myemail#gmail.com'); // Add a recipient
$mail->setFrom($email, $name);
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->isHTML(true); // Set email format to HTML
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
$mail->SmtpClose();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}

You have to include class.smtp.php as well to use smtp. Also uncomment //$mail->isSMTP(true);
As mentioned in documentation to avoid this you can include only autoloader class that is PHPMailerAutoload.php
updated code
$result = mysql_query($insert_query, $connection) or die(mysql_error());
if($result)
{
//require_once('../se482/class.phpmailer.php');
// require_once('../se482/class.smtp.php');
//or
require_once('../se482/PHPMailerAutoload.php');
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(true);
$mail = new PHPMailer; // 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 = 'mypassowrd'; // SMTP password
$mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';
$mail->addAddress('myemail#gmail.com'); // Add a recipient
$mail->setFrom($email, $name);
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->isHTML(true); // Set email format to HTML
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
$mail->SmtpClose();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Update
You have also add your app credential in get_auth_token.php as mentioned in documentation for use gmail. Here is link https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

$mail->from is set to $email which isn't defined anywhere.

Related

SMTP Error, I don't know why

I really hope someone will help me with this. I don't know what would be the cause of my problem. But there is an error saying,
SMTP Error: Could not connect to SMTP host. Mail Error - >SMTP Error:
Could not connect to SMTP host.
Thank you in advance, I appreciate any help.
<?php
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "kurokonobasket189#gmail.com";
$mail->Password = "vanishingdrive";
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = "465";
$mail->setFrom('kurokonobasket189#gmail.com', 'Francis');
$mail->AddAddress('astigvargas189#yahoo.com', 'Arnold');
$mail->Subject = 'using PHPMailer';
$mail->IsHTML(true);
$mail->Body = 'Hi there ,
<br />
this mail was sent using PHPMailer...
<br />
cheers... :)';
if($mail->Send())
{
echo "Message was Successfully Send :)";
}
else
{
echo "Mail Error - >".$mail->ErrorInfo;
}
?>
download latest version from this
and use this ;
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'test#gmail.com'; // your domain username
$mail->Password = 'pass'; // your address password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('test#gmail.com', 'Mailer'); //your domain addres
$mail->addAddress('bilisim_0#outlook.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('test#gmail.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
be sure you opened your secure setting ;
https://myaccount.google.com/lesssecureapps?pli=1
also dont open this part $mail->SMTPSecure = 'tls'; when you open the setting..

in php mailer the smtp connection is failed

I want to send a a mail from local-host to a mail-id. i am using php-Mailer. but its saying that the SMTP connection is failed. can anyone help me please? MY CODE IS BELOW:
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$body='hai';
$address='stin#f12technologies.com';
$name='hey';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "localhost";
$mail->Port = 25;
$mail->Username = "########-####-####-#####-############";
$mail->Password = "########-####-####-#####-############";
$mail->SetFrom('stinjohnece#gmail.com','Web App');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML($body);
$mail->AddAddress($address, $name);
if($mail->Send()) {
echo "Message sent!";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
you have to give smtp details
after that bounce(restart) your apache once.
<?php
require dirname(__FILE__) .'/library/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
//$mail->SMTPDebug = 2; // error mode
//$mail->SMTPDebug = 3; // error mode
$mail->isSMTP();
$mail->Host = 'mail.xxx.com';
$mail->SMTPAuth = true;
$mail->Username = 'admin#XXXX.com';
$mail->Password = 'XXXXX';
//$mail->SMTPSecure = 'None';
$mail->Port = 25;
$mail->setFrom('admin#XXXX.com', 'XXXXXX');
//for sending mail
$mail->addAddress($username); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'HAI';
$mail->Body = '<br> <br>
<html><body> <div><div><u><h3>HAI </h3></u></div><div><p>This email has been sent for testing</p><p>xxx<b>xx</b></p><p>xx<b>xx</b></p></div> </body></html>';
$mail->AltBody = 'Unable to display the mail';
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent';
}
?>
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth=true;
//$mail->SMTPDebug=2;
$mail->Host='smtp.gmail.com'; // SMTP server
$mail->Username ='...#gmail.com';
$mail->Password ='...';
$mail->SMTPSecure='ssl';
$mail->Port=465;
$mail->isHTML(true);
PHP mailer docs example
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

Mailer Error SMTP connect() failed

I need help i keep getting this error,
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Here's my code
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'mygmail#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('mymail#gmail.com', 'Mailer');
$mail->addAddress('sendtomail#gmail.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
Your code is correct except few things. I edited your code and the message get sent. Also check whether your username and password is correct or not as I tried with my username and password.And also check did you include the class.phpmailer.php.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('mymail#gmail.com', 'Mailer');
$mail->addAddress('sendtomail#gmail.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

How to rectify SMTP connection failure error?

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.comcast.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mass.mari06#gmail.com'; // SMTP username
$mail->Password = '8489328117'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->Port = $SmtpPort;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('mass.mari06#gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('deeparavisankar93#gmail.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
****ERROR:**** Message could not be sent.Mailer Error: SMTP connect() failed.
So what can I do? Please help me.. this code I downloaded from GitHub.
SMTP is mail sending server.
You should use the one you are trying to send mail "from" is assigned. I assumed that you try to send from mass.mari06#gmail.com so configuration should look like following:
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mass.mari06#gmail.com';

PHPmailer works on some host and not onthers

I'm trying to use phpmailer to send mail with my gmail account. If works fine on my local computer using wamp and one remote server I treid. But fails on two other servers. I get this error.
Message could not be sent.Mailer Error: SMTP connect() failed.
This is the code I'm using
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxx#gmail.com'; // SMTP username
$mail->Password = 'xxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('dstein-phins#hotmail.com', 'Josh Adams'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
Thanks for any help
$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 = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

Categories