How do I AddAddress automatically in PHPmailer from form php info? - email

This has got to be a simple one, but I've been searching all over and nothing seems relevant to my problem: or i'm not able to interpret advice for others adequately.
I'm getting the error "You must provide at least one recipient email address. Message could not be sent."
If I put a specific email address in 'AddAddress' field the message is sent. I want to have an email sent to the user who has submitted their form as an automatic email response thanking user for registering... Thus the ($_POST['email']) in the 'AddAddress' field. This works in Mail() php function. Why not PHP MAiler? What am I missing?
Here is my code with sensitive bits taken out (Relevant bit - I think - in bold):
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(email,) VALUES('$email',)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
}
else {
echo "ERROR";
}
?>
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = "";
$mail->FromName = "";
**$mail->AddAddress = ($_POST['email']);**
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "";
$mail->Body = "";
$mail->AltBody = "";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
<?php
// close connection
mysql_close();
?>
Many thanks in advance for your advice. Hopefully this will help others too...
Ed

Simple one... just change this line
$mail->AddAddress = ($_POST['email']);
to this:
$mail->AddAddress($_POST['email']);
as it is a method, as opposed to a variable, on the class! :)
there are some good examples you can use on the PHPMailer GitHub

Related

phpmailer not handling Iphone subject line in emails

I am using the following script to email our website users:
function mailerExpressBlueHost(array $mailInputs){
require_once '../../includes/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsMail();
$mail->SetFrom('swag#sustainablewestonma.org');
$mail->addAddress($mailInputs['addAddress']); // use for production;
$mail->AddBCC("swag#sustainablewestonma.org"); // set BCC: counts as part of the 500 limit;
$mail->AddEmbeddedImage("../images/newswagimageSmall.jpg", "swag-logo");
$mail->Subject = $mailInputs['subject'] ;
$mail->Body = $mailInputs['body'];
$mail->IsHTML(true);
$mail->ContentType="text/HTML";
if(!$mail->send()) {
$msg = 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo;
}else{
$msg = 'Message has been sent';
}
$mail->ClearAddresses();
return $msg;
}
when viewing the email in ms outlook the email looks like:
but when viewed on an Iphone it looks like:
Is there a way to either hide or place in the header the subject line instead of it appearing in the body? (the subject line being: SWAG Mailing List Confirmation!)
This is just a difference between email clients. There's nothing you can do about it.
Separately, you are using a very old version of PHPMailer (so update it), and you can remove these lines from your script as they're not doing anything:
$mail->IsMail(); //This is the default, so does nothing
$mail->ContentType="text/HTML"; //Calling isHTML does this for you, but correctly
$mail->ClearAddresses(); //This does nothing as the instance is destroyed when it goes out of scope

Not able to get data when form is submitted using phpmailer and later forwared

Simple PHP script to capture user form input and send me email.
Empty form gets submitted using phpmailer script i.e. Receiving email without user filled data. There is no SMTP error, Page get submitted and redirected to new Page. Request to please help. Sometimes email received also form has user filled data also, not sure why ?
<?php
date_default_timezone_set('Etc/UTC');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';
try {
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "XXXXX#gmail.com";
$mail->Password = "XXXXX";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXX#gmail.com', 'Mr Singh');
$mail->addReplyTo('XXXXX#gmail.com', 'Mr Singh');
$mail->addAddress('XXXXX#gmail.com', 'Mr Singh');
$mail->Subject = 'PHPMailer Exceptions test';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['name']}
Email: {$_POST['email']}
Mobile: {$_POST['mobile']}
EOT;
$mail->send();
header("Refresh:1; contact.html");
} catch (Exception $e) {
echo $e->errorMessage();
} catch (\Exception $e) { //The leading slash means the Global PHP Exception class will be caught
echo $e->getMessage();
}
?>
If this script is ever run at all, it will send an email, regardless of whether any data was submitted or not. Wrap it in a condition that only sends a message if data has been submitted, something like:
if (isset(_POST['email'])) {
try {
$mail = new PHPMailer(true);
...
You can be more thorough about this check, for example validate that a valid email address was submitted, that the other fields contain what you expect, and so on, but you should get the general idea.
Thanks, Kept only if statement and its working now. Able to use use Gmail SMTP services. For Gmail had to enable less secure app in Gmail and changed password once.
<?php
date_default_timezone_set('Etc/UTC');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';
if (isset($_POST['submit']))
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "XXXXX#gmail.com"; // Your Gmail address.
$mail->Password = "XXXXX"; // Your Gmail login password or App Specific Password.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
//Set who the message is to be sent from
$mail->setFrom('XXXXXgmail.com', 'Mr Singh');
//Set an alternative reply-to address
$mail->addReplyTo('XXXXX#gmail.com', 'Mr Singh');
//Set who the message is to be sent to
$mail->addAddress('YYYYY#gmail.com', 'Mr Singh');
$mail->Subject = 'Customer Enquiry Form';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['name']}
Email: {$_POST['email']}
Mobile: {$_POST['mobile']}
EOT;
$mail->send();
header("Refresh:1; contact.html");
} else {
echo "Your form is not submitted yet please call for booking";
}
?>
HTML Code
<form class="form-horizontal" id="submit" action="PHPMailer.php" method="post">
....... ** HTML CODE of Form .......
<input value="submit" name="submit" type="submit">

Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
// $mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'eportalforresidency#gmail.com'; // SMTP username
$mail->Password = '##RESIDENCY##'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->SMTPSecure = 'ssl';
$mail->setFrom('eportalforresidency#gmail.com', 'naimish golakiya');
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('eportalforresidency#gmail.com'); // Name is optional
$mail->addReplyTo('eportalforresidency#gmail.com', 'Information');
//$mail->addCC('cc#example.com');
//$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 = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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;
} else {
echo 'Message has been sent';
}
?>
i am trying to send a mail through phpmailer but its not working anyone could please help.i have downloaded te phpmaile from gitub and i have also tried to slove this error amonggg the amsweres mentioned in stackoverflow but still its not working.
Update the port number to 587. Also, those are fake credentials right?

PHPMailer HELP - Getting SMTP error but mail is send

I'm ripping my head off in a moment...
Using PHPmailer to send a email from my site.
I have created a HTML-form on my website, and the values from there needs to go to my mail... You know - standard :)
But I keep getting this error :
Mailer Error: SMTP connect() failed.
When I have turned SMTPDEBUG on it goes like this:
SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.
The host and port is correct, got the details from my provider..
Is there something i'm missing, typed in wrong or misunderstood?
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// here we use the php mail function
// to send an email to:
// you#yourdomain.com
mail( "info#recive.com", "Feedback Form Results",$message, "From: $email" );
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.test.com'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#recive.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->From = 'info#recive.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->addAddress('info#recive.com'); // Name is optional
//$mail->addReplyTo('info#recive.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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 = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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';
?>
From my understanding the 'ssl' setting for SMTPSecure is for direct ssl connect, while the 'tls' setting is for plain connect followed by upgrading to SSL with the STARTTLS command. With port 25 you need plain connect, e.g. 'tls'.

My SMTP Email Send Always in SPAM

Im developing a website wherein you can inquiry our ask question via email. every time i send it, it always goes to spam
here is my code:
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$QuestionComment = $_POST['comments'];
$hear = $_POST['about_us'];
$email_address = $_POST['email'];
// the first email is for thank you! email
date_default_timezone_set('Asia/Manila');
$mail = new PHPMailer();
$body = "Hi,<br /> <br />Thank you for contacting Magosaburo Philippines. <br />Please await for a confirmation email regarding your reservations and other concerns.";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.magosaburo.com.ph"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.magosaburo.com.ph"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "//dont mind it"; // SMTP account username
$mail->Password = "//dont mind it"; // SMTP account password
$address = $email_address;
$mail->AddAddress($address);
$mail->Subject = "Thank you from Magosaburo";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->SetFrom('send_mail#magosaburo.com.ph', 'Magosaburo');
$mail->AddReplyTo("send_mail#magosaburo.com.ph",'Magosaburo');
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
Im new in php email sending i dont know how or why it goes to spam. thank you!
Use a DK/DKIM signature in your mail, that might help.
If it's just gmail that's categorizing you as spam you may be on their blacklist, here's some info on getting off that. Then there's headers, there's an example of headers in action on this SO post. And finaly, on an email by email basis there's a few additional tips here.
Summary: 1. Check if you're IP is actually on google's spam list. 2. Use headers so provide filters with a more complete information on your emails. 3. Address any potential "spammy looking" content.
Good luck!
Its because of the fact that. The email providers have their own preferences about the email senders.
If the server from which you send emails is based on a malware. The email company will block them. Basically because the user has already marked the server as Spam. And the new emails are sent to the Spam!
Try adding some more information. Like a signature. Or try to read their user guides on how to remove or send a request to the email service to check that the emails you send are not malware!