phpgmailer set from mail issue - php

I'm trying to use php mail class as shown in the example.
http://www.vulgarisoip.com/category/phpgmailer/
I'm using it my site's contact us form. Can I set the "$mail->From" address as the person who filled the form? When i reveive the mail it always shows that the "from address" as my gmail account. Any help would really helpful.
<?php
require_once('phpgmailer/class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'username#gmail.com';
$mail->Password = 'gmailpassword';
$mail->From = 'from#hotmail.com'; // Like to set this address as the address of the person who filled the form
$mail->FromName = 'User Name';
$mail->Subject = 'Subject';
$mail->AddAddress('myname#mydomain.com'); // To which address the mail to be delivered
$mail->Body = 'Hey buddy, heres an email!';
$mail->Send();
?>

$mail->From = $_POST['from'];
or what ever the field is named

Gmail uses the authenticated account as the sender to cut down on address spoofing and spam. No matter what you set as the from address, Gmail will auto set it to your gmail user.
UPDATE
Your comment:
"My intention was actually when I received a mail with the actual
sender as "from address", so that I can directly reply to him."
You may want to try:
$mail->AddReplyTo($email, $display_name);

Related

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.

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.

PHP mailer change sender address using yahoo.com

I am trying to change the sender address of the mail. Firstly I tried by gmail, then I let to know that by gmail its not possible now I am trying using Yahoo, and I got following error:-
SMTP ERROR: MAIL FROM command failed: 553 From address not verified.
Getting blank here I dont understand what do I do? Any help will be appreciated. following is my code:-
<?php
//date_default_timezone_set('Etc/UTC');
require('PHPMailerAutoload.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = "plus.smtp.mail.yahoo.com";
//$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'myyahooid#yahoo.com';
$mail->Password = 'xyz';
$name='Test Name';
$mail->Sender='thishavetoset#domain.com';
//$mail->From = 'sender#senderdomain.com';
//$mail->FromName = 'Sender Name';
$mail->SetFrom('thishavetoset#domain.com', $name, TRUE);
$mail->AddReplyTo('no-reply#mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('myyahooid#yahoo.com', 'title1');
//$mail->AddAddress('abc2#yahoo.com', 'title2'); /* ... */
$fileName='../rough/test.pdf';
$mail->AddAttachment($fileName);
//$mail->send();
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Let me know another solution to set the sender name also if you think, it can't be done this way. Thanks a ton in advance.
The sender email you using should be real email id and as your using Yahoo mail i think then it must registered in yahoo
There are two reasons smtp trows such error are.
first is You are attempting to send email to a domain that is not recognized by this server
second is You are attempting to relay email through this server and have not authenticated
Check if $mail->Sender='thishavetoset#domain.com'; email is set for your domain name your using.
So try to use email which user email password your using.
$mail->Username = 'myyahooid#yahoo.com';
In Gmail you can add that email id as external address from accounts and import tab >> add another email address, You will need details of smtp from the Admin of hosting/domain and add the email id.
once you add the external email address you can use that email id as sender from gmail smtp.

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';

PHPMailer $mail->From headers not working with gmail

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

Categories