Something weird is going on when i'm trying to send an e-mail with attachment to gmail, using phpmailer.
Sometimes the email received after a delay of 5 minutes and sometimes it is never received. From phpmailer debugger i get that the email is actually sended, so i assume it has something to do with the attachment getting blocked by gmail.
The attachment is a zip file containing only .jpg and .pdf files.
If i remove the addAttachment('path/to/file') everything works fine.
The weird thing is that sometimes, even with the attachment, the email received immediately and other times the email never gets received.
Here is the code
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'my_host';
$mail->Port = 'my_port';
$mail->SMTPAuth = true;
$mail->Username = 'my_username#domain.com';
$mail->Password = 'my_password';
$mail->setFrom('my_username#domain.com', 'my_username#domain.com');
$mail->addAddress('test#gmail.com', 'test#gmail.com');
$mail->Subject = $subject;
$mail->msgHTML($msg);
$mail->AltBody = $subject;
$mail->addAttachment($zipfilename);
if($mail->send()){
echo "ok";
}else{
echo "error";
}
I don't have any clue why this is happening so any help will be appreciated.
EDIT:
I found out that with two changes everything works fine.
The changes are the setting
$mail->SMTPSecure = 'tls'
and i had a path like this ./path/to/file and changed it to 'path/to/file'.
Related
I am using PhpMailer class to send emails that contains html,I noticed that if the body on email contain img tags the email not receiving and no errors showing.
Any suggestions?
my code so far:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "myusername";
$mail->Password = "mypassword";
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body =$body;
$mail->IsHTML(true);
if ($mail->send()) {
return 1;
} else {
return 0;
}
if you're trying to send it from localhost and read in gmail, it won't work because gmail often creates proxy image links. you need to send image links that direct to internet-accessible location. That's probably it.
If you're sending email with links to internet-accessible locations, please attach them to the question and I'll update the answer, too.
EIDT: after chat with the asker, it was determined the email HTML was missing normal HTML markup: <html><body>ACTUALCONTENT</body></html>, after adding those,image shown up correctly.
You can use the AddEmbeddedImage method to attach image to your body. So your code should look like this.
$mail->AddEmbeddedImage($_REQUEST['image_name'], 'ImageName');
And then in your $body you can use this image
$body.= "<img src='cid:ImageName' />";
I am making a mail sending module on user approvals. The mail is being sent to multiple users on the checkbox values. The problem is once I am using the given code it sends mail to all the checked users and redirects to the page given but as a blank page.
Now when I remove set_time_limit(120); few users are being left and mail is not sent to them even after checking all the users in list and the page is being redirected successfully.
Here is my code, please let me know rectification into this. This will be highly appreciated.
set_time_limit(120);
$mail = new PHPMailer();
$subject = "Welcome mail";
$content ="AAFM";
$mail->IsSMTP();
$mail->Timeout = 120;
$mail->SMTPKeepAlive = true;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port = 465;
$mail->Username = "xyz#xyz.com";
$mail->Password = "xyz";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->SetFrom("xyz#xyz.com", "xyz");
$mail->AddAddress($row['email']);
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);
if(!$mail->Send())
echo "Problem sending mail.";
else
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Candidates approval successful. Thankyou')
window.location.href='reportApprovalUser.php';
</SCRIPT>");
$mail->SmtpClose();
I have subsequently changed the time_limit variable in phpmailer Class too as 120.
Basically when mail is sent to all the listed users, the redirection shows a blank page and when redirection shows the actual page the list still has few users left even on selecting all for approval/mailing.
Solved the issue. Actually headers were not being sent due to whitespaces. Added ob_start() and its functional.
I have a script that sends emails to users, which are sometimes divided into multiple messages depending on size. Here's the code:
for($i=0; $i<count($the_message); $i++){
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "my.smtp.server";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->IsHTML($is_html);
if($is_html === TRUE){
$mail->AltBody = "To view the message, please use an HTML compatible email client, or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
$mail->Body = $the_message[$i];
$mail->Subject = $the_sub;
$mail->Send();
}
This used to work great. Now, it seems that certain messages will never send. It seems to be a data issue (or perhaps the way PHPMailer sends lines to the server?), but I can't pinpoint why certain message segments are always rejected by the server.
The error message I get from PHPMailer is this:
SMTP Error: Data not accepted.
And from PHP itself:
Notice: fputs(): send of 31 bytes failed with errno=104 Connection
reset by peer in class.smtp.php on line 580
Here is a segment of HTML data that always fails to send:
http://pastebin.com/LGYkTTFA
Note that size isn't an issue, I can successfully send much larger HTML messages than this. However, when I send multiple segments, this one is never accepted by the server.
Here, I rewrote it for you in a somewhat saner way. You don't need to create everything from scratch every time around the loop, just change the body and send again.
<?php
require 'PHPMailerAutoload.php';
$the_message = array('Hello', 'there', 'Justin');
$to = array('tom#example.com', 'dick#example.net', 'harry#example.org');
$the_sub = 'Crazy subject';
$email_port = 25;
$from = 'justin#example.com';
$is_html = true;
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "localhost";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->Subject = $the_sub;
$mail->IsHTML($is_html);
$mail->WordWrap = 200;
if($is_html){
$mail->AltBody = "To view the message, please use an HTML compatible email client, '.
'or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
for($i=0; $i<count($the_message); $i++){
$mail->Body = $the_message[$i];
$mail->setWordWrap();
$mail->Send();
}
Update: added word wrapping to deal with content with very long lines.
In class.smtp.php, line 51, I changed the constant MAX_LINE_LENGTH from 998 to 500. This allowed all of my email segments to pass through.
I'm still not sure if the issue lies with my SMTP server or PHPMailer, I'll have to test it elsewhere.
EDIT: Tested using Gmail's SMTP server, and keeping 998 worked fine. This leads me to assume that my SMTP relay server is rejecting data sometimes depending on the length.
EDIT2: Still experienced failures with the above fix. I changed my port number and added SSL, and it is magically working. Wonky server issues...
I've downloaded PHPMailer library from github and used the following script to send the mail. I've tried to send email to my own account and it worked. It printed "Message has been sent" and received the email in gmail. But when I tried sending mail to my friend's account, he didn't receive the email. But the script says message has been sent.
<?php
include('PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "example#gmail.com";
$mail->Password = "examplepassword";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("example2#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I'm using xampp. What's the reason behind the problem, do I need to change any settings in my gmail settings?
EDIT: Now I got mail to second address but after 40 mins. But when I send it first address, it is received immediately. Don't know why? I want to use it for email address verification, 40 mins is very long.
below link will help you:
Sending emails through PHP mail is slow
http://stackoverflow.com/questions/6380477/sending-emails-through-php-mail-is-slow
I got a contact us page using PHP. Like all contact us, after someone clicking send, it will send a warning to my email.
The problem is it doesn't work. The script works perfectly in my localhost, it doesn't work only on the server and it doesn't show any error.
$default_path = get_include_path();
set_include_path(dirname(__FILE__)."/../");
require_once("extensions/PHPMailer/class.phpmailer.php");
set_include_path($default_path);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPSecure="ssl";
$mail->Host="smtp.gmail.com";
$mail->SMTPDebug =0;
$mail->Port=465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = Yii::app()->params['sender_email']; // SMTP username
$mail->Password = Yii::app()->params['sender_password']; // SMTP password
$webmaster_email = Yii::app()->params['webmaster_email']; //Reply to this email ID
$mail->From = $email_address;
$mail->FromName = "Webmaster";
$mail->AddAddress($email_address,"");
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 70; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = $mailcontent->subject;
$mail->Body = $mailcontent->body;
//$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->Send();
I think it's because of server misconfiguration(this is my first time settings a server), but i didn't know what i did wrong.
The firewall already set to allow every traffic, so it's not firewall problem.
No error and no results makes me very confused.
Okay, after I checked everything, it turns out google block my email access from the server because of suspicious activity.
To unblock this, you need to visit this page : https://accounts.google.com/DisplayUnlockCaptcha
and then run the script again.