HTML Template message in codeigniter email - php

I am using codeigniter email library to send email. My message contains html code. but couldn't send email, instead it shows error like "This message is rejected by our SPAM filters". here is my code :
email-template.php
<html>
<body>
<div style="background-color: #f3f3f3; max-width: 650px; margin: 0 auto; box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.25);">
<!-- header section -->
<div style="/*border-bottom: 5px solid #cc342f;*/ border-bottom: 5px solid #fa233d; background-color: #f4f1f1; padding: 20px;">
<div style="text-align: left;">
<img src="<?php echo base_url()?>assets/images/logo.png" alt="test">
</div>
</div>
<!-- content section -->
<div style="padding: 20px 25px; text-align: justify; overflow: hidden; position: relative;">
<p style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; color: #fa233d;">
Hi <?php echo $user;?>,
</p>
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
You recently requested to reset your password for your ***** account.
</p>
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
Click the button below to reset your password.
</p>
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
Reset Password
</p>
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
If you did not request a password reset, please ignore this mail.
</p>
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
Thanks,
</p>
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 24px;">
*********
</p>
<!-- Disclaimer -->
<p style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: 100; color: #1b1b1b; line-height: 14px; border: 1px solid #ddd; padding: 10px">
<small>
If you have trouble clicking the reset button, copy and paste below URL in your web browser. <br/>
Reset Password
</small>
</p>
</div>
<!-- footer section -->
<div style="border-top: 3px solid #fa233d; background-color: #f4f1f1; padding: 10px 25px; text-align: right">
<p style="margin: 0; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #1d1d1d; text-decoration: none; display: inline-block;">+91 1231 123123 <span style="color: #fa233d;">|</span> +91 123123 12313 </p>
<span style="color: #fa233d;">|</span> Visit Us
</div>
</div>
</body>
home.php controller
public function forgotPassword() {
$email = $this->input->post('email');
$password = ******
$user = $this->Home_model->findUsernameByEmail($email);
$data = array('email' => $email,
'password' => $password,
'user' => $user);
$body = $this->load->view('templates/forgot-password', $data, true);
$result = $this->Home_model->forgotPassword($email, $body);
echo $result;
}
and Home_model.php
function forgotPassword($email, $message){ //print_r( $message); exit;
$this->db->where('LoginEmailID', $email);
$result = $this->db->get('usermaster_tbl')->row_array();
if($result['UserID'] != 0 && $result['UserID'] != ''){
$this->email->message($message);
$this->email->to($email);
$this->email->from('****#****.in', 'Administrator');
$this->email->subject('Reset Password');
$this->email->send();
print_r($this->email->print_debugger());
return 1;
}
else{
return 0;
}
}
if i use message other than html code, it is working. But if html template used, then mail is not sent

Add mailtype in your config
$config = Array(
'mailtype' => 'html',
...etc...
);
See http://codeigniter.com/user_guide/libraries/email.html

1- In your configuration file located in Applications / config / email.php you must put the following
$config['protocol'] = 'smtp';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = "youemail#gmail.com";
$config['smtp_pass'] = "yourpass";
$config['newline'] = "\r\n";
$config['priority'] = 1;
$config['mailtype'] = 'html'; //very important to send email html
2 - In order for your emails not to be detected as spam you must configure the from with the same mail that you put in the configuration file email.php
$this->email->from('myemail#gmail.com', 'Administrator');

I found my answer. Actually the problem is with url's used in template for image and anchor. I'm testing it with local server, so the link been blocked by mail server security. I changed it to live site url. Now the problem solved.
Thank you all for your valuable suggestions.

Related

How To send attachments with email in php

I have email content with data=> name, email, phone , description and attachment.
I am able send the email with all the data to email. But what I want is , Actually I want to send attachments to the email content. But in my code I just sending the links to the email content. But If I click on that links it is not downloading. I need to send attachments to the email and also when I click on that it should download. How to resolve this. Plese somebody help me.
Below is code,
$name = $this->request->post['name'];
$email = $this->request->post['email'];
$description = $this->request->post['description'];
$phone = $this->request->post['phone'];
$upload_base_dir="image/service-images/";
$upload_time_dir=time(); // setup directory name
$upload_dir = $upload_base_dir.$upload_time_dir;
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0775, true); //create directory if not exist
}
foreach($_FILES['attachment']['error'] as $k=>$v)
{
$uploadfile = $_FILES['attachment']['name'][$k];
move_uploaded_file($_FILES['attachment']['tmp_name'][$k],$upload_dir."/".$uploadfile);
$subject = 'Service Request';
$email_header ='<html style="color: rgb(34, 34, 34); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); max-width: 575px; line-height: 18px; font-size: 12px; font-family: Arial, Helvetica, sans-serif;"><head><meta name="viewport" content="width=device-width" /><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body bgcolor="#FFFFFF" text="#000000" style="color: rgb(34, 34, 34); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); max-width: 575px; line-height: 18px; font-size: 12px; font-family: Arial, Helvetica, sans-serif;"><table style="font-family:Helvetica,Arial,sans-serif;background:#EFEDED;" cellpadding="0" cellspacing="0" bgcolor="#EFEDED" border="0" width="100%"><tbody><tr><td align="center"><table width="96%" cellpadding="0" cellspacing="0"border="0"><tbody><tr><td style="border-top:5px solid #1e96d3; background:#fff;margin:0; padding:20px; border-spacing:0px;">';
$email_footer = '</td></tr><tr><td style="margin:0; padding:15px 0;"><table style="font-family:Arial, Helvetica,sans-serif;font-size:11px;color:#999999;" cellpadding="0" cellspacing="0" border="0" width="100%"><tbody> <tr>
<td style="font-family:Arial, Helvetica,sans-serif; margin:0; font-size:11px; padding:8px 0 0 0;">This email was intended for Inf. </td></tr><tr>
<td style="font-family:Arial, Helvetica,sans-serif; margin:0; font-size:11px; padding:8px 0 0 0;"></td></tr></tbody></table></td></tr></tbody></table></td>
</tr></tbody></table></body></html>';
$body = $email_header.'<table width="100%" cellpadding="0" cellspacing="0"><tr>
<td style="margin:0; padding:0px 0px 15px 0px; border-spacing:0px;">
<p style="font-size:14px; color: rgb(0, 0, 0); font-family: Arial, Helvetica, sans-serif; font-weight: bold; line-height: 1.5em; margin: 0px; padding: 0.4em; text-align: left;">'.$subject.'</p></td></tr><tr><td style="margin:0; padding:0px 0px 15px 0px; border-spacing:0px;"><p style="color:#000; font-size:13px; margin:0; font-family:Arial, Helvetica,sans-serif;"><strong>Dear Sir/Madam,</strong><br></p></td></tr><tr><td style="margin:0; padding:0 0 5px 0;"><p style="font-size:13px; background-color: rgb(234, 234, 234); color: rgb(0, 0, 0); font-family: Arial, Helvetica, sans-serif; font-weight: bold; line-height: 1.5em; margin: 0px; padding: 0.4em; text-align: left;">Details:</p></td></tr><tr><td style="margin:0; padding:0px 0px 15px 0px; border-spacing:0px;"><table style="font-family:Helvetica,Arial,sans-serif; font-size:12px; font-weight:bold; margin-top:10px; width:100%"><tbody>
<tr><td style="padding:4px 0;">Name</td>
<td>:</td><td style="font-weight:normal;">'.$name.'</td></tr>
<tr><td style="padding:4px 0;">Email Address</td>
<td>:</td><td style="font-weight:normal;">'.$email.'</td></tr>
<tr><td style="padding:4px 0;">Phone</td><td>:</td><td style="font-weight:normal;">'.$phone.'</td></tr>
<tr><td style="padding:4px 0;">Description</td><td>:</td><td style="font-weight:normal;">'.$description.'</td></tr>
<tr><td style="padding:4px 0;">Attachments</td><td>:</td><td style="font-weight:normal;">';
foreach($_FILES['attachment']['error'] as $k=>$v)
{
$uploadfile = $_FILES['attachment']['name'][$k];
move_uploaded_file($_FILES['attachment']['tmp_name'][$k],$upload_dir."/".$uploadfile);
$images[] = $_FILES['attachment']['name'][$k];
$count=0;
foreach($images as $image){
$count++;
}
if($_SERVER['HTTP_HOST'] == "localhost"){
$body .= '<a href='.'localhost'."/inf/".$upload_dir."/".$image.' download>Attachment'.$count.'</a> ';
}
if($_SERVER['HTTP_HOST'] == "192.168.1.156"){
$body .= '<a href='.'192.168.1.156'."/inf/".$upload_dir."/".$image.' download>Attachment'.$count.'</a> ';
}
if($_SERVER['HTTP_HOST'] == "floretmedia.net"){
$body .= '<a href='."http://floretmedia.net/temp"."/inf/".$upload_dir."/".$image' download>'.$image.'</a> ';
}
if($_SERVER['HTTP_HOST'] == "www.arbour.in" || $_SERVER['HTTP_HOST'] == "arbour.in"){
$body .= '<a href='.'http://www.arbour.in'."/".$upload_dir."/".$image.' download>Attachment'.$count.'</a> ';
}
}
$body .= '</td></tr></tbody></table></td></tr></table>'.$email_footer.'';
//echo $body;die;
$mail = new Mail($this->config->get('config_mail'));
$mail->setTo($this->config->get('config_email'));
$mail->setFrom($email);
$mail->setSender($name);
$mail->setSubject($subject);
$mail->setHtml($body);
$mail->send();
you can attach the file to the body like this
$body .= '</td></tr></tbody></table></td></tr></table>'.$email_footer.'';
$filename = 'myfile';
$path = 'your path goes here';
$file = $path . "/" . $filename;
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$body .= $content . "\r\n";
// send the mail
$mail = new Mail($this->config->get('config_mail'));
$mail->setTo($this->config->get('config_email'));
$mail->setFrom($email);
$mail->setSender($name);
$mail->setSubject($subject);
$mail->setHtml($body);
$mail->send();

My PHP code doesn't save the HTML form

I've made code to create a form in HTML and I want to save the form details in my server so i've made a PHP code to save it and it won't show me the details themselves, it only saves:
Email:
Password:
And the details from the form itself it won't save.
My PHP code:
<?php
$Email = "";
$Password = "";
if (isset($_POST["Email"])) {
$Email = $_POST["Email"];
}
if (isset($_POST["Password"])) {
$Password = $_POST["Password"];
}
$text = "
Email: $Email
Password: $Password";
$file = fopen('details.html','a+');
fwrite($file, $text);
fclose($file);
?>
My HTML code:
<html>
<head>
<title>Send price offer to Embit Systems Inc.</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<p style="text-align:center;">
</p>
<br>
<br>
<p style="font-family: Verdana, Geneva, sans-serif;text-align: center;">
Please fill out your business Email details to send an offer to Embit Systems Inc.
</p>
<br>
<p>
<form action="1.php" method="POST" enctype="text/plain" style="background-image:url(fff.png);
background-position:center;
background-size:20%;
background-repeat: no-repeat;
text-align: center">
<br>
<p style="font-family: Verdana, Geneva, sans-serif;">Connect to your Gmail account</p>
<p style="font-family: Verdana, Geneva, sans-serif;">Email:</p>
<input type="text" name="Email" id="Email" style=" width: 15%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;">
<br>
<br>
<p style="font-family: Verdana, Geneva, sans-serif;">Password:</p>
<input type="password" name="Password" id="Password" style=" width: 15%;
display: inline-block;
padding: 12px 20px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;">
<br>
<br>
<input type="submit" value="Login" style="font-family: Verdana, Geneva, sans-serif; width: 12%;
background-color: #498BF4;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;">
<br>
<br>
<br>
</form>
</p>
</body>
</html>
What do I need to change so my form will save itself in the site?
Edit: if it helps the site's URL is:
embit.comli.com
Check your Email and Password are not empty, and then only append in your file,
if (isset($_POST["Email"]) && $_POST["Email"]) { // check email is not empty
$Email = $_POST["Email"];
}
if (isset($_POST["Password"]) && $_POST["Password"]) { // check password is not empty
$Password = $_POST["Password"];
}
if($Email && $Password){ // if both have values then only append in your file
$text = "
Email: $Email
Password: $Password";
$file = fopen('details.html','a+');
fwrite($file, $text);
fclose($file);
}
I can't understand what the problem may be...
It seems like I don't insert anything to the textboxes because what Rohan suggested doesn't seem to work.
Is there something wrong with the code? ID not matching or anything?

Adding html template to swift email body

I'm new to swiftmailer and I could successfully send email using it. I need to add a html template to the email body. I'm not sure how to do that?
This is the code I used to
function sendMail($destinationEmail){
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
$html = "<html>
<head>
<title>Alerting System</title>
<style type=\"text/css\">
<!--
table {
font-family: Verdana, Arial, Helvetica, sans-serif;
border: thin solid;
}
th {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #f0f0f0;
background-color: #ff0000;
font-size: 12px;
}
td {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000000;
font-size: 10px;
border: thin solid;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
-->
</style>
<head>
<body>
<CENTER>
<TABLE width=\"50%\">
<TBODY>
<tr>
<th >test</th>
</tr>
</TBODY>
</TABLE>
<br>
Alerting and Reporting System
</CENTER></BODY></HTML>
";
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
$destinationEmail => "test",
$destinationEmail => "test mail"
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("test#testcom.com", "Alerts");
$message->attach($html, "text/html");
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
}
How can I add this $html variable an email template? Any suggestion would be appreciated.
Use this
$html = "<html>
<head>
<title>Alerting System</title>
<style type=\"text/css\">
<!--
table {
font-family: Verdana, Arial, Helvetica, sans-serif;
border: thin solid;
}
th {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #f0f0f0;
background-color: #ff0000;
font-size: 12px;
}
td {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000000;
font-size: 10px;
border: thin solid;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
-->
</style>
<head>
<body>
<CENTER>
<TABLE width=\"50%\">
<TBODY>
<tr>
<th >test</th>
</tr>
</TBODY>
</TABLE>
<br>
Alerting and Reporting System
</CENTER></BODY></HTML>
";
$message->setBody($html,'text/html' // Mark the content-type as HTML);

PHPMailer doesn't send email

I would like to send and email trough the PHPMailer class. The code doesn't launch any error but the method $mail -> Send() returns false and I don't know why.
$emailList looks like:
Array(
[0] => mate80#yopmail.com
[1] => mate81#yopmail.com)
The ./html/mail/index.html looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- If you delete this meta tag, the ground will open and swallow you. -->
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TeamEQ</title><style type="text/css">img {max-width: 100%;}body {-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%;}#media only screen and (max-width: 600px) { a[class="btn"] {
display: block !important; margin-bottom: 10px !important; background-image: none !important; margin-right: 0 !important; }div[class="column"] {
width: auto !important; float: none !important; }table.social div[class="column"] { width: auto !important;}}
<!-- HEADER -->
<table class="head-wrap" bgcolor="#999999" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; background: white; margin: 0; padding: 0;">
<tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;">
<td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td>
<td class="header container" align="" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto; padding: 0;">
<!-- /content -->
<div class="content" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; max-width: 600px; display: block; margin: 0 auto; padding: 15px;">
<table style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; margin: 0; padding: 0;">
<tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;">
<td class="logo" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; text-align: center; margin: 0; padding: 15px;" align="center"><img src="{#urlToLogo}" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; max-width: 100%; margin: 0; padding: 0;" /></td>
</tr>
</table>
</div>
<!-- /content -->
</td>
<td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td>
</tr>
</table>
<!-- /HEADER -->
<!-- BODY -->
<table class="body-wrap" bgcolor="" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; margin: 0; padding: 0;">
<tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;">
<td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td>
<td class="container" align="" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto; padding: 0;">
<!-- content -->
<div class="content" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; max-width: 600px; display: block; margin: 0 auto; padding: 15px;">
<table style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; width: 100%; margin: 0; padding: 0;">
<tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;">
<td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;">
<h1 style="font-family: 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; line-height: 1.1; color: #000; font-weight: 200; font-size: 44px; margin: 0 0 15px; padding: 0;">Bienvenid#!</h1>
<p class="lead" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 17px; line-height: 1.6; margin: 0 0 10px; padding: 0;"></p>
<!-- A Real Hero (and a real human being) -->
<!--<p><img class="quote" src="https://s-media-cache-ak0.pinimg.com/236x/ad/5c/92/ad5c92c02290a4cb06dbcda734dc09da.jpg" /></p>-->
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">Has sido invitad# al equipo {#teamName} por parte de {#teamLeader}.</p>
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">Para completar el registro pulsa aquí, o copia el siguiente enlace en tu navegador:</p>
<!--
#env teameq-frontend.cloudapp.net variable de entorno.
Cambiar en la rama env las variables de entorno para el link de retorno del mail de invitación de usuarios.
-->
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">http://app.teameq.net/#/signup/teammate/{#teamId}/{#invitationalToken}</p>
<br style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;" />
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-weight: normal; font-size: 14px; line-height: 1.6; margin: 0 0 10px; padding: 0;">Gracias por tu atención, estamos ilusionados por ver cómo TeamEQ mejora tu entorno laboral!</p>
<!-- /hero -->
</td>
</tr>
</table>
</div>
</td>
<td style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; margin: 0; padding: 0;"></td>
</tr>
</table>
<!-- /BODY -->
The code updated with comments below is:
$teamMembers = $bbdd -> getTeam($teamId);
for($i=0; $i<count($emailList); $i++)
{
$userExistInTeam = false;
foreach($teamMembers as $key => $member)
{
if($member['email'] == $emailList[$i])
{
$userExistInTeam = true;
}
}
if(!$userExistInTeam)
{
$user = insertInitialTeamMate($teamId, $emailList[$i], $teamLeader["companyId"]);
$emailTemplate = file_get_contents('./html/mail/index.html');
$emailValues = array(
'{#urlToLogo}' => 'http://dapalmes.com/teamEQapiRC/html/mail/img/teameq-logo.png',
'{#teamName}' => $teamLeader["teamName"],
'{#teamLeader}' => $teamLeader["name"] ." ". $teamLeader["surname"],
'{#invitationalToken}' => $user["token"],
'{#teamId}' => $teamId
);
$mail = new PHPMailer(true);
$body = strtr($emailTemplate, $emailValues);
$mail -> setFrom('no-reply#teameq.net', 'TeamEQ');
$mail -> AddAddress($emailList[$i]);
$mail -> Subject = "Has sido invitado a TeamEQ";
$mail -> MsgHTML($body);
// $result = $mail -> Send();
if(!$mail->Send())
{
echo "error al enviar!!";
}
else
{
echo "mensaje enviado";
}
}
}
the error was that the Send method is written lower case
You have an error in this line . Instead of
$mail -> AddAdress =$emailList[$i];
Use
$mail -> AddAddress($emailList[$i]);
Also this code will send the mail twice. so remove this line.
$result = $mail -> Send();
You shouldn't be calling Send() twice, check for $result instead:
$result = $mail -> Send();
if(!$result)
{
echo "send KO";
}
else
{
echo "send OK";
}
Also; try using $mail = new PHPMailer(true); instead; it should enable Exceptions and will tell you if & why sending failed

Cannot forward the message to my inbox via PHP webform in XAMPP

I created a WEB FORM which is supposed to forward the message(s) of people who want to contact me, right into my GMAIL's inbox. It was supposed to be simple: people who visit my site and want to CONTACT me are just supposed to fill out their name, email and message and click submit button which should forward the message to my gmail address. However, that doesn't happens and I can't figure out why. I tried to test it via XAMPP but I never receive anything in my inbox.
In fact, I always receive the message: SMTP ERROR: COULD NOT CONNECT TO SMTP HOST.
I am still new to web design and PHP so this is probably a basic question for you. I tried to solve this problem for a few days, asked for help on some forums but still couldn't find the solution.
I downloaded XAMPP 1.8.1. in ZIP form and extracted it to C:\xampp folder.
Then I started the Apache server and requested my php file via localhost/email/newnew.php
I simply filled the webform and clicked on submit button and then I receive the message: SMTP ERROR: COULD NOT CONNECT TO SMTP HOST.
Here is my PHP code:
<?php
require_once 'PHPMailer/class.phpmailer.php';
// form url sanitizing
$php_self = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// variable initializing
$name = '';
$email = '';
$message = '';
$errors = array();
// is form send?
if( isset( $_POST['submit'] ) ) {
// validate $_POST['name']
$name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
if( '' == $name ) {
$errors[] = 'Please enter a valid name';
}
// validate $_POST['email']
$email = filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL );
if( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
$errors[] = 'Please enter a valid email';
}
// validate $_POST['message']
$message = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
if( '' == $message ) {
$errors[] = 'Please enter a valid message';
}
// If no errors
if( empty( $errors ) ) {
//values are valid, lets send an email
$mail = new PHPMailer();
// base parameters. working for me
$mail->IsSMTP(); // use SMTP
$mail->Host = "smtp.gmail.com"; // GMail
$mail->Port = 567; // I tried 465 but with no success
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true; // turn on SMTP authentication
// adjust these lines
$mail->Username = "mymail#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom($email, $name);
$mail->AddAddress('myothermail#gmail.com', 'MyName');
$mail->Subject = "Feedback Form Results";
$mail->Body = $message;
// sending
if(!$mail->Send()) {
// first error message is just for debugging. This don't generate messages a user should read
// comment this and uncommend the second message for a more user friendly message
$errors[] = "Mailer Error: " . $mail->ErrorInfo;
//$errors[] = "email couldn't be send";
// Output Sanitizing for repopulating form
$name = filter_var( $name, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$email = filter_var( $email, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$message = filter_var( $message, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
} else {
// maybe you should generate/fill a success message here
// clear fields
$name = '';
$email = '';
$message = '';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>self referencing form</title>
<link rel='stylesheet' href='http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css'/>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="button" class="title">
<h6>Contact</h6>
</div>
<div id="dropbox">
<header class="title">
<h6>Whats up?</h6>
</header>
<?php if(!empty($errors)): ?>
<ul class="error">
<li><?php echo join('</li><li>', $errors); ?></li>
</ul>
<?php endif; ?>
<div class="contact-form">
<form action="<?php echo $php_self; ?>" method="post">
<!-- input element for the name -->
<h6><img src="img/person.png" alt=""> Name</h6>
<input type="text"
name="name"
value="<?php echo $name; ?>"
placeholder="Please enter your full name here"
required>
<!-- input element for the email -->
<h6><img src="img/email.png" alt=""> E-mail</h6>
<input type="email"
name="email"
value="<?php echo $email; ?>"
placeholder="Please enter your e-mail address"
required>
<!-- input element for the message -->
<h6><img src="img/message.png" alt=""> Message</h6>
<textarea name="message" placeholder="Type your message..." required><?php echo $message; ?></textarea>
<!-- input element for the submit button -->
<input name="submit" type="submit" value="Submit">
</form>
</div>
</div>
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script src='dropbox.js'></script>
</body>
</html>
Here is my CSS code:
#import url("reset.css");
#button {
position: absolute;
top: 0;
right: 10%;
color: #eee;
z-index: 2;
width: 175px;
background: #c20000;
text-align: center;
height: 40px;
-webkit-border-radius: 0px 0px 2x 2px;
border-radius: 0px 0px 2px 2px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 1em;
text-transform: uppercase;
}
#button:hover {
background: #da0000;
cursor: pointer;
}
#button > h6{
line-height: 40px;
margin: 0px;
padding-top: 0px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 0.8em;
text-transform: uppercase;
}
#dropbox {
position: absolute;
top: 0px;
right: 10%;
color: #eee;
z-index: 1;
background: #222222;
width: 350px;
display: none;
-webkit-box-shadow: 0px 0px 16px rgba(50, 50, 50, 0.75);
-moz-box-shadow: 0px 0px 16px rgba(50, 50, 50, 0.75);
box-shadow: 0px 0px 16px rgba(50, 50, 50, 0.75);
}
#dropbox .title {
height: 40px;
background: #414141;
}
#dropbox .title > h6{
line-height: 40px;
padding-left: 58px;
margin-top: 0px;
}
#dropbox {
font-family: Tahoma, Geneva, sans-serif;
font-size: 1em;
text-transform: uppercase;
}
#dropbox .contact-form {
margin: 10px;
}
#dropbox .contact-form h6{
margin: 5px;
}
#dropbox input {
font-family: Tahoma, Geneva, sans-serif;
font-size: 0.9em;
outline: none;
border: none;
width: 320px;
max-width: 330px;
padding: 5px;
margin: 10px 0px;
background: #444444;
color: #eee;
}
#dropbox textarea {
height: 70px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 0.9em;
outline: none;
border: none;
width: 320px;
max-width: 320px;
padding: 5px;
margin: 10px 0px;
background: #444444;
color: #eee;
}
#dropbox input[type=submit] {
margin: 0px;
width: 330px;
cursor: pointer;
color: #999;
font-family: Tahoma, Geneva, sans-serif;
font-size: 0.8em;
text-transform: uppercase;
font-weight: bold;
}
#dropbox input[type=submit]:hover {
color: #eee;
background: #c20000;
}
Here is my JQuery:
$(document).ready(function () {
$('#button').mouseenter(function() {
if($('#dropbox').is(':hidden')) {
$('#dropbox').slideDown('fast', function() {
// slidedown animation
});
} else {
$('#dropdox').hide();
}
});
$('#dropbox').mouseleave(function() {
$('#dropbox').slideUp('fast', function() {
// slide back up
});
});
});
I downloaded PHP MAILER to C disc - xampp\htdocs\email\ and created a new subfolder called PHPMailer. In this folder I simply extracted the contents of the downloaded PHPMailer .zip archive.
Hope someone can help me with this so I can move on with my learning. Thank you all in advance!
This is the phpMailer example
The main difference is $mail->Port=587; and $mail->SMTPSecure='tls';
Also re-check you gmail login, sometimes it's the simple things.

Categories