email not sent when trying to send as HTML - email

I am able to send plain text but not HTML..
Please assist as why the mail is not being recieved..
THX in advance :)
Below is the code:
$mailBody ='<table width="100%" >
<tr>
<td>
<img src="MailerImages/img_logo.gif" alt="UrMint.com"/>
<h1 style="font:bold 20px tahoma; color:#808080;">Hi #USERNAME,</h1>
<p style="font:13px tahoma; color:#666666; line-height:25px;">
Your password for xyzacccount associated with mobile no password #MOBNO is : <strong style="color:#e65786;">#PASSWORD</strong>
<br />
If you didn\'t request a new password, let us know immediately.
<br />
This message was sent from xyz.com at your request.
</p>
<strong style="font:bold 13px tahoma; color:#666666; line-height:25px;">
Happy Learning ,
<br />
The <span style="color:#42b1d6;">Ur</span><span style="color:#e65786;">urMint</span> Team
</strong>
</table>
';
echo $mailBody;
SendEMail_fun('xyz#gmail.com', $mailBody,'sub','xyzsender','xyz#rediffmail.com');
function SendEMail_fun($strEmailTo,$strEmailBody,$strSubject,$SenderName = '',$SenderEmail='')
{
global $SwiftMessage;
$SenderName = $SenderName <> ''?$SenderName:"support#xyz.com";
$SenderEmail = $SenderEmail <> ''?$SenderEmail:"support#xyz.com";
$email_from_name = $SenderName." <".$SenderEmail.">";
$email_from_mail = $SenderEmail;
$email_to_bcc= "";
$email_to= $strEmailTo;
$email_subject= $strSubject;
$emailbody =$strEmailBody;
try
{
require_once ("lib/Swift.php");
$smtp = new Swift_Connection_SMTP("localhost", 25);
$swift = new Swift($smtp);
$message = new Swift_Message($email_subject, $emailbody, "text/html");
$message->setFrom($email_from_name);
$message->setReplyTo($email_from_mail);
$message->setBcc($email_to_bcc);
if ($swift->send($message, $email_to, $email_from_mail))
{
//echo "Message sent";
echo $SwiftMessage = 'Message Sent';
return true;
}
else
{
//echo "Message failed to send";
echo $SwiftMessage = 'Message failed to send';
return false;
}
$swift->disconnect();
return true;
}
catch (Swift_ConnectionException $e)
{
// echo "There was a problem communicating with SMTP: " . $e->getMessage();
echo $SwiftMessage = "There was a problem communicating with SMTP: " . $e->getMessage();
return false;
}
catch (Swift_Message_MimeException $e)
{
// echo "There was an unexpected problem building the email:" . $e->getMessage();
echo $SwiftMessage = "There was an unexpected problem building the email:" . $e->getMessage();
return false;
}
}

I'm not sure the exact reason yours is not sending. I've included the html email script I always use. Hope it helps.
<?php
$myFile = "email.html";
$fh = fopen($myFile, 'r');
$htmlemail = fread($fh, filesize($myFile));
fclose($fh);
$to = "YOUREMAIL#gmail.com" . ",";
//$to .= "#gmail.com" . ",";
$subject = "SUBJECT";
$message = $htmlemail;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'From: NAME <email#email.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I'm wondering if the mail order has any effect on if it will work or not:
You have
if ($swift->send($message, $email_to, $email_from_mail))
Mine:
mail($to, $subject, $message, $headers);
Worth a shot

Related

Error in mail configuration in php

I designed my website and hosted on my amazon ec2 instance and I bought my domain in godaddy (www.mydomain.com).Now I want a mail configuration in my contact form page in website.. Below its my code , I don't know where am I mistake the code?
<?php
if(isset($_REQUEST['submit'])) {
try {
$name = $_POST['name'];
echo "<script type='text/javascript'>alert('$name')</script>";
$email = $_POST['email'];
echo "<script type='text/javascript'>alert('$email')</script>";
$subject = $_POST['subject'];
echo "<script type='text/javascript'>alert('$subject')</script>";
$message = $_POST['message'];
echo "<script type='text/javascript'>alert('$message')</script>";
$response ="";
$body ="<div style='font-size:18px'>
<b>Name</b> : $name <br />
<b>Email address</b> : $email <br />
<b>Message</b> : $message <br />
</div>"
$to = "XXXXX#gmail.com";
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.phpmailer.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.smtp.php');
$mail = new PHPMailer(true);
//$mail->Host = "relay-hosting.secureserver.net"; // your SMTP Server
// echo $res;
$mail->IsSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPDebug = true;
$mail->SMTPAuth = true; // Auth Type
$mail->Port = 25;
$mail->IsSendmail();
//$mail->SMTPSecure = "ssl";
$mail->Username = "support#mydomain.com";
$mail->Password = "******";
$mail->Sender = "supportexample#mydomain.com";
$mail->From = "supportexample#mydomain.com";
$mail->AddReplyTo($email);
$mail->FromName = "Example";
$mail->AddAddress($to);
//$mail->AddAddress("desired recipient no.2 optional");
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->WordWrap = 50;
If($mail->Send()){
echo "<script type='text/javascript'>alert('Mail Send Successfully')</script>";
}
} catch (phpmailerException $e) {
echo "<script type='text/javascript'>alert('Failed')</script>";
echo $e->errorMessage();
}
}
?>
I got this error. How do solve this issue?
Could not execute: /var/qmail/bin/sendmail
You can't call random functions and expect it to all work magically.
You're setting up for SMTP with isSMTP(), but then ignoring all that by calling isSendmail(), when it looks like your server simply isn't set up for it. Why do that? Re-enable isSMTP and configure your script for your mail server correctly.
You're also requesting auth while disabling encryption; that's likely to fail on any modern server.
You're also using an old version of PHPMailer, and you've based your code on an obsolete example. Get the latest, and base your code on the examples provided.

Two contact forms, send to two different email addresses

I got two contact forms on my website. On in the contact section, and one in the careers section. I want to receive both forms in different email adresses.
<?php
include_once('class.phpmailer.php');
$mail = new PHPMailer;
$mail->setFrom('adres1','test');
$mail->Subject = 'Contactverzoek via careerspagina.';
$html = "Er is een contactverzoek gedaan via de careerspagina.<br>";
$html .= "<br>";
$html .= "<b>Naam:</b> " . $_POST['name']."<br>";
if(isset($_POST['lastname'])) {
$html .= '<b>Achternaam: </b> ' . $_POST['lastname']."<br>";
}
$html .= "<b>E-mail:</b> " . $_POST['email']."<br>";
if(isset($_POST['telefoon'])) {
$html .= '<b>Telefoon: </b> ' . $_POST['telefoon']."<br>";
}
if(isset($_POST['onderwerp'])) { //FIX NOG!!!
$html .= '<b>Onderwerp: </b> ' . $_POST['onderwerp']."<br>";
}
if(isset($_POST['Werkervaring'])) {
$html .= '<b>Werkervaring: </b> ' . $_POST['Werkervaring']."<br>";
}
if(isset($_POST['Functie'])) {
$html .= '<b>Functie: </b> ' . $_POST['Functie']."<br>";
}
if(isset($_POST['Opleidingsniveaus'])) {
$html .= '<b>Opleidingsniveaus: </b> ' . $_POST['Opleidingsniveaus']."<br>";
}
if(isset($_POST['comment'])) {
$html .= '<b>Bericht: </b> ' . $_POST['comment']."<br>";
}
$html .= "<br>";
if (isset($_FILES['resume']) &&
$_FILES['resume']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['resume']['tmp_name'],
$_FILES['resume']['name']);
}
if (isset($_FILES['letter']) &&
$_FILES['letter']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['letter']['tmp_name'],
$_FILES['letter']['name']);
}
$mail->msgHTML($html);
$mail->addAddress('adres1', 'Info ');
$mail->addBCC('adres1', 'Test ');
if(!$mail->send())
{
echo "0";
}
else
{
echo "1";
}
This is my code, and everything seems to work.
So once again. I want to keep sending this contact form to email A. But There needs to be made and else statement which sends the other contact form to email B.
i'm very new to PHP. Excuse me if i'm not clear.
One simple solution is to send a form parameter indicating which email address should be used. Then you check the POST variable for that parameter in an if statement and set the correct email address to be used.
$to = "forma#example.com";
if($_POST['form'] === "formB") {
$to = "formb#example.com";
}
$mail->addAddress($to, 'Recipient name');
Add hidden inputs in your forms named "form" and set the value to either "formA" or "formB":
<input type="hidden" name="form" value="formA">
Do not set the receiving email address in the form and use that since it can easily be abused to send spam from your server.

Php form redirect to thank you page

I'd like to use this free form template but I want it to redirect me to a thank you page upon submission.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "e-mail here";
$email_subject = "subject here";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
How can I add a code where I will be redirected to a page of my choice?
header("Location: http://page_of_your_choice.com");
Just insert this where you have the
<!-- include your own success html here -->

Unable to send HTML mail through mail()

The mail sent by the below code includes all the HTML tags. How can I get rid of that tried a few ways but still mail output is scrambled with html tags. And also need help on if any better way of sending array of elements through mail();
My code:
<?php
include('Mail.php');
include('mail\mime.php');
$errors=array();
if(empty ($_POST)===false)
{
//$product = $_POST['product'];
//$quantity = $_POST['quantity'] ;
$email="noreply#cortexonline.in";
$item1 = $_POST['product']['1'];
$item2 = $_POST['product']['2'];
$item3 = $_POST['product']['3'];
$item4 = $_POST['product']['4'];
$item5 = $_POST['product']['5'];
$quan1 = $_POST['quantity']['1'];
$quan2 = $_POST['quantity']['2'];
$quan3 = $_POST['quantity']['3'];
$quan4 = $_POST['quantity']['4'];
$quan5 = $_POST['quantity']['5'];
$message = "<html><head><title></title>
</head> <body> <table>
<tr><td>Shop id=</td>
</tr><tr><td>Product Name</td>
<td>Quantity</td></tr>
<tr><td>".$item1."</td>
<td>".$quan1."</td></tr>
<tr><td>".$item2."</td>
<td>".$quan2."</td></tr>
<tr><td>".$item3."</td>
<td>".$quan3."</td></tr>
<tr><td>".$item4."</td>
<td>".$quan4."</td><td>".$item5."</td><td>".$quan5."</td></tr>
</table></body></html>";
$header = 'From: "Quick order" <noreply#cort.in>'.PHP_EOL.
'Reply-to: <noreply#cort.in>'.PHP_EOL.
'MIME-Version: 1.0'.PHP_EOL.
'Content-Type: text/plain; charset=utf-8'.PHP_EOL.
'Content-Transfer-Encoding: 8bit'.PHP_EOL.
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
if(ctype_alpha($quan1)===true||ctype_alpha($quan2)===true||ctype_alpha($quan3)==true||ctype_alpha($quan4)===true||ctype_alpha($quan5)===true)
{
$errors[]=' Quantity cannot be alphabet /n
Cannot send request';
}
if(isset($_POST['send'])===true)
{
if (empty($errors)===false)
{
echo '<script language="javascript">alert("NOT SENT! Qunatity cannot be Alphabet")</script>';
}
else
{
mail('order#cortexonline.in','Quick order',$message,$header);
echo '<script language="javascript">alert("Your order sent!")</script>';
echo '<script language="javascript">window.location = "index.php"</script>';
}
}
else
{
echo '<script language="javascript">alert("ORDER NOT SENT! Please Try Again.")</script>';
echo '<script language="javascript">window.location = "index.php"</script>';
}
}
?>
Also tried using single quotes it is not working.
Try this:
$header = 'From: "Quick order" <noreply#cort.in>'.PHP_EOL.
'Reply-to: <noreply#cort.in>'.PHP_EOL.
'MIME-Version: 1.0'.PHP_EOL.
'Content-Type: text/html; charset=utf-8'.PHP_EOL.
'Content-Transfer-Encoding: 8bit'.PHP_EOL.
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;

Cant send e-mail by php mail on first attempt

I'm trying to send an email via SMTP using PHPMailer on my site, it works the second time I try (use submit), but the first time it says it cannot authenticate, login or password is invalid. I have searched a lot and can't find the answer, can someone help me please?
By the way I'm using JSON to get the PHP response in an alert. My host doesn't have "smtp." in front because support told me to do it this way.
This is the code:
else {
$phpmail = new PHPMailer();
$phpmail->IsSMTP(); // envia por SMTP
$phpmail->Host = "velvetwebdesign.com.br"; // SMTP servers
$phpmail->Port = "587"; // Port
$phpmail->SMTPAuth = true; // Caso o servidor SMTP precise de autenticaĆ§Ć£o
$phpmail->SMTPDebug = 1;
$phpmail->Username = "email#velvetwebdesign.com.br"; // SMTP username
$phpmail->Password = "xxxxxxx"; // SMTP password
$phpmail->IsHTML(true);
$phpmail->From = 'email#velvetwebdesign.com.br';
$phpmail->FromName = $_POST['nome'];
$phpmail->AddAddress("velvetwebdesign#velvetwebdesign.com.br");
$phpmail->AddAddress($_POST['email']);
$phpmail->Subject = 'Contato Velvet Web Design';
$phpmail->Body .= "<b>Cliente:</b> ".$_POST['nome']."<br />";
$phpmail->Body .= "<b>E-mail:</b> ".$_POST['email']."<br />";
$phpmail->Body .= "<b>Telefone:</b> ".$_POST['telefone']."<br />";
$phpmail->Body .= "<b>Assunto:</b> ".$_POST['assunto']."<br /><br />";
$phpmail->Body .= "<b>Mensagem:</b><br />".nl2br($_POST['mensagem'])."<br /><br />";
$phpmail->Body .= "Recebemos a sua mensagem, responderemos em breve.<br />";
$phpmail->Body .= "http://www.velvetwebdesign.com.br/";
$send = $phpmail->Send();
if($send){
echo "A Mensagem foi enviada com sucesso. Enviaremos uma copia para o seu e-mail tambem.";
exit;
}else{
echo "Tente novamente por favor. Erro: " .$phpmail->ErrorInfo;
exit;
}
}
?>