I want to send a large mail from my webspace. This mail contains many variables, which are inputed through a form.
First I tried the php mail(), but I didn't receive any mail. I used mail() in another part of my website and from this script I receive a mail. So the function should work on my webspace.
For the large mail I tried to use PHPMailer, because the mail() didn't work as expected. The code for the PHPMailer is:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx.xxx.de';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->From = 'xxx#xxx.de';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.de', 'xxx xxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mess;
$mail->AltBody = $mess;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
After the script has finished, I don't get an error and I don't receive a mail. What could be a problem for this? How can I check, why my PHPMailer is not working? Has the require path to be absolute or relative?
Try this way:
//... another option code
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//...another your code
dump($mail->send());
Each time ,you open this php-page,the mail result will let you know.what are you missed.
use try/catch as follow :
$mail = new PHPMailer(true); // enable exceptions
try {
$mail->isSMTP();
//....
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
Related
I have made a website on my localhost. After everything was ready to go I hosted the website. However, now PHPMailer seems to cause the trouble. Mails are not sending and it seems like PHP code does not execute after $mail = new PHPMailer command. Here is part of the code:
if($conn->query($sql)){
require_once '/home/ziptie/public_html/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = true;
$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 = 'mygmail#gmail.com'; // your email id
$mail->Password = 'mypassword'; // your password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('mygmail#gmail.com', 'ZIPTIE');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$v="http://ziptie.rs/verification-page.php?sifra=$token";
$body = file_get_contents('/home/ziptie/public_html/verificationemail.html');
$body = str_replace('promenljiva', $v, $body);
$mail->Subject = 'Verifikacija kupca, ZIPTIE';
$mail->Body = $body;
$mail->addAttachment("naruzbine ".date("d-m-Y").".zip");
$mail->send();
header("Refresh:0; url=https://www.ziptie.rs/verification-page.php");
//echo '<script>alert("Verifikacioni link je poslat, proverite Vašu mejl adresu.")</script>';
/*if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}*/
}
else{
echo $conn->error;
}
Does anybody know the solution? Thanks!
found solution in upgrading to PHPMailer 6.3
I don't know if my code is the issue or if I had configured Zoho's SMTP's settings incorrectly.
Basically I want the ability to dynamically send emails with a simple php function like for example
phpMail("to#example.com", $subject, $body, "from#example.com", "replyto#example.com");
This is my PHPMailer.php script (it sees the function and its settings)
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**REMOVED**'; // SMTP username
$mail->Password = '**REMOVED**'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true); // Set email format to HTML
// SYTAX: phpMail($from, $reply, $to, $subject, $body);
function phpMail($to, $subject, $body, $from = "from#example.com", $reply = "replyto#example.com") {
if (isset($from))
$mail->From = $from;
$mail->FromName = "testing";
if (isset($to))
$mail->addAddress($to);
if (isset($reply))
$mail->addReplyTo($reply);
if (isset($subject))
$mail->Subject = $subject;
if (isset($body))
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Now the issue with this is that, I'm not receiving emails nor am I receiving any errors / messages but it's also not sending me an email.
When you make a PHP function, make sure your objects aren't outside of your function.
My error wasn't that though, it was basically my own stupidity which had nothing to do with the code but more of my "form post data".
The following configuration works for me using zoho mail.
Give it a try:
$mail=new JPhpMailer;
$mail->IsSMTP();
$mail->Host='smtp.zoho.com';
// Enable this option to see deep debug info
// $mail->SMTPDebug = 4;
$mail->SMTPSecure = 'ssl';
$mail->Port='465';
$mail->SMTPAuth=true;
$mail->Username='your_email_address';
$mail->Password='your_eamil_address_password';
$mail->isHTML(true);
$mail->SetFrom('your_email_address','Your Name');
$mail->Subject='PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody='To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress('destination_email_address','John Doe');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I created a php application at dan.creativeloafing.com. This just takes form data and builds an html page with it, then emails the contents of that page to dan#creativeloafing.com. A couple days ago, it stopped working. I have been trying to figure this out ever since. I was using the mail() php function and have switched it over to the PHPMailer Library. This is supposedly sending the emails and I get confirmation, but nobody ever recieves the email and I get no bounceback or any errors. This is the jist of the code:
//PHPMailer
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPSecure = 'tsl'; // Enable encryption, 'ssl' also accepted
$mail->Username = 'dan#omgsurvey.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPDebug = 0;
$mail->WordWrap = 50;
$mail->From = 'dan#omgsurvey.com';
$mail->FromName = 'DAN Application';
$mail->addAddress('dan#creativeloafing.com'); // Name is optional
$mail->addReplyTo($repEmail);
$mail->addCC('david.miller#creativeloafing.com');
$mail->isHTML(true);
$mail->Subject = "New DAN Request: ".$campaignName;
$mail->msgHTML(file_get_contents('./tmp/DAN_REQUEST_'.$specialString.$randomNumber.'.html'));
if(!$mail->send()) {
echo '<br />Proposal could not be sent.<br />';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Proposal has been sent';
}
The script always reaches 'Proposal has been sent.' too. This is driving me crazy!
So what was happening here is that godaddy was blocking emails that included my domain name in them. I am not sure if this was a spam issue, but they are currently looking into it. I have gotten the emails to send using a simple mail() function and by removing any references to omgsurvey.com in the email. Silly, this mail was only ever sent to two email addresses!
Isn't:
$mail->SMTPSecure = 'tsl';
supposed to be:
$mail->SMTPSecure = 'tls';
Use try...catch and PHPMailer(true); https://github.com/Synchro/PHPMailer/blob/master/examples/exceptions.phps
//Create a new PHPMailer instance
//Passing true to the constructor enables the use of exceptions for error handling
$mail = new PHPMailer(true);
try {
mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'relay-hosting.secureserver.net'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPSecure = 'tsl'; // Enable encryption, 'ssl' also accepted
$mail->Username = 'dan#omgsurvey.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPDebug = 0;
$mail->WordWrap = 50;
$mail->From = 'dan#omgsurvey.com';
$mail->FromName = 'DAN Application';
$mail->addAddress('dan#creativeloafing.com'); // Name is optional
$mail->addReplyTo($repEmail);
$mail->addCC('david.miller#creativeloafing.com');
$mail->isHTML(true);
$mail->Subject = "New DAN Request: ".$campaignName;
$mail->msgHTML(file_get_contents('./tmp/DAN_REQUEST_'.$specialString.$randomNumber.'.html'));
$mail->send();
echo 'Proposal has been sent';
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thinking send() should be Send()
if(!$mail->Send()) {
I am trying to let users fill out a contact form, which will then be sent to my email. But its not working for some reason. I just get a blank page with no error message or any text and email is also not sent.
if (isset($_POST['submit']))
{
include_once('class.phpmailer.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$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';
You need to call:
$mail = new PHPMailer(true); // with true in the parenthesis
From the documentation:
The true param means it will throw exceptions on errors, which we need
to catch.
Its working now, i didnt include the 'class.smtp.php' file. The working code is below:
if (isset($_POST['submit']))
{
include_once('class.phpmailer.php');
require_once('class.smtp.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$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 had the same problem with no error message even with SMTPDebug enabled. After searching around for working examples I noticed that I didn't include the SMTP Secure value. Try adding this line:
$mail->SMTPSecure = 'ssl'; //secure transfer enabled
Work like a charm now.
I had a similar problem. In reference to #Syclone's answer. I was using the default "tls".
$mail->SMTPSecure = 'tls';
After I changed it to
$mail->SMTPSecure = 'ssl';
It worked ! My mailserver was only accepting connections over SSL.
What worked for me was setting From as Username and FromName as $_POST['email']
Hope this helps
PHPMailer use exception.
Try this
try {
include_once('class.phpmailer.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
exit;
} catch (phpmailerException $e) {
echo $e->errorMessage(); //error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage();
}
I was trying to load an HTML file to send, which did not belong to the www-data group on my Ubuntu server.
chown -R www-data *
chgrp -R www-data *
Problem solved!
I was debating whether to write my own handler or crow-bar PHPMailer into my existing class structure. In the event it was very easy because of the versatility of the spl_autoload_register function which is used within the PHPMailer system as well as my existing class structure.
I simply created a basic class Email in my existing class structure as follows
<?php
/**
* Provides link to PHPMailer
*
* #author Mike Bruce
*/
class Email {
public $_mailer; // Define additional class variables as required by your application
public function __construct()
{
require_once "PHPMail/PHPMailerAutoload.php" ;
$this->_mailer = new PHPMailer() ;
$this->_mailer->isHTML(true);
return $this;
}
}
?>
From a calling Object class the code would be:
$email = new Email;
$email->_mailer->functionCalls();
// continue with more function calls as required
Works a treat and has saved me re-inventing the wheel.
Try this ssl settings:
$mail->SMTPSecure = 'tls'; //tls or ssl
$mail->SMTPOptions = array('ssl' => array('verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true));
I'm trying to send a file as an email attachment, but for some reason if the file is > 100k then the email doesn't go through, even though I get the email sent message.
It may also be a limit on the attachments in IIS smtp setting, but when I unchecked the Limit session size and limit message size options, it didn't change anything. I may have to restart the server tonight...
I don't know if it's a php.ini setting, or what.
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
I might be wrong because I don't use IIS but the code you provided would actually use a native MTA not SMTP. As far as I know you have to use the IsSMTP() method to let PHPMailer know that you intend to use SMTP.
Something like this:
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Your code is not actually checking if the message was sent or not.
You need to change your code to check the return of the send method
if ($mail->Send())
echo 'Message has been sent.';
else
echo 'Sorry there was an error '.$mail->ErrorInfo;
This should give you the error message saying what is up if it does go wrong.