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.
Related
I am using the following code to send emails using phpmailer 6:
$mail->isSMTP();
$mail->Host = 'myhost';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'email#mydomain.com';
$mail->Password = 'password';
$mail->Port = '587';
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$mail->setFrom('email#mydomain.com');
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress($to);
$mail->Subject = $_POST['subject'];
$mail->Body = "...";
$mail->send();
The email sends successfully to our company, however it always says it is from email#mydomain.com, which makes it hard for staff to find specific emails in their inbox. In the form, the user provides their name and email so ideally I'd like it to appear in Outlook as being sent from John Doe (jdoe#gmail.com) rather than email#mydomain.com.
In old versions of webforms I use to do this by setting the from address however now that doesn't seem to be possible because it's considered spoofing.
Is there a way to accomplish this?
Simply add in the name as the second parameter in your setFrom. See the example below:
$mail->setFrom('darth#empire.com', 'Darth Vader');
It'll then appear as you want it!
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.
**> 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';
I have a problem with Replying mails with SMTP through PHPMailer. When I try to send the mail I get
"You must provide at least one recipient email address."
The following PHP Code I use is:
require("smtp/class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = '****#gmail.com';
$mail->Password = '***';
$mail->SetFrom('***#gmail.com', '***#gmail.com');
$mail->Subject = 'RE: Hello World';
$mail->Body = 'Hello World';
$mail->AddReplyTo('****#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
I would like to know what else I'm lacking in the configuration.
You're missing a To address. You can add one like so:
$mail->AddAddress('josh#example.net', 'Josh Adams');
See a full example here: https://github.com/PHPMailer/PHPMailer#a-simple-example
The Reply-To header designates the default/recommended address to use when the recipient clicks "reply."
AddReplyTo is used to add a Reply-To address. Responses to messages you send with a reply-to address are delivered to that address.
Say, you send an email to one of your visitors with the reply-to address set to support#example.com. When they reply to that email, it will be sent to the email you specified as AddReplyTo.
If you're trying to send an email to yourself, you can just use AddAddress instead.
$mail->AddAddress('someone#example.net', 'JohnDoe');
Hope this helps!
Documentation: phpMailer methods, phpMailer examples.
Use $mail->AddAddress() instead of $mail->AddReplyTo().
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