I am trying to add a reply to address to my php mailer and it just puts from "me" and replies to my address.
Any ideas what I am doing wrong? I have added the $mail->AddReplyTo. I want it to reply to the sender of the web form.
$name = $_POST['name'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = file_get_contents('phpmailer/contents.html');
$body = eregi_replace("[\]",'',$body);
$body = eregi_replace("<name>", $name,$body);
$body = eregi_replace("<telephone>", $telephone, $body);
$body = eregi_replace("<email>", $email, $body);
$body = eregi_replace("<message>", $message, $body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "xxx#xxx.net"; // GMAIL username
$mail->Password = "xxxxx";
$mail->AddReplyTo($email, $name);
$address = "xxxx.net";
$mail->AddAddress($address, "Contact form");
$mail->Subject = " Contact Form";
Something to try is to make sure your $email and $name variables are being passed in correctly (add some debugging statements to echo them out). Not sure if you have done that or if you are checking if the form has posted or not. But that would be step one.
From my workings with PHPMailer and GMail, they do not work to well. Instead I would suggest trying the phpGMailer script. It works great for GMail. See if that does not fix your issues.
UPDATE
Thinking about it, I do not think GMail permits the changing of the ReplyTo address unless the GMail account has activated authorization for that account. I am not 100% sure on this, but I know through the web interface that is not possible.
Off Topic
I would avoid using eregi_replace it is depreciated. I would use preg_replace instead. Here is an updated version so you can update your code:
$body = file_get_contents('phpmailer/contents.html');
$body = preg_replace("~[\]~",'',$body);
$body = preg_replace("~<name>~i", $name,$body);
$body = preg_replace("~<telephone>~i", $telephone, $body);
$body = preg_replace("~<email>~i", $email, $body);
$body = preg_replace("~<message>~i", $message, $body);
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?
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.
I'm using Migadu mail server and PHP Mailer to set up a simple contact form.
This is the PHP file:
<?php
require("includes/class.phpmailer.php");
require("includes/class.smtp.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$smtpHost = "smtp.migadu.com";
$smtpUsername = "info#mywebsite.com";
$smtpPassword = "mypassword";
$to = 'info#mywebsite.com';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->Host = $smtpHost;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->From = $smtpUsername;
$mail->FromName = $name;
$mail->AddAddress($to);
$mail->AddReplyTo($email);
$mail->Subject = "Contact Form";
$body = '<h1>Contact!</h1>';
$body .= '<p><b>Name: </b>'.$name.'</p>';
$body .= '<p><b>Email: </b>'.$email.'</p>';
$body .= '<p><b>Message: </b>'.$message.'</p>';
$mail->Body = $body;
$sentStatus = $mail->Send();
if($sentStatus){
echo json_encode(['status' => 'ok']);
}else{
echo json_encode(['status' => 'error', 'errorType' => 'server']);
}
?>
But it's not sending the mail. Also, it's not throwing any error, it just stuck in loading forever.
It's not showing any error because you're not displaying any errors - look in the ErrorInfo property. All of the examples provided with PHPMailer do this, so go look at them to see how to do that.
It's most likely that it's not "stuck forever", it's just that the timeout is long, and that's probably because your ISP blocks outbound SMTP, which is very common. The troubleshooting guide tells you how to diagnose this. Your ISP probably has an alternative method for sending email, for example they may provide their own relay, so you should refer to their docs.
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.
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');
I have a form on my website where people can join events. The code behind it works in this way:
All info is saved in a database. This part works fine.
The second part of the code send out an email to me and to the user with the info he entered (same info as saved in the database)
The issue is that these emails are sent unauthenticated through a default email on the hosting account. I had to modify the script to force SMTP authentication with a valid email under my hosting account to fix the error. Right now the script sends out the email but it ends in spamfilter with all ISPs so the user never receive the email.
I have no idea of how to do, or create the codes so the script use SMTP authentication. Below is the codes I have so fare. Can someone help me?
<?
// SEND OUT EMAIL PART
// COPY SEND TO MY SELF
$to = "my#email.com";
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Thanks!";
$fields = array();
$fields{"name"} = "Name";
$fields{"address"} = "Address";
$fields{"phone"} = "Phone";
$fields{"email"} = "E-mail addesse";
$body = "INFO:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// SEND TO THE USER
$headers2 = "From: my#email.com";
$subject2 = "THANKS!";
$fields2 = array();
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Thanks!";
$body2 = "
TEXT TO EMAIL RECEIVER
\n\n"; foreach ($fields2 as $a => $b){ $body2 .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// ERROR MESSAGES
if($from == '') {print "MISSING EMAIL ADDRESS.";}
else {
if($name == '') {print "MISSING NAME";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $body2, $headers2);
if($send)
{header( "Location: http://mysite/send.php" );}
else
{print "MISSING EMAIL ADDRESS ALL FILDS MUST BE FILLED!"; }
}
}
?>
Best if you use a dedicated script for sending emails. Such as PHPMailer which gives you various config options including SMTP:
// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "email#email.com";
$mail->Password = "password";
$mail->Port = 465; // change port is required
This is one way to do it. You need to include PHPMailer
#somewhere in code before you call function
include($path_to_PHPMailer."class.smtp.php");
function sendEmail($email,$name,$title,$content,$who=false,$att=array('')){
//VARIABLE $email is email of sender
//VARIABLE $name is name of sender
//VARIABLE $title is title of email
//VARIABLE $content is content of email
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
// DOUBLE $mail->Host = "your email_server"; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "your email_server"; // sets the SMTP server
$mail->Port = server port; // set the SMTP port for the GMAIL server
$mail->Username = "your username"; // SMTP account username
$mail->Password = "your password"; // SMTP account password
if($who){
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$address = "set your email";//EMAIL OF RECIPENT
} else{
$mail->SetFrom("set your email", "your name (or service name)");
$mail->AddReplyTo("set your email","your name (or service name)");
$address = $email;
}
$content=str_replace("\n", "<br>", $content);
$mail->Subject = $title;
$mail->MsgHTML($content);
$mail->AddAddress($address, $name);
$mail->CharSet='utf-8';
if($att[0]!=''){
foreach ($att as $at){
$mail->AddAttachment($at);
}
}
if(!$mail->Send()) {
return false; //$mail->ErrorInfo;
} else {
return true;
}
}