Mail Sending, Line break & and From header using gmail smtp - php

i send email with PHPMailer, my code is:
$mail = new PHPMailer(true);
$mail->Mailer = "smtp";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 0;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "nati323#gmail.com";
$mail->Password = "****";
$mail->addCustomHeader('Content-Type: text/plain; charset="utf-8"');
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->From = $from;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $msg;
return $mail->Send();
the $msg variable is a function parameter i use it like this:
function sendMail ($to, $subject, $msg, $from, $add = null)
and i use it like this:
sendMail($_POST['mail'], $text['EMAIL_RECOVER_MSG_SUBJECT'], $text['EMAIL_RECOVER_MSG'], $from)
the variables contain:
$text['EMAIL_RECOVER_MSG_SUBJECT'] = 'Password Recover: From dmworld24.com';
$text['EMAIL_RECOVER_MSG'] = 'You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token={TOKEN}&mail={MAIL} \r\n If You Didnt Ask For Password Recovering Just Ignore This Message';
$from = 'System#dmworld24.com';
now i try to add line break to message for example:
You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token=7054343021*****353214&mail=nati323#gmail.com \r\n If You Didnt Ask For Password Recovering Just Ignore This Message
and what i get in the mail is the same, the \r\n shows as chars and there is not line breaks, what i did wrong?
another problem that i have, that the form method dosent work, i send email for try to nati323#gmail.com , and i connect to smtp server with nati323#gmail.com acount, and i always get the message from my self, and the Form that i defined dosent show....

You're using single quotes in your message. That will take everything contained in it literally, which is why you're getting the literal string \r\n.
Replace the single quotes with double quotes to get the new line properly output.
I.e.
$text['EMAIL_RECOVER_MSG'] = "You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token={TOKEN}&mail={MAIL} \r\n If You Didnt Ask For Password Recovering Just Ignore This Message";`

Call the PHP function nl2br() to convert newlines to HTML line breaks. And then, setup PHPMailer to sent email as an html using $mail->IsHTML(TRUE);

Related

Sending e-mail with attachment to gmail never gets received, using phpmailer

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'.

Godaddy phpmailer smpt configuration

We have a site on godaddy server. We have a contact form. When user filled form, first we should send mail to our info#oursite, after we should send mail to user's mail from our info#oursite. My tried code below:
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->port = 25;
$mail->Host = 'localhost';
$mail->Username = 'username#example.com';
$mail->Password = 'password';
$mail->addAddress($email, $name);
$mail->subject = 'Subject';
$mail->MsgHTML('mail içeriği');
if ($mail->Send()){
this outputs below:
it seems mail delivered, but it never delivered the message.
I tried many things;
$mail->SMTPAuth = true;
port 25,80,3535,465(with ssl),
tls, ssl authentication,
what should I do? what should I try more, any ideas?
First of all, you're using a very old version of PHPMailer; get the latest.
I don't know where you got your code, but you've got the case of some vital properties wrong - I suggest you base your code on the examples provided with PHPMailer. In particular, subject should be Subject and port should be Port. When sending to localhost you don't need a username or password, so you don't need to set them. Similarly, Port defaults to 25, so you don't need to set it.
You're not specifying a From address, and it looks like whatever address you're passing to addAddress is invalid, so you're sending a message from nobody to nobody - and not surprisingly it's not going anywhere!
It looks like your message body is in Turkish (?), which will not work in the default ISO-8859-1 character set, so I suggest you switch to UTF-8 by setting $mail->CharSet = 'UTF-8';.
Check your return values when calling addAddress to make sure the submitted value is valid before trying to send to it.
With these things fixed:
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer;
if (!$mail->addAddress($email, $name)) {
die('Invalid email address');
}
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 2;
$mail->Host = 'localhost';
$mail->Subject = 'Subject';
$mail->msgHTML('mail içeriği');
$mail->setFrom('me#example.com', 'My Name');
if ($mail->send()) {
echo 'Sent';
} else {
echo 'Error: '.$mail->Errorinfo;
}

Avoid phpMailer to send an email if it is not server validated

I have the following code to send an email with phpMailer. What I don't understand is why the email is sent and everything is OK even if I use a wrong password. In that case, the headers of the email give this answer (adapted from real case): X-PHP-Originating-Script: 532:class.phpmailer.php
How can I force the login error to appear and avoid the email being sent anyway? I guess that this has something to do with the class using other methods after trying to connect to the SMTP server. I don't want the email to be sent in ALL cases, if the password has changed or service not available, I want to know it and the script to stop and throw error. I use the latest available version of the class.
require 'phpmailer/class.phpmailer.php';
$to = "someone-email#example.com";
$subject = "Hello World";
$body = "HELLO WORLD";
$mail = new PHPMailer;
$mail->Host = "mail.example.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "example#example.com";
$mail->Password = "**********";
$mail->From = "example#example.com";
$mail->FromName = "TEST";
$mail->AddReplyTo($to);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "KO " . $mail->ErrorInfo;
return false;
}
else
{
echo "OK";
return true;
}
You aren't using SMTP so I guess it's defaulting to mail(), which doesn't accept authentication.
To enable SMTP:
$mail->IsSMTP();

PHPMailer - Certain SMTP messages don't send

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...

php code for sending a mail using smtp [duplicate]

This question already has answers here:
Reintroduce $HTTP_POST_VARS in PHP 5.3
(4 answers)
Closed 8 years ago.
this is my code am not receiving any mail through this code.
$email = $HTTP_POST_VARS[email];
$mailto ="nr.shubha#gmail.com";
$mailsubj = "Form submission";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Email form the web site form:\n";
echo $HTTP_POST_VARS;
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
$mailbody .= "$key : $val\n";
}
if (!eregi("\n",$HTTP_POST_VARS[email])) {
mail($mailto, $mailsubj, $mailbody, $mailhead);
}
print_r($HTTP_POST_VARS);
I strongly suggest you to use premade classes like https://github.com/PHPMailer/PHPMailer
It's much "spam safe" than using plain PHP code, and helps you with image embedding, attachments, etc...
I recommend you to use PHPMailer Library. Works great.
Here is a sample of my work
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
//This part is for authentication
$mail->Host = "mail.example.com"; // specify main and backup server
$mail->SMTPAuth = "SMTPAuth"; // turn on SMTP authentication
$mail->Username = "user#example.com"; // SMTP username
$mail->Password = "****"; // SMTP password
//the receiver will see that the sender address is this
$mail->From = "ihavesentit#example.com";
$mail->FromName = "The Sender";
//You can use AddAdress() and AddCC() functions several times for different receivers
$mail->AddAddress("receiver#example.com");
$mail->AddCC('another_receiver#example.com');
$mail->AddReplyTo("reply-to-me#example.com");
$mail->WordWrap = 50; // set word wrap to 50 characters (arbitrary)
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "mail subject";
$mail->Body = "<p>mail content will be typed here</p>";
$mail->AltBody = "will be used if the receiver does not accept HTML content e-mails";
//final and the most important action
$mail->Send();
This is Work only on the live server when it will host. It will not work on the localhost.
Because the SMTP only work on the live hosted server not on the "localhost".

Categories