I am writing a script to send birthday messages automatically to employees. For now I have taken hard-coded email ids. Now the problem is that these scripts are working perfectly well on my laptop Windows 10 Wampserver3.0.6. But now when I am transfering these files to the main desktop in the office(Windows 7 Wampserver3.1.3), the email sending based scripts crash the computer when you embed or attach an image. If I remove the attachment and send only the text, the email gets sent. But on attaching an image the computer crashes directly. The code is as follows:
require("PHPMailer-master\src\PHPMailer.php");
require("PHPMailer-master\src\SMTP.php");
require("PHPMailer-master\src\Exception.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "abc#gmail.com";
$mail->Password = "********";
$mail->SetFrom("abc#gmail.com");
$mail->Subject = "Happy birthday!";
$mail->AddEmbeddedImage('compressed.jpg', 'compressed');
$mail->Body = "<h1>Happy Birthday!</h1><br>Have a wonderful day!";
$mail->AddAddress("abc#gmail.com","def#hotmail.com");
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
I have tried so many things. I have increased the memory_limit of wamp server.
An additional thing, debug=1 displays the process on chrome. So if i embed an image, it stops halfway, it does not display the process completely and crashes in the middle.
NOTE: addAttachment() works well but addEmbeddedImage() crashes the computer
It works perfectly well on my laptop. Please help.
Related
I just wanna clear out. This Code is Working. But somehow I have a problem. I'm just a university student and I'm including this phpmailer for my email verification in the registration part of my project. In my house, this code is working. But, when I move to our university about one city away, the php mailer is now failing to send Email and it says "SMTP error()". My friends guess that since I moved location and changed internet connection, the error might be with the HOST and PORT. Is there any other way to make PHPMailer working without regards to my IP address, or my location, or my internet connection??
I can't see the answer within the link
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
As I said before, this code used to work already. It's just that it does not function if I move to other location or other internet connection.
I hope somebody can help me with regards to this matter.
I guess it's within the PORT and HOST.
This is my code:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "cspgryffindor#gmail.com";
$mail->Password = "cspgryffindor123";
$mail->setFrom('cspgryffindor#gmail.com', 'CSPGryffindor');
$to=$email;
$mail->From = "no-reply#web.com";
$mail->AddAddress($to);
$mail->AddReplyTo("no-reply#web.com","no-reply");
//$subject = "this is s1ubject";
//$msg="this is msg";
$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 150;
if(!$mail->send())
{
// echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
// echo "Message has been sent successfully";
}
Your new provider may have blocked the SMTP port, some of them do to prevent spam.
You should check on their FAQ if they do.
Unrelated but if your password is really cspgryffindor123, now is a good time to change it
This could be your university blocking all SMTP connections like mine did.
To find out, try using your mobile connection or another connection other than your university connection. If it's working in your home, it should be with a regular connection.
I'm trying to use phpmailer to work alongside a contact form I have made. I was originally using simple php coding for the form, but this would not work on the web company's server, so they asked me to use phpmailer instead. I have changed the coding a little bit to try and integrate it into the contact page that has the contact form.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->Username = "myemail#hotmail.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->addAddress('stacey_victoria#hotmail.com', 'Sender'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message from Website';
$mail->Body = $message; $name;
$mail->From = $email;
if($_POST){
$feedback = 'Thanks for your message';
}
?>
The contact page appears as normal, and once the submit button has been pressed, the echo-feedback message is shown. However, no email is sent through.
I edited the "Simple Example" found here: https://github.com/PHPMailer/PHPMailer and uploaded the php file to the server and this worked fine so I know the smtp information I have used is correct and working.
Is it even possible to put phpmailer coding above the html coding?
If so, what am I doing wrong?
You are not sending the email...
Use :
if($mail->Send())
I've been trying to get my PhpMailer to work for over a week now.
I'm on a Windows machines, so I set up a "droplet" server with DigitalOcean (because they seem to have good step-by-step instructions) and then set up fake email accounts with ZohoMail (because it's free and I just need to test this.)
I've got breaks all over the three PhpMailer files I need (PhpMailerAutoload.php, class.phpmailer.php, and class.smtp.php). What I figured out is that I'm getting hung up on this line:
class.smtp.php line 1060 (or about since I've got breaks in there)
inside function get_lines:
$str = #fgets($this->smtp_conn, 515);
I put in more breaks, as below:
//*********Debug Code here
$test = $this->smtp_conn;
echo "\nContents of test var: \n";
var_dump($test);
echo "about to set str var \n";
$str = #fgets($test, 515);
echo "Contents of str var \n";
var_dump($str);
echo "\n out of debug code";
//***********End of debug code
After two minutes (2 minutes and 5 seconds to be precise) $test var_dumps just fine and "about to set str var" prints just fine.
But that's it. No more code executes.
I don't know what #fgets is. I don't see any documentation in the PHP manual for that, so I'm not sure where to go from here.
FIRST IDEA:
I checked on my php versions:
- server is running 5.5.9
- my PC is running 5.6.12
I'm guessing those are close enough.
SECOND IDEA:
I found that in the past there are a "==" vs "=" issue with this line of code, but that's clearly not the issue here.
THIRD IDEA:
I also read that some people fixed this problem by making sure that "php_openssl.dll" is enabled in their php.ini file.
In mine I have a line that reads:
extension = php_openssl.dll
So I think that's OK.
FOURTH IDEA:
Is my configuration wrong?
I'm using the following:
SMTP Host: smtp.zoho.com (should I get the IP address instead?)
SMTP Port: 465
and I think I have encryption and authentication turned on. (My code is at the end if that helps.)
FIFTH IDEA:
Start over using gmail to see if that gives me any clues, unless someone has a better idea.
function send_SMTP_email($email, $name, $subject, $msg) {
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require_once '/inc/phpmailer/PHPMailerAutoload.php'; // will load class.phpmailer.php and class.smtp.php itself
require_once '/inc/config.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging, TO DO: Disable for production
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = SMTP_HOST;
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = SMTP_PORT;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = SYSTEM_EMAIL;
//Password to use for SMTP authentication
$mail->Password = SYSTEM_PASSWORD;
//Set who the message is to be sent from
$mail->setFrom(SYSTEM_EMAIL, SYSTEM_NAME);
//Set an alternative reply-to address
$mail->addReplyTo(SYSTEM_EMAIL, SYSTEM_NAME);
//Set who the message is to be sent to
$mail->addAddress($email, $name);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Send HTML OR Plain Text
$mail->isHTML(true);
//Set body
$mail->Body = "<p>" . $msg . "</p>";
//Include plain text if non-HTML email reader
$mail->AltBody = $msg;
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
$result = $mail->send();
//send the message, check for errors
//if (!$mail->send()) {
if (!$result) {
$return_val = "Mailer Error: " . $mail->ErrorInfo;
} else {
$return_val = "";
}
return $return_val;
}
I just had the same problem and finally found the problem. The default is set to TLS. Once I changed it to SSL with
$mail->SMTPSecure = 'ssl';
everything worked fine. Hopes this helps, if anyone else faces the same issue.
I recently moved from windows shared hosting to a VPS. I'm struggling to get phpmailer to work for a contact form on my website.
The code for my site the sets up the email to send is:
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server or localhost
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->SMTPDebug = 1; // can vary the amount of debug info given on error
$mail->Username = "mail#mydomain.com"; // SMTP username
$mail->Password = "########"; // SMTP password
$mail->Port = 587;
This code worked on the old windows shared hosting. Also everything else is identical too with the site (the class.phpmailer.php file etc)
So I'm guessing it's something to do with my setup of iis or the server itself.
Connecting out works on port 587 according to my webhosts who checked that for me
I installed php using the Microsoft tool that does everything for you. the site is in php and is working, and it looks to my untrained eye like all is fine there.
I've checked that php_openssl.dll is enabled.
I've spent a long time reading around and fiddling with no luck. I'm really at a loss as what to look at next. Any ideas what it could be or what I should try?
thanks!
Phil.
EDIT:
The action on the php form is to go to contactprocessor.php.
That file contains this code:
<?
ob_start();
if(isset($_POST['btnSubmit']))
{
require("class.phpmailer.php");
$mail = new PHPMailer();
//Your SMTP servers details
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server or localhost
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->SMTPDebug = 1; // can vary the amount of debug info given on error
$mail->Username = "mail#mydomain.com"; // SMTP username
$mail->Password = "########"; // SMTP password
$mail->Port = 587;
$redirect_url = "http://www.mydomain.com/contact/contact_success.php"; //Redirect URL after submit the form
$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Website Contact Form";
$mail->AddAddress("mail#mydomain.com", "From Contact Form"); //Email address where you wish to receive/collect those emails.
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "From Contact Form";
$message = "FullName: ".htmlspecialchars($_POST['fullname'])." <br> <br>
\r\n <br>EmailAddress: ".htmlspecialchars($_POST['email'])." <br> <br>
\r\n <br>Phone Number: ".htmlspecialchars($_POST['mobile'])." <br> <br>
\r\n <br> Message: <br>".htmlspecialchars($_POST['message']);
$mail->Body = $message;
if(!$mail->Send())
{
echo "Sorry the message could not be sent. Please email mail#mydomain.com instead. Thanks<p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
header("Location: $redirect_url");
}
?>
Instead of actually sending the email, it just seems to print the above to screen of the user as if it's a webpage. it's as if the server thinks it's a html page and is just serving it.
You wrote:
Instead of actually sending the email, it just seems to print the above [code] to screen of the user as if it's a webpage. it's as if the server thinks it's a html page and is just serving it.
I think your problem is unrelated to phpMailer, or indeed email at all.
This is almost certain to be simply a small difference between the PHP configuration on your old and new servers.
Your PHP code is using the short-style PHP tags -- ie starting with <?.
Many PHP servers are configured not to allow this short-style tag, and only run PHP code that starts with the full <?php tag.
Given what you stated in the remark I quoted above, I suspect this is your problem, because what you describe is exactly the symptom I would expect from that.
You have two options:
Change your code to use the long style <?php tag.
Change your server config to allow the short <? tag.
I would recommend taking option (1) if possible, because the long-form tags are considered best practice (the short form is not recommended because it can have a potential clash with the <?xml marker at the start of most XML documents).
If you have a lot of short-form <? tags (and assuming you're allowed to do so), changing the server config may be worth considering, but if it's easy enough to fix the problem by switching to long tags, I'd say that would be better.
Hope that helps.
Thanks to Spudley for the answer. I had <? at the start (which is a shorthand that doesn't work on new php installations apparently). It should be <?php that's made it work. Thanks again.
I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // 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 as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.