I have a procedure to send mails as this:
sub SendMail {
my $subject = shift;
my #message = #_;
my $sender;
my $MIME_BOUNDARY = '====Multipart.Boundary.689464861147414354====';
my $now = strftime("%Y-%m-%d %H:%M:%S", localtime);
my #addresses = split(",", $ENV{ADMIN_MAIL});
my $sender = $ENV{USER} || $ENV{USERNAME};
$sender .= "\#" . hostname();
my $smtp = Net::SMTP->new($ENV{MAILHOST} || 'mailhost', Debug => 1);
unless ( $smtp ) {
die "Error while sending notification mail. Not connected to SMTP server.";
}
$smtp->mail( $addresses[0] );
$smtp->recipient( #addresses );
$smtp->data;
$smtp->datasend("From: $sender\n");
$smtp->datasend("To: " . join(",", #addresses) . "\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("Date: " . strftime("%a, %d %b %Y %H:%M:%S %z", localtime) . "\n");
if ( #log_messages ) {
$smtp->datasend("Mime-Version: 1.0\n");
$smtp->datasend("Content-Type: multipart/mixed; boundary=\"$MIME_BOUNDARY\"\n");
$smtp->datasend("This is a multipart message in MIME format.\n");
$smtp->datasend("--$MIME_BOUNDARY\n");
}
$smtp->datasend("Content-type: text/plain; charset=UTF-8\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\n");
foreach ( #message ) { $smtp->datasend("$_\n") }
$smtp->datasend("\n\n");
$smtp->datasend("Message from " . hostname() . " (PID=$$) sent by 'LogDumper.pl' at $now");
$smtp->datasend("\n");
if ( #log_messages ) {
$smtp->datasend("\n");
$smtp->datasend("--$MIME_BOUNDARY\n");
$smtp->datasend("Content-Type: text/plain; name=\"logs.txt\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"logs.txt\"\n");
$smtp->datasend("\n");
foreach ( #log_messages ) { $smtp->datasend("$_\n") }
$smtp->datasend("\n");
$smtp->datasend("--$MIME_BOUNDARY--\n");
}
$smtp->dataend;
$smtp->quit;
}
The procedure works fine with plain text mails, i.e. empty #log_messages. However, if I try to attach a text file
my #log_messages;
push #log_messages, "Line 1";
push #log_messages, "Line 2";
SendMail("The Subject", "The Message");
then the mail is not sent.
Debug output is this:
Net::SMTP=GLOB(0x7a78a40)<<< 354 Start mail input; end with <CRLF>.<CRLF>
Net::SMTP=GLOB(0x7a78a40)>>> From: Domscheit#xxxxx
Net::SMTP=GLOB(0x7a78a40)>>> To: wernfried.domscheit#xxxxx.xxx
Net::SMTP=GLOB(0x7a78a40)>>> Subject: The Suject
Net::SMTP=GLOB(0x7a78a40)>>> Date: Mo, 01 Okt 2018 10:15:57 W. Europe Daylight Time
Net::SMTP=GLOB(0x7a78a40)>>> Mime-Version: 1.0
Net::SMTP=GLOB(0x7a78a40)>>> Content-Type: multipart/mixed; boundary="====Multipart.Boundary.689464861147414354===="
Net::SMTP=GLOB(0x7a78a40)>>> This is a multipart message in MIME format.
Net::SMTP=GLOB(0x7a78a40)>>> --====Multipart.Boundary.689464861147414354====
Net::SMTP=GLOB(0x7a78a40)>>> Content-type: text/plain; charset=UTF-8
Net::SMTP=GLOB(0x7a78a40)>>> Content-Disposition: quoted-printable
Net::SMTP=GLOB(0x7a78a40)>>> The Message
Net::SMTP=GLOB(0x7a78a40)>>> Message from xxxxx (PID=8072) sent by 'LogDumper.pl' at 2018-10-01 10:15:54
Net::SMTP=GLOB(0x7a78a40)>>> --====Multipart.Boundary.689464861147414354====
Net::SMTP=GLOB(0x7a78a40)>>> Content-Type: text/plain; name="logs.txt"
Net::SMTP=GLOB(0x7a78a40)>>> Content-Disposition: attachment; filename="logs.txt"
Net::SMTP=GLOB(0x7a78a40)>>> Line 1
Net::SMTP=GLOB(0x7a78a40)>>> Line 2
Net::SMTP=GLOB(0x7a78a40)>>> --====Multipart.Boundary.689464861147414354====--
Net::SMTP=GLOB(0x7a78a40)>>> .
Net::SMTP: Unexpected EOF on command channel at C:\Developing\Source\LogDumper.pl line 1271.
Apparently there is a missing or needless \n. I tried to put it almost everywhere but I don't get it working.
Update:
Actually it is working when I execute the script on Linux but not on my Windows development PC.
I think the problem is solved.
It works on production server at Linux
It works on development Windows PC when I am working in the office
It does not work on Windows when I am working at home and connected via VPN - nasty security department...
Well, it is not really solved but for me it is working fine. I did not test (yet) whether it fails over VPN also for one of the higher-level packages.
Recently i came across -- as a header of mail() function in php. I tried to google it out but didn't found any result so i am posting this question. While trying to edit my code i found that the id after these two -- should be unique.My php script to send email with attachement.
<?php
$file = "http://wpbrisk.com/download/email/email/doc1.pdf"; // Specify the Url of the attachment here
$filename = "doc1.pdf"; // Specify the name of the file here
$to="hudixt#gmail.com"; // Enter the mail to which you want to sent the attachement
$your_name = "Hudixt"; // Your name
$from_mail = "info#wpbrisk.com"; // Your email
$subject = "This is a mail with attachment."; // Subject
$message = "<div id='hello'>World</div>"; // message
// Get the content of the file
$content = file_get_contents($file);
$content = base64_encode($content);
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($mailto, $subject, "", $header);
?>
Can you please tell me what this -- means in header and why it should be unique.
These "--ID--" lines are MIME part boundaries (see https://en.wikipedia.org/wiki/MIME). They are described in https://www.rfc-editor.org/rfc/rfc2046. They are used to separate different parts of a message.
According to RFC#2046 those only must be unique "enough". If you want to encapsulate an email into another one, the boundaries must not infer with each other, i.e. they have to be distinct. So it's a good idea to make them as unique "as possible and needed":
This implies that it is crucial that the composing agent be able
to choose and specify a unique boundary parameter value that does
not contain the boundary parameter value of an enclosing multipart
as a prefix.
(https://www.rfc-editor.org/rfc/rfc2046#section-5.1)
Seems like a MIME boundary to me...
Basically, when you have multiple types of content in an e-mail message, the different parts are separated using an ID. Each part is then preceded by '--' followed by the boundary. From the RFC:
Thus, a typical multipart Content-Type header field might look like this:
Content-Type: multipart/mixed;
boundary=gc0p4Jq0M2Yt08jU534c0p
This indicates that the entity consists of several parts, each itself with a structure that is syntactically identical to an RFC 822 message, except that the header area might be completely empty, and that the parts are each preceded by the line
--gc0p4Jq0M2Yt08jU534c0p
See http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html for more info.
To answer your last question, when you have an e-mail with text and attachment, the whole message will have Content-Type: multipart/mixed, the text will be text/plain, the attachment (suppose a word file) will be application/msword. Each new content-type part will be separated by '--' . $boundary. The boundary is chosen to be unique and different from the other message parts so that it doesn't appear inside either of them - when the application interpreting the e-mail contents finds $boundary it must be safe to assume that's an intended boundary and not just a fluke inside the message.
Hope that helps!
besides there is Mime:Lite etc. I have no other possibility to use Net::SMTP.
I am trying to send an email containing html message part and also an attachment containing text. I guess something is not in the right order or mime format may be incorrect?
I can get the attachment with the content but no message in the email body is displayed:
$smtp->data();
$smtp->datasend("Subject: ".$self->{SUBJECT}."\n");
$smtp->datasend("To: ".#{$self->{TO}}[0]."\n");
$smtp->datasend("From: ".$self->{FROM}."\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: multipart/mixed; boundary=\"frontier\"\n");
$smtp->datasend("\n--frontier\n");
$smtp->datasend("Content-Type: text/html; charset=\"UTF-8\" \n");
$smtp->datasend("<h3>".$self->{BODY}."<\/h3>");
$smtp->datasend("\n");
$smtp->datasend("--frontier\n");
$smtp->datasend("Content-Type: application/text; name= traps.txt \n");
$smtp->datasend("Content-Disposition: attachment; filename=\"traps.txt\"\n");
$smtp->datasend("\n");
$smtp->datasend($self->{BODY});
$smtp->datasend("\n");
$smtp->datasend("--frontier\n");
$smtp->dataend();
$smtp->quit;
I am creating a page that sends the response from contact form within mail but reply-to is not working (the variable i am using is having value, but is not added within headers)
here's my code:
<?php
//$uname=$_REQUEST['uname'];
if(isset($_REQUEST['name']))
{
$name=$_REQUEST['name'];
}
if(isset($_REQUEST['email']))
{
$email=$_REQUEST['email'];
}
if(isset($_REQUEST['phone']))
{
$phone=$_REQUEST['phone'];
}
if(isset($_REQUEST['message']))
{
$message=$_REQUEST['message'];
}
// TESTING IF VARIABLES HAVE VALUES
echo "$name $email $phone $message";
// RESULT: TRUE TILL HERE
if($name=="" || $email=="" || $phone=="" || $message=="")
{
header("location:../?inst=invalid");
}
else
{
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to="mail#example.com";
// Your subject
$subject="$name Contacted you via Contact Form";
// From
$headers = "From: ME <no-reply#example.com>\r\n";
$headers .= 'Reply-To:' . $email . "\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: PHP\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
print $message;
// send email
$sentmail = mail($to,$subject,$message,$headers);
//$sentmail = mail($to1,$subject,$message,$header);
}
// if your email succesfully sent
if($sentmail){
echo "Mail Sent.";
}
else {
header("location:../landing/?status=verification-pending");
}
?>
Now when i checked headers in my gmail, the value for $email doesn't appear in header information, Also no message is received. All I get is a blank message or may be $message is not printing anything like the same case i am facing with reply-to.
please help me a little with this. Thanks in advance.
Check that you have php warnings and notices enabled. When you are echoing out $name, $email, etc you are doing a redirect using headers after that. If your php notices etc aren't turned on, your header redirect will fail due to having already echoed something, and you won't know that you had invalid input. This is part of the reason you shouldn't echo things out during logic, but should store and out put values later.
Actually there was a little mistake with the above code. I mistakenly added = instead of == while comparison.
This line:
if($name=="" || $email="" || $phone="" || $message="")
Should read as:
if($name=="" || $email=="" || $phone=="" || $message=="")
Since = is for equation and not a condition for comparison ==
Fixed it in the Question
I'm trying to send an e-mail with Codeigniter like this:
$this->load->library('email');
$this->email->from("myemail#email.com");
$this->email->reply_to("myemail#email.com");
$this->email->to("myemail#email.com");
$this->email->subject("Test mail");
$this->email->message("Email body");
$this->email->set_alt_message("Email body txt");
$this->email->send();
and I got this on the email debugger: Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
If I do e simple PHP mail() function with the same addresses, it works but when I use CodeIgniter it gives me the error. So why would it work with simple mail() but not with CodeIgniter ? Any ideas ?
Thanks.
Had similar issue.
That's working code from the controller :
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "smtp";
$config['smtp_host'] = "localhost";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->from($fromEmail, $fromName);
$this->email->to($email);
$this->email->subject('Тест Email');
$this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));
$this->email->send();
Clearly, there does not seem to be a definitive 'one size fits all' answer. What worked for me was changing
$config['protocol'] = 'smtp';
TO:
$config['protocol'] = 'mail';
Hope this helps...
Do you have an email.php file in your config folder? Maybe there's a problem with your configuration in there.
Nobody seemed to really find a definitive answer, so I did some digging around and found out why.
in system/libraries/Email.php, first look at line 1552:
if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
it seems to send everything all peachy like. I had the exact same symptoms too. To see if I was crazy, i inserted immediately before...
mail($this->_recipients, $this->_subject, $this->_finalbody)
so I basically removed all the headers and let PHP put in the defaults. Bingo! Without CI's headers, it works. With the CI headers, it doesn't. So what is it?
Digging around some more, I looked up to where html is initialized and used. Turns out it doesn't really do anything until around 1046, where it builds the message body.
from line 1048:
if ($this->send_multipart === FALSE)
{
$hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
$hdr .= "Content-Transfer-Encoding: quoted-printable";
}
else
{
$hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
$body .= $this->_get_mime_message() . $this->newline . $this->newline;
$body .= "--" . $this->_alt_boundary . $this->newline;
$body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
$body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
$body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
$body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
$body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
}
Flipping send_multipart between TRUE and FALSE will make the mail class work or not work.
Looked through the Code Ignitor's email class docs reveals nothing. Going to line 52:
var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
So there you have it. Possibly an error in how CI does multipart messages? The hidden config preference
$config['send_multipart'] = FALSE;
in the email.php seems to do the trick.
Be sure domain name in
$this->email->from("myemail#**email.com**");
match to server domain name
Add a protocol variable to the config array and assign it the value "sendmail". The email.php file in the config folder should read as shown below. Mine works like this:
$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
Ensure that Apache can send emails.
To check your current sendmail status:
sestatus -b | grep httpd_can_sendmail
Change it to this if it is off:
sudo setsebool -P httpd_can_sendmail on
I read a comment in the file email.php :
// most documentation of sendmail using the "-f" flag lacks a space after it, however
// we've encountered servers that seem to require it to be in place.
return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));
"-f" flag - this problem!!!
Had the same problem,
Make sure your 'from' address is a valid email address.
I had the same problem and though it seems silly to me now, I had some of the config array options capitalized when they all need to be lowercase:
was:
$mail_config['smtp_host'] = 'mail.example.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'email#example.com';
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'TLS'; //ERROR
$mail_config['protocol'] = 'SMTP'; //ERROR
$mail_config['mailtype'] = 'HTML'; //ERROR
$mail_config['send_multipart'] = FALSE;
$this->email->initialize($mail_config);
Fixed:
$mail_config['smtp_host'] = 'mail.example.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'email#example.com';
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls'; //FIXED
$mail_config['protocol'] = 'smtp'; //FIXED
$mail_config['mailtype'] = 'html'; //FIXED
$mail_config['send_multipart'] = FALSE;
$this->email->initialize($mail_config);
That worked for me
Its worth saying that if you're on WAMP (Windows) you will need to have sendmail installed otherwise there is no default SMTP method of sending. I wanted to use Gmail but couldn't because there is simply no default mail mechanism.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'pass',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email',$config);
**$this->email->set_newline("\r\n");** <- add this line
this code worked for me.