I am trying to figure out why my users are receiving duplicate copies of email messages being sent from my web application. Here is the code which sends the email:
function _send_user_email($to, $subject, $message) {
$this->load->library('email');
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$config['protocol'] = 'sendmail';
$this->email->initialize($config);
$this->email->from('support#mysite.com', 'Customer Service');
$this->email->reply_to('support#mysite.com', 'Customer Service');
$this->email->to($to);
$this->email->bcc('support#mysite.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
if ( ! $this->email->send())
{
echo $this->email->print_debugger();
exit;
}
}
Is there anything wrong with this code that might be causing the message to be sent twice?
Obviously, because of
$this->email->send();
if ( ! $this->email->send())
You are sending email twice. Remove the first call, leave only the one in if statement
Related
I am in a little bit of trouble sending mail with attachments in CodeIgniter. Here is my code:
$to="mailtouser#gmail.com"
$fileUrl=array(base_url()."assets/pdf/sample.pdf", base_url()."assets/pdf/sample1.pdf");
$subject="TEST MAIL";
$message="TEST MESSAGE";
$this->load->library('email');
$from = "admin#gmail.com;
$config['wordwrap'] = TRUE;
$config['mailtype']='html';
$config['charset'] = 'utf-8';
$config['priority'] = '2';
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from($from, 'Little Bloom');
//$this->email->to();
if($cc!=''){
$this->email->cc('sendmail#gmail.com');
}
$this->email->bcc($to);
$this->email->subject($subject);
$this->email->message($message);
if(!empty($fileUrl))
{
foreach($fileUrl as $key)
{
$this->email->attach($key);
}
}
return $this->email->send();
There are multiple pdf files so I am using an array to save the URL but when I send this to my email function these files are not attached to the mail.
if you print you array $fileUrl, you'll se at once where the problem is. Using e.g.: echo '<pre>'; print_r($fileUrl);die;
the urls are missing a forward slash between your_site and assets: http://your_siteassets/pdf/sample.pdf
you can fix this either with:
$fileUrl=array(base_url("assets/pdf/sample.pdf"), base_url("assets/pdf/sample1.pdf"));
or with
$fileUrl= array(base_url()."/assets/pdf/sample.pdf", base_url()."/assets/pdf/sample1.pdf");
docs url helper base_url()
I am working on a project that needs to send email.I use this as my controller
$from_email = $this->input->post('email');
$to_email = "info#test.com";
$name = $this->input->post('name');
$phno = $this->input->post('phno');
$from = $this->input->post('start');
$to = $this->input->post('end');
$message = $this->input->post('message');
$bodyContent="<table>
<tr><td>Name:</td><td>$name</td></tr>
<tr><td>Phno:</td><td>$phno</td></tr>
<tr><td>From Date:</td><td>$from</td><td>To Date:</td><td>$to</td></tr>
<tr><td>Message:</td><td>$message</td></tr>
</table>";
//Load email library
$this->load->library('email');
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject('Enquiry From Test TOURS');
$this->email->message($bodyContent);
//Send mail
if($this->email->send()) {
$this->session->set_flashdata("email_sent","Email sent successfully.");
} else {
$this->session->set_flashdata("email_sent","Error in sending Email.");
}
redirect('test_view/index');
And I am getting the output in the mail as like this
<table>
<tr><td>Name:</td><td>wer</td></tr>
<tr><td>Phno:</td><td>wer</td></tr>
<tr><td>From Date:</td><td>10/18/2017</td><td>To
Date:</td><td>10/25/2017</td></tr>
<tr><td>Message:</td><td>wer</td></tr>
</table>
I don't know why i am getting output in the mail like this , can anyone sortout this error please.
use mailtype configuration 'html' in Email configuration file
$config['mailtype'] = 'html';
from user guide
You need to set the type as html :
$this->email->set_mailtype("html");
I'm building a form that allows someone to sign up for a deal. I want the form to send 3 emails using php once it has been submitted; one sending the details of the form to myself, one sending the details to the person offering the deal, and one sending the actual deal confirmation and voucher to the person who signed up.
I've run into problems in that certain email addresses don't seem to receive or be sent the email when i'm testing it (it works for both my hotmail and gmail addresses, but not for my own domain email address). I also can't figure out how to add some sort of html style to the emails being sent.
How can I style these emails and make sure that they are being sent to right email addresses?
here is the code:
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$airline=$_REQUEST['airline'];
$position=$_REQUEST['position'];
$checkin=$_REQUEST['checkin'];
$attendance=$_REQUEST['attendance'];
$email=$_REQUEST['email'];
$terms=$_REQUEST['terms'];
if (($name=="")||($position=="")||($checkin=="")||($attendance=="")||($email==""))
{
echo "All fields are required, please fill the form again.";
}
//email going to me//
else{
$from="From: Deal 1 Form Submission<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemail#test.com",
$subject,
$message="someones taken the deal: heres their info: <br> Name: $name\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n" );
echo "Email sent!";
}
//email going to the customer//
{
$from="From: me<myemail#test.com>\r\nReturn-path: $email";
$subject="Thank you for choosing this deal";
mail("$email", $subject, $message="thanks for choosing this deal!, please present this voucher when attending the restaurant", $from );
}
//email going to the partner//
{
$from="From: Me<myemail#test.com>\r\nReturn-path: $email";
$subject=" Great news! Someone has chosen your deal";
mail("partneremail#test.com",
$subject,
$message="Fantastic news, a customer has taken your deal! here's their info: Name: $name\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n
", $from );
}
}
?>
Many thanks.
Your best bet is to use PHPMailer. Here is some example code that relays the email through gmail. The reason why you would want to do this is to avoid emails from the server going into you spam box.
$debug = true; // set it to false in production
$Mail = new PHPMailer();
if ($debug) {
// this allows you to see the details of the connection to gmail
$Mail->SMTPDebug = 4;
}
$Mail->isSMTP();
$Mail->Host = 'smtp.gmail.com';
$Mail->SMTPAuth = true;
$Mail->Username = 'your#gmail.com';
$Mail->Password = '[your-password]';
$Mail->SMTPSecure = 'tls';
$Mail->Port = 587;
$Mail->setFrom('from#address.com', 'John Smith');
$Mail->addAddress('to#address.com', 'Someone Else');
$Mail->addReplyTo('from#address.com', 'John Smith');
$Mail->addBCC('bcc#address.com', 'BCC Person');
$Mail->addAttachment('path/to/img/or/pdf/or/whatever/you/want/to/attach.pdf');
$Mail->Subject = 'Test Subject';
$Mail->Body = '<h1>Hi! This is an HTML format body'
$Mail->AltBody = 'Hi! Im just a text email in case HTML is not supported!';
if (!$Mail->send()) {
print $Mail->ErrorInfo;
return false;
} else {
return true;
}
I can't figure out why if I try to use the CI Email Class it doesn't send emails, while if I use the native PHP mail() Class works.
Has to be noted that sometimes I get "email sent" while is not actually sent and sometimes I get the error "my server is not setup".
I tried to figure out how to set it up but... nothing...
Controller code follows:
if($this->form_validation->run()){
//Set Language
$this->lang->load('site', $this->session->userdata('lang'));
//Random key
$user_valid_key = md5(uniqid());
//Prepare email
$this->load->library('email', array('mailtype' => 'html'));
$this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
$this->email->to($this->input->post('email'));
$this->email->subject($this->lang->line('email_signup_subject'));
//Format mail con %s per inserire i campi necessari
$signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);
$this->email->message((string)$signup_msg);
if($this->email->send()){
//TODO: load view...
echo "email sent";
}else{
$to = $this->input->post('email');
mail($to, 'test', 'Other sent option failed');
echo $this->input->post('email');
show_error($this->email->print_debugger());
}
//TODO: Add to db
}else{
// Form validation failed
}
Use this setup email..
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'sender_mailid#gmail.com';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('sender_mailid#gmail.com', 'sender_name');
$this->email->to('recipient#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
I faced this problem and found the following solution. Just a little change in the email config and it's working 100%:
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
Codeigniter User Guide: https://www.codeigniter.com/user_guide/libraries/email.html
This setup works for me:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'Your SMTP Server',
'smtp_port' => 25,
'smtp_user' => 'Your SMTP User',
'smtp_pass' => 'Your SMTP Pass',
'mailtype' => 'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);
$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
if($this->email->send()){
//Success email Sent
echo $this->email->print_debugger();
}else{
//Email Failed To Send
echo $this->email->print_debugger();
}
I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)
But when run this Below code in my server it works for me.
application>controller>Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "my#gmail.com"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
and my view page where I defined f_email and email comes through post method.
application>view>emailtesting.php
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
if some error comes again please visit official documentation below:
https://codeigniter.com/user_guide/libraries/email.html
After fighting with this same problem for a couple hours I finally decided to change my config to send through a different server. My original server for some reason would send to some addresses but not others (in same domain). As soon as I changed to sendgrid it worked as expected.
If you are not getting the results you expect, try a different smtp server. The problem may not be your code...
I had the same problem and I try below code instead of Codeignitor's mail function.
mail('mypersonalmail#domainserver.com' , 'Test', 'Test Email');
It works and mail is send to the email address. That mail has sent by already created email address (As i think). In my case it is:
gtt28651ff#p3plcpnl0552.srod.ahx3.domainserver.com
So I copy this email address and try it with below code.
$this->email->from('gtt28651ff#p3plcpnl0552.srod.ahx3.domainserver.com', 'www.domainserver.com');
And it work fine. It seems to be some servers need already created email address to send the email while others are NOT.
Hope this is clear and helpful.
Lately, sending emails through Google SMTP can be very tricky due to their security checks.
Verify if:
you have the rDNS on your server and it's matching your server IP
disable the 2-step auth on the email you are using to send emails from (on Google settings)
have IMAP and POP enabled on the email you are using to send emails from (on Google settings)
allow less secure apps on the email you are using to send emails from (on Google settings)
Change the CI settings as follows:
//Google settings for CI
$config['protocol'] = 'ssmtp'; //smtp not working
$config['smtp_host'] = 'ssl://ssmtp.gmail.com'; //notice ssmtp
$config['smtp_user'] = 'anyemail#server.com'; //must use Google
$config['smtp_pass'] = '***********';
$config['smtp_port'] = '465';
$config['smtp_crypto'] = 'ssl';
//general settings
$config['_smtp_auth'] = TRUE; //important
$config['smtp_timeout'] = 30;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
//optional
$config['wrapchars'] = 76;
$config['wordwrap'] = TRUE;
I hope this will save you the 2 days I have struggled with this issue.
I've spent hours trying to change the port,enabling the ssl extension in php.ini file etc nothing worked only ended up in a message SMTP server is not configured properly.I use WAMP as a localhost,when i turn off my antivirus avast the same config worked!!! the was send successfully.so it may be your antivirus program blocking on a local machine.
I tried the code below. At one time the code was working fine. I was able to send the emails. After a few minutes, when I tried it again without even changing anything, I get this error message Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
I dont know whats wrong. Lately I am facing a lot of similar bugs with codeigniter.
public function email($message = NULL, $subject=NULL, $email=NULL){
if(!isset($email)){
$to = $this->session->userdata('email');
}else{
$to = $email;
}
$this->load->library('email');
$this->email->from('noreply#mydomain.com', 'Mydomain');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
i face this problem and work hard and i find solution this
just little change in email config its working 100%
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
I added the following lines and the mail is working again.
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);