PHPMailer wrong from address when receiving email - php

I have this problem where when I receive an email sent from my localhost website, I get the wrong email address for the for the sender section ($mail->SetFrom($email, $name)). I get my own email address as the sender and not the one inputed in the text box on my website.
I've looked everywhere for some answers, sadly nothing worked. I've tried going on Chrome Account Settings and setting the less secure apps to ON. That didn't work.
I've tried multiple ways of setting the SetFrom email and name. NEED HELP!
<?php
$dir = __DIR__;
require_once("$dir/../PHPMailer-master/PHPMailerAutoload.php");
extract($_POST, EXTR_PREFIX_ALL, "P");
$name = $_POST['postName'];
$email = $_POST['postEmail'];
$subject = $_POST['postSubject'];
$message = $_POST['postMessage'];
$file = $_POST['postFile'];
echo "Name: ".$_POST['postName'];
echo "\n";
echo "Email: ".$_POST['postEmail'];
echo "\n";
echo "Subject: ".$_POST['postSubject'];
echo "\n";
echo "Message: ".$_POST['postMessage'];
echo "\n";
echo "File: ".$_POST['postFile'];
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.gmail.com"; // SMTP server
//$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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 = "xxxx#gmail.com"; // GMAIL username
$mail->Password = "xxxx"; // GMAIL password
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email, $name);
$mail->addAddress("xxxx#gmail.com", "name");
$mail->AddAttachment("$file");
$mail->Subject = "$subject";
$mail->Body = "$message";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
} ?>
This is an alert I set up showing all the POST parameters sent to the my PHP script. All the POST variables are there. The only problem is the SetFrom($email, $name)
Javascript Alert with POST Parameters

Gmail does not allow you to set arbitrary from addresses, though you can define preset aliases.
However, you shouldn't be trying to do this anyway. Putting user-provided value in the from address is a very bad idea as your messages will fail SPF checks because it's forgery. Put your own address in the from address (as well as the to address), and put the submitter's address in a reply-to header using the addReplyTo() method. You can see this working in the contact form example provided with PHPMailer:
//Use a fixed address in your own domain as the from address
//**DO NOT** use the submitter's address here as it will be forgery
//and will cause your messages to fail SPF checks
$mail->setFrom('from#example.com', 'First Last');
//Send the message to yourself, or whoever should receive contact for submissions
$mail->addAddress('whoto#example.com', 'John Doe');
//Put the submitter's address in a reply-to header
//This will fail if the address provided is invalid,
//in which case we should ignore the whole request
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
...

Related

Is sending email from yahoo's email address possible using gmails's smtp in phpmailer

<?php
require ("PHPMailer-master/PHPMailerAutoload.php");
$mail = new PHPMailer;
if(isset($_POST['submit']))
{
$email_query = "select email1,email2 from tbl_contacts where id = '1'";
$query_result = mysql_query($email_query);
$value = mysql_fetch_array($query_result);
$primary_email = $value['email1'];
$CC = $value['email2'];
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail->IsSendmail(); // Set mailer to use SMTP
$mail->Host = "smtp.google.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email; // SMTP username
//$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email, $name);
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress($primary_email); // Name is optional
$mail->addReplyTo($email, '');
$mail->addCC($CC);
//$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 = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
redirect('/contact.php?message=Your+Message+Has+Been+Sent!', 'location');
}
}
?>
I am using gmail's smtp and it works fine if I send an email with a gmail account but when I send an email with an yahoo account it displays a success message but the message does not get delivered. Is it possible to send email from yahoo's email using gmail' smtp? If yes, how can I do it? And if not, how can I solve the problem?
In general,
avoid declaring incorrect identity.
This might be possible from technical aspect; however in 2016 most email systems tend to protect themself by using multip. techniques - like SPF, DKIM and DMARC and that is both for sending and evaluating received emails.
If this is true for more and more email systems and ESP's it's not hard to imagine what ESP giants like Yahoo, GMAIL and others are doing to express their hate on incorrectly declared identities (mark as spam, reject at SMTP handshake level, silently discard...).
In your case displaying success message only mean that your mail is received on MTA side for further processing, not anything about it's future.

Mailgun with PHP

I've been trying to implement a contact service using Mailgun coded in PHP. I receive the following error:
Could not connect to SMTP host
Below is my code:
<?php
require("../includes/config.php");
require("../mailgun-php/vendor/autoload.php");
require("../phpmailer/_lib/class.phpmailer.php");
use Mailgun\Mailgun;
if($_SERVER["REQUEST_METHOD"] === "GET")
{
render("contact-form.php", ["title" => "Contact us"]);
}
if($_SERVER["REQUEST_METHOD"] === "POST")
{
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailgun.org'; // Specify mailgun SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'postmaster#sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // SMTP username from https://mailgun.com/cp/domains
$mail->Password = '*SMTP password from sandbox domain'; // SMTP password from https://mailgun.com/cp/domains
$mail->SMTPSecure = 'sll'; // Enable encryption, 'ssl'
$mail->Port= '465';
$mail->From = 'sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // The FROM field, the address sending the email
$mail->FromName = 'Enquiry bot'; // The NAME field which will be displayed on arrival by the email client
$mail->addAddress('****#gmail.com'); // Recipient's email address and optionally a name to identify him
$mail->isHTML(true); // Set email to be sent as HTML, if you are planning on sending plain text email just set it to false
// The following is self explanatory
$mail->Subject = 'Client enquiry';
$mail->Body = $_POST["message"];
if(!$mail->send())
{
echo "Message hasn't been sent.";
echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
}
else
{
redirect("confirmation.html");
}
}
?>
Note that this is the controller (I'm using MCV).
This is part of a website I've been implementing as a project for a CS class. Therefore I would avoid buying a private domain.
UPDATE: I've modified the original post with how this piece of code should be. May anyone else encounter this issue, this snippet is working without any issue.
I've contacted the Mailgun staff which provided me with excellent support. The problem lies with how I specify the port. It should be 465 for SSL and 587 for TLS.

How can I add a PHP script to Auto-respond with a "thank you" message

I'm php newbie that just figured out how to use php with phpmailer to send email addresses of my users to my email address to be added to a newsletter.
However, now I want to add a simple auto-respond script in php, so when users add their email to my guestlist it sends them an autoreply email to their email that says:
Thanks for signing up. [Picture of my logo] www.mysite.com
I've searched and searched, but I haven't been able to find a proper answer on how to create an autorespond script in php. Please let me know how I can accomplish this task. Thank you!
<?php
$email = $_REQUEST['email'] ;
require("C:/inetpub/mysite.com/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
$mail->Host = "smtp.comcast.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myusername#comcast.net"; // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("myemail#gmail.com", "Guestlist");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "A new member wishes to be added";
$message = $_REQUEST['message'] ;
$mail->Body = $email;
$mail->AltBody = $email;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
$mail2 = new PHPMailer();
// set mailer to use SMTP
$mail2->IsSMTP();
$mail2->Host = "smtp.comcast.net"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = "myusername#comcast.net"; // SMTP username
$mail2->Password = "*******"; // SMTP password
$mail2->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail2->Port = 25;
$mail2->From = $email;
// below we want to set the email address we will be sending our email to.
$mail2->AddAddress("$email");
// set word wrap to 50 characters
$mail2->WordWrap = 50;
// set email format to HTML
$mail2->IsHTML(true);
$mail2->Subject = "Thank you for joining";
$message = "Please stay tune for updates" ;
$message = $_REQUEST['message'] ;
$mail2->Body = $message;
$mail2->AltBody = $message;
if(!$mail2->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail2->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Update: 1
Ok, I figured out how to send auto-respond emails to users. However, now the users are receiving messages with their own email address and the name Root user
So what can I do to fix this problem so that users see my email address when they recevie auto-responses, and how can I make sure it says my name instead of root user?
Do not use the submitter's email address as the from address - it won't work as it looks like a forgery and will fail SPF checks. Put your own address as the From address, and add the submitter's address as a reply-to.
Using SMTPSecure = 'ssl' with Port = 25 is an extremely unusual combination, and very likely to be wrong. ssl/465 and tls/587 are more usual.
To send multiple messages, you do not need to create a second instance - just re-use the same one. You can reset any individual properties you want (such as Body) and clear addresses using $mail->clearAddresses(), then just call $mail->send() a second time.
It looks like you have based your code on an old example (try a new one) - make sure you are using latest PHPMailer - at least 5.2.10.
add:
echo "Message has been sent";
$mail = new PHPMailer();
$mail->From = 'myemail#gmail.com';
// below we want to set the email address we will be sending our email to.
$mail->AddAddress($email);
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "Thanks for joining ...";
$message = "Thanks for joining...";
$mail->Body = $email;
$mail->AltBody = $email;
$mail->Send();

PHPMailer with domain-based Gmail account

I've been using PHPMailer for years and have been pretty happy with it, but now I'm trying to get a contact form working with an email address that is domain based but tied to Gmail via Google Apps. I.e. the MX record points to Google. I've added PHPMailer's Gmail example trying to get the setup to work, but at this point, I'm not connecting; I get this message:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
Here's my form processor code:
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
// CUSTOM: collect data from our web form
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');
require 'mailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "myemailaddress.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
//$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('myemailaddress', 'my name');
//Set the subject line
$mail->subject = $subject;
$mail->Body = "Name : $name\n\n"
. "Email : $email\n"
. "Telephone : $tel\n"
. "Message :\n\n $message\n"
. "";
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
To answer some questions in advance: yes, SSL rather than TLS is enabled in Gmail, and the MX record is changed at the host. I also did comment out some stuff, but as far as I can tell nothing that I need to get the form to actually send.
Beyond that, I don't even qualify as a hack with PHP; my primary skills are design, HTML and CSS, so I'm probably missing something obvious....
Thanks in advance.

i am submitting my form with php but it is going to spam

**> the mail posted with the below given is going to spam. I am not using
captcha in the form as i don want to. So can anybody help me to the
mail in Inbox**
<?php
if(isset($_POST['submit']))
{
$name = $_POST['Name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$date = $_POST['checkinDate'];
$package = $_POST['package'];
$person = $_POST['adults'];
$kids = $_POST['kids'];
$ip=$_SERVER['REMOTE_ADDR']; //trace the ip address of the user submited
$subject ="Query From :Kerala-Honeymoon-Packages - Promotion\n"; //subject of the email
$to="paul#roverholidays.com";
$cc="online#roverholidays.com";
$ccc="deepti#roverholidays.com";
$from=$_POST['email'];
$adc="Name :$name\n";
$adc.="Email :$email\n";
$adc.="Phone :$phone\n";
$adc.="Date of Travel :$date\n";
$adc.="Package :$package\n";
$adc.="Adults :$person\n";
$adc.="Kids :$kids\n";
$message ="$name copy of the query you submited in Kerala-Honeymoon-Packages";//message header to user submited
$headers="From: <".$from. ">" ;
mail($cc,$subject,$adc,$headers);
mail($ccc,$subject,$adc,$headers);
mail($email,$message,$adc);
header("Location: thanks.htm");
}
else
{
return false;
}
?>
coding-wise I don't think there is anything you can do because it is the email server that classifies your email as a spam not the way you coded your script. All you can do is to control it from the receiver email setting i.e you setup your gmail filters to detect that email based on keyword like "Kerala-Honeymoon-Packages" and move it out of spam.
I don't know for sure what the email servers algorithms are for marking email as spam. However, I think sending email from different domain rather than your domain name is likely to be detected as phishing email. what I mean is when someone put his/her yahoo email in the form and click on send, your server will send the email to emails addresses in the script but it will send it as if it came from yahoo, which will be suspicious for the receiver email server as it knows that it did not come from yahoo.
Many email services block mail sent directly from random servers because they have little to no reputation as a legitimate source of non-spam emails. Instead of using the straight php mail() function, try using a SMTP service like Mandrill or Gmail's SMTP service. Both are free.
Here is the configuration page for Mandrill:
http://help.mandrill.com/entries/23737696-How-do-I-send-with-PHPMailer-
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MANDRILL_USERNAME'; // SMTP username
$mail->Password = 'MANDRILL_APIKEY'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Your From name';
$mail->AddAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->AddAddress('ellen#example.com'); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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';

Categories