Phpmailer not working on webserver - php

I am trying to get phpmailer to work on a web server. It does work with no problem on my xampp server but as soon as I put it on the web server it cant find these and it gives me the error. I also tried putting it on another web server and that doesn't work. I also copied the files from my xampp server to the web server and it still doesn't work.
use PHPMailer\PHPMailer\PHPMailer; (line 3)
use PHPMailer\PHPMailer\Exception; (line 4)
Error: parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '('email.php on line 3
the code that the error points to is
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function sendemail(){
require '../mail/src/PHPMailer.php';
require '../mail/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
include "removed(was database)";
$name = $_POST['name'];
$Uemail = $_POST['email'];
$select = $_POST['select'];
$issue = $_POST['issue'];
$resolution = $_POST['resolution'];
$datesubmited = date("Y-m-d H:i:s");
//Server settings
$mail->SMTPDebug = 4; //1-4 // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'removed'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'removed'; // SMTP username
$mail->Password = 'removed'; // SMTP password
$mail->SMTPSecure = ''; // Enable TLS encryption, `ssl` also accepted
$mail->Port = removed; // TCP port to connect to
//Recipients
$email = 'removed'; //put in default email that corresponds with the username / passwoard.
$mail->setFrom( $email, 'removed');
$mail->addAddress($Uemail, $name);
$mail->addAddress('removed', 'removed');
$mail->addAddress('removed', 'removed'); // Add a recipient
// $mail->addAddress('', $name);
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Request from ' . $name;
$mail->Body = 'Details ' . "<br>" .
"Date: " . $datesubmited . "<br>" .
"Name: " . $name . "<br>" .
"Email Address: " . $email . "<br>" .
"Area of Concern: " . $select . "<br>" .
"Issue: " . $issue . "<br>" .
"Suggested Resolution: " . $resolution . "<br>";
$mail->AltBody = 'Hello '. $name . ' your request has been sumbited!'.'</b>';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->send();
echo "Thank you " . $name . " for your submission." . "<br>" . "Someone will repsond to you shortly.";
echo "<br>";
echo "Go back home " . "removed" ;
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
?>

At first glance I would say that the use function seems to call the problem. Could you try adding these lines before your first use call and check the result:
$funcDefinition = new ReflectionFunction('use');
print $funcDefinition->getFileName() . ', ' .
$funcDefinition->getStartLine();
Check the results of the call on your local and your remote systems and compare the outcome. I don't think that you are deploying on Pre-PHP-5.3 systems where the use command was not available but please also add some information regarding the different PHP versions used locally and remote.
In fact it seems that you get your error message in case you are deploying on PHP 5.2 systems as this SO thread mentions. So first check has to be the PHP version (i.e. by adding a phpinfo(); before your first use call).
Conclusion: question author received the error due to deploying on a PHP 5.2 system without use support.

Related

How can I remove the promotional footer my hosting provider adds to emails?

How do I get rid of this footer in my messages?
require ('PHPMailerAutoload.php');
$mail = new PHPMailer;
// Form Data
$name = $_REQUEST['name'] ;
$subject = $_REQUEST['subject'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$mailbody = 'Information' . PHP_EOL . PHP_EOL .
'Name : ' . $name . '' . PHP_EOL .
'E-mail Address : ' . $email . '' . PHP_EOL .
'Message : ' . $message . '' . PHP_EOL;
// $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 = 'sample#gmail.com'; // SMTP username
$mail->Password = 'xxxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('sample#gmail.com', 'WebMaster'); // Admin ID
$mail->addAddress('sample#gmail.com', 'Admin'); // Business Owner ID
$mail->addReplyTo($email, $name); // Form Submitter's ID
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $mailbody;
// $mail->send();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You are using free hosting so you cannot remove this.
This advertisement is automatically attached by the hosting providers as you use their hosting.
Its better to use a paid hosting or try free trials of GOOGLE CLOUD/AWS/MICROSOFT AZURE
Your hosting provider is able to do this because you're doing this:
// $mail->isSMTP();
By commenting out this line, PHPMailer falls back to sending via PHP's mail() function. This way of sending sends through a local mail server without any encryption, and your provider is exploiting that fact in order to inject the footer you're seeing.
If you use a secure transport, such as by using SMTP with TLS to gmail, your provider is denied that opportunity because they can't see (let alone interfere with) your messages, and the footer would not appear.
However, its likely that you commented out that line because your SMTP configuration wasn't working, and that's likely to be because your hosting provider blocks outbound SMTP, which is very common, especially at the low end.

Bluehost: PHPMailer save sent message in Sent Items

I'm trying to send an email with the help of PHPMailer, everything works fine except now I want to have a copy of the send mail in Sent Items.
The website is currently hosted in Bluehost. I tried following the example of PHPMailer - GMail but I'm stuck on which path should I specify.
From the example of PHPMailer - GMail the path for Sent Items is:
{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail
I don't know what path should I specify. Everything works fine in my code, only the path for the sent item is missing.
<?php
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$username = 'john#mydomain.com';
$password = 'password';
function save_mail( $mail ) {
//path or folder for sent items
$path = "{imap.mydomain.com:993/imap/ssl}[...]/..."; //what exactly is the path for my sent item.
//Tell your server to open an IMAP connection using the same username and password as you used for SMTP
$imapStream = imap_open( $path, $mail->Username, $mail->Password );
$result = imap_append( $imapStream, $path, $mail->getSentMIMEMessage() );
imap_close( $imapStream );
return $result;
}
function send(){
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = 'mail.mydomain.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->setFrom( $username, 'John Doe' );
$mail->addAddress( 'someone#example.com', 'David Doe' );
$mail->Subject = 'TEST SUBJECT';
$mail->msgHTML('<b>TEST</b>');
$mail->AltBody = 'TEST';
if ( !$mail->send() ) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
if (save_mail($mail)) {
echo "Message saved!";
}
}
}
?>
Thanks to Max for the idea of imap_list function.
This gives me the list of a path for my mail directories.
$host = '{imap.mydomain.com/ssl/novalidate-cert}';
$mail = 'support#mydomain.com';
$pass = 'password';
$mbox = imap_open($host, $mail, $pass, OP_HALFOPEN)
or die("can't connect: " . imap_last_error());
$list = imap_list($mbox, $host, "*");
if (is_array($list)) {
foreach ($list as $val) {
echo imap_utf7_decode($val) . "\n";
}
} else {
echo "imap_list failed: " . imap_last_error() . "\n";
}
imap_close($mbox);
?>
using the {imap.mydomain.com} or {imap.mydomain.com:993/imap/ssl}gives me an error of:
can't connect: Certificate failure for imap.mydomain.com: Server
name does not match certificate: /OU=Domain Control
Validated/OU=Hosted by BlueHost.Com, INC/OU=PositiveSSL
Wildcard/CN=*.bluehost.com
Certificate issue, luckily I found this question and I end up using the following host:
{imap.mydomain.com/ssl/novalidate-cert}
and the path I'm looking to forward the sent mail into sent items is:
{imap.mydomain.com/ssl/novalidate-cert}INBOX.Sent

PHPmailer is giving me a 504 gateway timeout error

i am getting a 504 gateway timeout error from my server when using phpmyadmin to send out indivudual emails to a list of about 1200 users
i need the emails to go out to the users one by one as i do not want expose any of the email addresses in the list to any other users
it seems to work well when i am sending out to a small list, but when i want to send to a large list i am getting this error
what would be the best way to send out one email, reset the request and send out the next email in the list as it goes through the loop?
Also i'd like to see message sent to [name] each time the email goes out
heres my php
require('connection.inc.php');
include ("PHPMailer/class.phpmailer.php");
include ("PHPMailer/class.smtp.php");
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
if (isset($_POST['submit'])) {
error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('Etc/UTC');
$message = $_POST['message'];
$UeMail = uniqid();
$UDomain = uniqid();
$domain = '#zzz.org';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'zzz.zzz.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Username = 'zzz#zzz.com';
$mail->Password = 'zzz';
$mail->setFrom('zzz' . $UeMail . $domain);
$mail->addReplyTo('zzz#zzz.com');
$mail->WordWrap = 9999; // set word wrap
// I ADDED THIS TO BYPASS SSL ERRORS
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
// SET ENVOIRMENT #1 (DONT FORGET TO SET #2)
// LIVE ENVIORMENT EMAIL TO SMS
$result = mysqli_query($con, "SELECT name, mob_email FROM `user` WHERE mob_email<>''");
// TEST ENVIORMENT
// $result = mysqli_query($con, "SELECT email, name FROM user_test WHERE email<>''");
foreach($result as $row) {
set_time_limit(60);
$mail->Body = $message; //HTML Body
$mail->AltBody = $message; //Text Body
// SET ENVOIRMENT #2
// LIVE ENVIORMENT EMAIL TO SMS
$mail->addAddress($row['mob_email'], $row['name']);
// TEST ENVIORMENT
// $mail->addAddress($row['email'], $row['name']);
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("#", "#", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
}
else {
echo "Message sent to :" . $row['name'] . ' (' . str_replace("#", "#", $row['email']) . ')<br />';
// Mark it as sent in the DB (NOT USING THIS RIGHT NOW)
/*
mysqli_query(
$mysql,
"UPDATE mailinglist SET sent = TRUE WHERE email = '" .
mysqli_real_escape_string($mysql, $row['email']) . "'"
);
*/
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
$mail->clearAttachments();
}
}
and heres my html
<!DOCTYPE html>
<html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
Message:<br><textarea rows="5" name="message" cols="30" maxlength="110"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
all code is in a single file (sendEmail.php)
I can see that you've based your code on the PHPMailer mailing list example (though it looks like you may have started with an old version, so check you're using the latest), and it looks basically correct.
However, sending that many messages on page load is never going to work reliably. You need to run this from cron so that it is not subject to such timeouts. There's also no point in the call to set_time_limit(60); that will not extend the overall timeout you're hitting.
Also never disable SSL certificate verification unless you exactly why you are doing so - fix it properly. The PHPMailer troubleshooting guide gives lots of info about how to check what's wrong.

PHPmailer sending email successful, no email received [duplicate]

This question already has answers here:
PHPMailer email sent successfully but not received (EC2)
(3 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am using PHPmailer now for the first time, because I need to use attachments in future and the build in mail is not so good for that.
I am playing around with the example code and eventhough I get no errors + Message has been sent no messages arrive at the destination email.
Here is the code I use. The uncommented things can be ignored (I need them for a form, on submit an email should be sent)
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require '../vendor/autoload.php';
// Fetching Values from URL.
//$phone = $_POST['phone1'];
//$email_var = $_POST['email1'];
$email_var = 'sender.email.info#gmx.net';
$email_var = filter_var($email_var, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email_var, FILTER_VALIDATE_EMAIL)) {
$mail = new PHPMailer(); // Passing `true` enables exceptions
try {
//$mail->SMTPDebug = 2; // Enable verbose debug output
//$mail->isSMTP(); // Set mailer to use SMTP
//$mail->Host = 'SERVER'; // Specify main and backup SMTP servers
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'USER'; // SMTP username
//$mail->Password = 'PW'; // SMTP password
//$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('receiver.email#gmx.net');
//$mail->addAddress('receiver.email#gmx.net'); // Add a recipient
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = '<div style="padding:50px; color:white;">'
. '<br/> Kautionsrückzahlung Interesse von: <br/><br/>'
//. 'Telefon: ' . $phone . '<br/>'
. 'Email: ' . $email_var . '<br/>';
$mail->AltBody = '<div style="padding:50px; color:white;">'
. '<br/> Kautionsrückzahlung Interesse von: <br/><br/>'
//. 'Telefon: ' . $phone . '<br/>'
. 'Email: ' . $email_var . '<br/>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
I tryed now different things, it seems it does not matter whether I use server PW etc. I assume its only for localhost.
EDIT:
To clarify, I am not on localhost. I have the data for my server, but obviously I dont include the data in the question. I changed them to USER, SERVER and PW.
But it does not matter whether they are there or uncommented I get Message has been sentin both cases.
do you really need that to be done in your local host. instead you can deploy the code in any host and check. you don't have to use any additional stuff. simple mail option is enough. reason is in your local server no mail server configured. so you using some other class to achieve that.
Did you checked on Server host ?

PHPMailer w/Gmail - Not Working

I have been recently learning PHP, and I need to be able to send emails using it.
I have found out that PHPMailer is a great way to send the mail, so I have tried it out. I have set up my code according to all of the examples that I can find, but I still am unable to get it to work.
Here is my PHP code:
<?php
error_reporting(E_ALL);
$fc = file_get_contents("http://redxtech.ca/fbm/");
echo $fc;
if (isset($_GET["name"]) && isset($_GET["email"]) && isset($_GET["m"])) {
$name = $_GET["name"];
$email = $_GET["email"];
$msg = $_GET["m"];
}
else {
$name = "Blank Name";
$email = "<email here>";
$msg = "Blank";
};
$e_msg = "FB:/nYou have recieved a new submission form from " . $name . " at <" . $email . ">./n/nIt says:/n/n" . $msg . "./n/nDo what you wish with this information./n~ Gabe";
require "PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->SMTPDebug = 1;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "<my gmail>";
$mail->Password = "<my super secret password>";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->setFrom("<my gmail>", "Red's Mailer");
$mail->addAddress("<my other email>", "Gabe");
$mail->addAddress("<another email>", "FB");
$mail->addReplyTo("<my other email>", "Gabe");
$mail->isHTML = true;
$mail->Subject = "New Submission";
$mail->Body = $e_msg;
$mail->AltBody = "This is an altbody.";
if(!$mail->send()) {
echo "<script>console.log('Mail was not sent')</script>";
$errInf = $mail->ErrorInfo;
echo "<script>console.log('Mailer Error: " . $errInf . "')</script>";
} else {
echo "<script>console.log('Mail was sent')</script>";
}
?>
When I click on the submit button to send the form on the previous page, it takes me to /mail.php?name=aName&email=anEmail&m=aMsg and echo's the page, but it isn't sending the email.
When I open up Chrome Dev Tools:
"The devicemotion event is deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See for more details."
However, I'm pretty sure that is caused by the embedded Vimeo players and not PHPMailer.
If anyone can give me a hand here that would be great :D
I fixed my own problem.
When I mentioned that I didn't think it was loading properly, that was half correct.
To fix it, I added the rest of the .php files into the same directory as the autoloader. I thought that the autoloader automatically included all of them.

Categories