PHPMailer $mail->From headers not working with gmail - php

I'm using the following code to send a mail after a form submission with the PHP mailer class https://github.com/Synchro/PHPMailer. The mail sends and is received successfully. The only thing that isn't wokring is the following:
$mail->From = $email;
$email is the email that a user will enter on the form (it is set with a $_POST variable). I would like the email to appear that it's from the user who filled out the form, so I can hit reply and have it go to their email address.
However, the "from" email address is being set as $mail->Username, i.e. the username from the gmail account that the PHPMailer script is sending from.
What am I doing wrong here, and how do I get the From email header to work?
Also, I am using Gmail to receive the mail-- maybe there's a gmail security setting that won't allow the "From" email to be "faked"???
Thanks!
$email = $_POST['email'];
$name = $_POST['moveName'];
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('me#gmail.com');
$mail->AddReplyTo($email, $name);
$mail->IsHTML(true);
$mail->Subject = 'Quote Request';
$mail->Body = 'hey';
$mail->Send();

With gmail you have to configure an email address as allowed "$mail->from" first. This FAQ entry explains how to do it: https://support.google.com/mail/answer/22370?hl=en

Related

I'm using the PHPMailer to my website contact page , but I faced a problem , when the message is received both sender and receiver gmail are the same?

I'm using the PHPMailer to my website contact page , but I faced a problem , when the message is received both sender and receiver gmail are the same?
use PHPMailer\PHPMailer\PHPMailer;
if (isset($_POST['name']) && isset($_POST['email'])) {
$name = filter_var( $_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['mail'],FILTER_SANITIZE_STRING);
$subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
$body = filter_var($_POST['body'],FILTER_SANITIZE_STRING);
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
//SMTP Settings
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "#gmail.com"; //enter you email address
$mail->Password = ''; //enter you email password
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
//Email Settings
$mail->isHTML(true);
$mail->setFrom($email,$name);
$mail->addAddress("ennokhba22#gmail.com"); //enter you email address
$mail->Subject = ($subject);
$mail->From= $email;
$mail->Body = $body;
When sending via Gmail's SMTP servers, they ignore any From headers to prevent abuse. Your From will always be that of the Gmail account you're sending via.
You'll need a different email service provider if you want to send custom From headers, but note that this is likely to wind your emails up in spam unless you've got the rights (via SPF etc.) to send email on behalf of that custom address. You might consider a Reply-To header instead.

How to send contact form message to email of smtp server using php mail() function?

There is a form in my webpage, message from the contact form goes to an email of a smtp server. I have used this codes for sending the message:
require_once("PHPMailer-master/PHPMailerAutoload.php");
$fromName = $_POST['username'];
$fromEmail = $_POST['email'];
$theMessage = $_POST['message'];
$theSubject = $_POST['subject'];
$theCompany = $_POST['company'];
$thePhone = $_POST['phone'];
$isSuccess = 0;
$notificationMsg = "";
$mail = new PHPMailer;
// $mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 465;
// Authentication
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
// Compose
$mail->SetFrom($fromEmail, $fromName);
$mail->addReplyTo($fromEmail, $fromName);
// Send To
$mail->addAddress('info#mycompany.com', 'My Company');
$mail->WordWrap = 50;
$mail->isHTML(true);
if ($mail->send()) {
$isSuccess = 1;
$notificationMsg = "Thank you for your message";
} else {
$isSuccess = 0;
$notificationMsg .= "Sorry, there is something wrong. Please, try again letter.";
exit ;
}
echo $notificationMsg;
But, it didn't work. If I add these line for Compose section:
// Compose
$mail->From = 'user#mycompany.com'; // any email address from our own
it will work then! And it shows at our email box:
from: Root User <user#mycompany.com>
reply-to: Sender Name <sender#gmail.com>
to: My Company <info#mycompany.com>
Message body at the email box is okay. But, instead of sender's email address, our email address is showed at form field. Also, Root User is showed instead of sender's name. If I add one more line at compose section:
// Compose
$mail->From = 'user#mycompany.com'; // any email address from our own
$mail->FromName = 'Anything';
It shows then:
from: Anything <user#mycompany.com>
reply-to: Sender Name <sender#gmail.com>
to: My Company <info#mycompany.com>
Even, I tried with this:
// Compose
$mail->From = $fromEmail;
$mail->FromName = $fromName;
But, message won't sent then form my contact form.
So, for compose section,
// Compose
$mail->From = 'user#mycompany.com'; // any email address from our own
$mail->FromName = 'Anything';
$mail->SetFrom($fromEmail, $fromName);
$mail->addReplyTo($fromEmail, $fromName);
3rd line doesn't seem working. But, that line should be working instead of first two lines and at our email box, it should be shown:
from: Sender Name <sender#gmail.com>
reply-to: Sender Name <sender#gmail.com>
to: My Company <info#mycompany.com>
How to solve that problem? Thanks in advance.
Don't try to use the submitter's address as the from address; it's forgery and even if you can get away with sending with it (which it looks like you can't anyway), it will cause your messages to fail SPF checks and be spam-filtered or bounced. Put your own address in the from address and the submitter's address in a reply-to, as the contact form example provided with PHPMailer shows you.
The combination of $mail->SMTPSecure = 'tls'; and $mail->Port = 465; will not work; change Port to 587 or SMTPSecure to ssl.
Read the docs, base your code on the examples provided with PHPMailer, and update to the latest version.

Amazon Simple Email Service SMTP using PHP Mailer

I finally find the solution of my problem, i post new codes that work properly. I have a new issue now, let say that my email has more than one destination, when i try to put many email adresses in $to variable. For exemple $to="abc#gmail.com,pat#gmail.com"; it's not working but with only one it's worked. How can i do it with more than one address ?
$account="username";
$password="password";
$to="abc#gmail.com";
$from="efb#gmail.com";
$from_name="name";
$msg="<strong>test smtp with amazon.</strong>"; // HTML message
$subject="HTML message";
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPAuth= true;
$mail->Port = 465;
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
As u are using gmail u should less secure your app using this link after login to gmail.
link: https://www.google.com/settings/security/lesssecureapps
host should be $mail->Host = "smtp.gmail.com"; for gmail.
Do you completed validate e-mail abc#gmail.com ? First you must go to
> https://console.aws.amazon.com/ses/
Then in Email Addresses checked there is your abc#gmail.com address. If no click on Verify a New Email Address and add one. You'll get test message. After that in Verified Sender: Email row you will see next to your e-mail the green world "verified".
For adding more than one email you can do it in the following way:
$mail->AddAddress("prodip#cottonist.org");
$mail->AddAddress("srasel84#yahoo.com");
$mail->AddAddress("abid#cottonist.org");

Mail getting sent from a different email address

I am facing this problem where the mail is getting sent as a different email rather than the one specified.
$from_name = 'send';
$from = 'send#test.com';
$to = 'receive#test.com';
$to_name = 'receive';
$also_to = 'cc#test.com';
$also_to_name = 'cc';
$message = 'Dear receive Thank you for the booking';
$message .= '<br><br>'.$homepage;
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = 'send1#test.com';
$mail->Password = ******;
$mail->SetFrom($from,$from_name );
$mail->AddReplyTo($from,$from_name );
$mail->Subject = 'Booking Details';
$mail->MsgHTML($message);
$mail->AddAddress($to,$to_name);
$mail->AddCC($also_to,$also_to_name);
When I sent it using this code the mail goes with name as 'send' but not the 'send#test.com' email address it gets sent from "send -> send1#test.com" rather than send -> send#test.com.
Above I have mentioned the smtp details that i have used. Also when i change:
$mail->Username = 'send#test.com';
$mail->Password = ******;
The mail does not go through to any email address. I hope this is clear enough for you guys and I would appreciate any help. Thank you
$mail->Username = send1#test.com;
this need to be in quotes like :
$mail->Username = "send1#test.com";
Check whether this resolves you issue ?
If not try setting debug to true $mail->SMTPDebug = 1;
and see what's being actually sent to gmail server
By default gmail will use the account's email address
Ref : How to change from-address when using gmail smtp server
Look at this: http://phpmailer.worxware.com/index.php?pg=properties
$From public root#localhost Sets the From email address for the message
and
$FromName public Root User Sets the From name of the message
So you want specifically add the following:
$mail->From = $from;
$mail->FromName= $from_name;
You have to call setFrom method to set the sender's mail address and name:
$mail->SetFrom('send#test.com', 'Your Name');

Adding Custom Header using phpmailer

I have a contact form which a user types his/her details including his/her email, the code below works well
$mail = new PHPMailer();
$body = $message;
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Host = "smtp.bizmail.yahoo.com";
$mail->Port = 587;
$mail->Username = "name#domainname.net";
$mail->ContentType ='text/html';
$mail->Password = "password";
$mail->SMTPSecure = 'tls';
$mail->SetFrom('name#domainname.net', 'my name',false);
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->IsSMTP();
$address = $to;
$mail->AddAddress($address, $name);
if(!$mail->Send()) {
return 0;
} else {
return 1;
}
this code sends a mail and a header "From: name#domainname.net", but i want to show the email the user inputs from the contact form. e.g a user inputs myenquiry#anotherdomain.com, i want the from mail to be "From: myenquiry#anotherdomain.com" in which anotherdomain is not in my domain (i.e yahoo small business)
Don't do that. That is forging the from address and it will usually cause your messages to fail to be delivered because they will fail SPF checks. This is especially the case with Yahoo who is by far the pickiest ISP to deliver to. Put your own address in From (as you are doing now), and add the submitter's address in reply-to (see addReplyTo() in PHPMailer).
Also you've based your code on an old example, so make sure you are using an up to date example and the latest PHPMailer.

Categories