Problems With Emailing Through A Submit Button - php

After going through my website the user can email me a file and a brief description to go along with it. However once the user of my website clicks the submit button, he goes to a page that says "this webpage is unavailable" and I don't get an email.
I have been using PHP and HTML for this part of my website and I don't know why it isn't working.
PHP
<?php
mail('Example#gmail.com', $_POST['Subject'], $_POST['Content']);
?>
HTML
<form method="post" action="email.php">
<input type="file">
<input type="text">
Content Goes Here
<br>
<br>
<input type="Submit">
</form>

Try to use something like libmail class for sending Emails, in most cases it fixes the problem.
If even after making it work with libmail you will get an issues, try to use SMTP along with libmail.
Cheers.
Sure, here is example of usage:
Download php_libmail class with this link http://webi.ru/base/files/tovar/php_libmail_2_1.zip
Then use this code:
<?php
include "libmail.php"; // including the class
$m= new Mail; // create instance
$m->From( "asd#asd.com" ); // from
$m->To( "who#asad.com" ); // to
$m->Subject( "Subject zzz" ); // subject
$m->Body( "Hey, pal" ); // body
$m->Cc( "copy#asd.com"); // copy of email, if need
$m->Bcc( "bcopy#asd.com"); // hidden copy of email, if need
$m->Priority(3) ; // priority of message, i think from 1 to 5
$m->Attach( "asd.gif","", "image/gif" ) ; // attachment, if need
$m->smtp_on( "smtp.asd.com", "login", "password" ) ; // via SMTP, if need
$m->Send(); // And the magic Send ;)
echo "Message body:<br><pre>", $m->Get(), "</pre>";
?>
For make functionality you need, just create simple HTML form with enctype="multipart/form-data" attribute and add any fields you want, files, inputs, texts, any of those.
And then in you PHP script accept those field values via global $_POST variable and pass accepted values into the libmail instance ;)
For accepted files use global $_FILES variable.

You can simply use PHP Mailer for sending any mail.
It is very helpful and easy way to do this kind of work.
Code would be like-
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("recepient1#example.com", "Recepient Name");
$mail->addAddress("recepient1#example.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("reply#yourdomain.com", "Reply");
//CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Here is a illustrative example given for this.

Related

How to properly send raw html code to gmail using phpmailer in php

Am sending raw code to gmail via phpmailer. when I tried to run the code, the code is formatted in the gmail instead of appearing as raw codes.
I want the code in the message body to appear the way it is in the email as per below
. In order words, how do i make the code to appear as raw codes in gmail
<html>
<script>
alert('This is your Code');
</script>
<body><p>
<b>Display as raw codes.</b>
</p>
</body></html>
below is the code
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email_subject='my codes';
$recipient_email='reciever#gmail.com';
$recipient_name='Ann';
$sender_name='john';
$mycode = "<html>
<script>
alert('This is your Code');
</script>
<body><p>
<b>Display as raw codes.</b>
</p>
</body></html>";
// Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings goes here
$mail->setFrom('nancy#gmail.com', "$sender_name");
$mail->addAddress($recipient_email, $recipient_name); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email_subject;
$mail->Body = $mycode;
//$mail->AltBody = ""; // for Plain text for non-HTML mails
$mail->send();
echo 'Message successfully sent';
} catch (Exception $e) {
echo "Message not sent. Error: {$mail->ErrorInfo}";
}
?>
$mail->isHTML(true); // Set email format to HTML
Don't do that.
If you want the message to be displayed as plain text, then don't tell the client that it should be displayed as HTML.
$mail->isHTML(false);

Send php mail from html code [duplicate]

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 5 years ago.
after I tried to get an answer to my question here, I try it with a new thread.
Little explanation:
I have a Contact-Form with 180 input fields + calculation for every field.
I want to send all the fields + the calculated value with e-mail.
Thats just an example of one of the 180 rows which i need to send with mail:
<form id="wohnzimmer" method="post" action="mailto:myemail#mymail.com">
<div style="clear: left;">
<div class="text">
DIV TEXT - ANY ARTICLE
</div>
<div class="restText">
<input id="w0" type="text" class="raumeinheitInput_x4 inputWidth" value="0" name="ARTICLENAME" /> = <span class="raumeinheitenErgebnis" id="w0g"> CALCULATED NUMBER </span>
</div>
</div>
// 179 more input-fields will follow here!!
<input type="submit" value="Send Email" />
My Calculation is working, so i just need help to send all my content via mail.
My question is:
How can i send the mail without Outlook or Thunderbird (i think i need php)
with the following Content:
Name of the article , the Number from the Input field (w0) + the calculated number (w0g)?
I hope anyone has an answer for me.
Thanks in advance.
You need to create a PHP file to handle these "server-side" actions for you. Then set the action attribute of your HTML form to this PHP page. When the HTML is submitted, the 180 input fields are then all POSTed to the PHP page inside a variable called $_POST. You can then work on that data to create the string you want and finally use the mail() function (or perhaps a pre-built emailer package that gives you a bit more control) to actually send that email.
Your new HTML
<form id="wohnzimmer" method="post" action="send_email.php">
Note:
You say you want to get the name of the article, w0 and w0g, but you have only put w0 inside an input. Only inputs, textareas and selects will be sent to the PHP script. You will need to change your HTML to make sure they are all gathered. I'd suggest using array syntax to do this:
<input type="hidden" name="article0" value="ARTICLENAME" />
<input id="w0" type="text" class="raumeinheitInput_x4 inputWidth" value="0" name="w0" /> = <input type="text" class="raumeinheitenErgebnis" name="w0g" id="w0g"> CALCULATED NUMBER </input>
<input type="hidden" name="article1" value="ARTICLENAME" />
<input id="w1" type="text" class="raumeinheitInput_x4 inputWidth" value="0" name="w1" /> = <input type="text" class="raumeinheitenErgebnis" name="w1g" id="w1g"> CALCULATED NUMBER </input>
I'm making some presumptions here about your data but that should make sense. You may want to write a PHP loop to output the data if you can. Also it might help you to use HTML input arrays to simplify things a bit.
The PHP
You'd end up with something like this very rough example:
<?php
$myString = "";
for ($x=0;$x<180;$x++) {
$tempString = $_POST['article' . $x] . $_POST['w' . $x] . $_POST['w' . $x . 'g'];
// don't forget to sanitize this data!!
$myString .= sanitize_however_you_want($tempString);
}
// now email
mail('myemail#mymail.com', 'Email Subject', $myString, 'From: you#yoursite.com' '-fyou#yoursite.com');
Read more about posting forms here: Dealing with forms
Read more about sending email here: The mail() function
Read more about HTML input arrays in this stack question
you can use a mail() (http://php.net/manual/en/function.mail.php ) but i think is better if you use a PHPMailer library (https://github.com/PHPMailer) is
very simple
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Sending PHP emails with HTML/CSS

I'm trying to send emails with PHP. I would like to add some style with CSS (no matter if inline, internal or with external file).
I've tried PHPmailer, but it fails to recognize some elements (such as body), media queries (#media) and declarations (max-width, inline-block, etc...). I'm now trying to use SwiftMailer, but online documentation doesn't mention anything about styling.
Just for sake of clarity, here's the snippet I would like to use: JsFiddle
Any ideas to send PHP emails with working HTML/CSS?
You could use a CSS inliner tool like http://templates.mailchimp.com/resources/inline-css/
to convert your 'normal' template (made like a normal HTML page) to an email-friendly template. This is needed because mail clients doesn't recognise separate elements.
I currently use a template based on the ones made available by Zurb, http://zurb.com/ink/
It also depends on the receiver's email client. Not all clients accept all css. Use a litmus test of some sort to check with different e-mail clients which css you can use. Tables often work with email clients, a lot of them don't accept divs I believe.
I use PHPMailer to send emails with PHP, and it never fails to recognize these elements (for me anyway).
To use it, i create a fonction :
function sendMail($name, $from, $message, $to, $subject)
{
$mail = new PHPMailer();
$body = "<html><head></head><body style=\"background-color: #F2F1F0;\"><p>".$message."</p></body></html>";
$mail->SetFrom($from, $name);
$mail->AddReplyTo($from, $name);
// Only if you want to send an attachment, send a variable $object to the function
//$mail->AddAttachment($object);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($to, $name);
if(!$mail->Send()) {
$result = $mail->ErrorInfo;
}
else
{
$result = "Mail sent successfully";
}
return $result;
}
And an exemple to use it :
// Preparation of the message
$message = '<p class="img"><img src="http://mywebsite.com/img/myImage.png" width="220px"></p>';
$message .= '<p class="txt">Hello Mr Dupond !</p>';
$message .= '<p class="txt">We are glad to have you among us</p>';
$message .= '<p class="txt">Se you soon on our Website !</p>';
$message = utf8_decode($message);
// variables needed
$name = "Mr Oktopuss";
$from = "contact#oktopuss.eu";
$to = "contactAddress#domain.ext";
$subject = "The subject of this mail !";
// Send the mail and receive the result
$result = sendMail($name, $from, $message, $to, $subject);
echo $result;
Maybe that could be help you

PHPMailer Not Sending Emails - Am I missing something simple?

I am using PHPMailer to send HTML emails. I first set the HTML body of the email using HTML and PHP variables after first calling ob_start.
Here's my code:
<?php
ob_start();
?>
..... a bunch of VALID HTML
<?
$body = ob_get_contents();
require_once('class.phpmailer.php');
// Send to Me
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport
try {
$mail->AddReplyTo('info#example.com', 'Company Name');
$mail->AddAddress('me#example.com', 'My Name');
$mail->SetFrom('info#example.com', 'Company Name');
$mail->Subject = "Contact Form Confirmation";
$mail->AltBody = "This is a contact form submitted through the main website.";
$mail->WordWrap = 50; // set word wrap
$mail->Body = $body;
$mail->IsHTML(true); // send as HTML
$mail->Send(); // Try to send email
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// end try
For some reason, this line:
$mail->Body = $body;
causes the email not to send. If I replace it with:
$mail->Body = "This is a test email.";
the email sends perfectly.
If my HTML contained within $body is just a valid HTML page with CSS in the head, and no Javascript or anything, why won't the email send?
Is there another way to do this? Please help!!
Turns out, for some reason, the emails were not sending if they contained a fax number. Totally strange. I tested my entire markup, and after ONLY removing a 10 digit phone number, the emails finally went through. I'm not a newbie - literally that's the only text I removed and the emails sent fine.
HAS ANYONE SEEN THIS BEFORE??

I need to attach a file to an outbound email automatically...Help

I have one form (input.html) that's completed by people working in five different divisions of this business. When the form is submitted it posts to output.php.
Output.php does a number of things:
First it displays all of the input information to the screen as a completed form. (Just to show them their completed document).
It has also created a file called unique_file.html based on two of the input fields in input.html.
Next, output.php has sent an email to one of five email groups, based upon another input field selected in input.html.
Finally (for now), I need the unique_file.html to be an attachment to the email. That is my problem.
I have found scripts for uploading files, I have found many tutorials about uploading files, but I just want to attach the unique_file.html to my outgoing email and I am not seeing how that is done.
Can someone point me in the right direction as to where to start? I am certainly missing the boat on this one and I have probably seen it and not realized it.
If you can use the Zend_Framework you can easily attach file to an email i.e.
$mail = new Zend_Mail();
$at = new Zend_Mime_Part($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->addAttachment($at);
$mail->send();
see the documentation,
You should be able to attach a file also by using Mail_Mime pear package and the normal mail function.
But I think the solution using the Zend framework is much more straight forward.
Cheers.
I find PHPMailer best for this. An example from their site:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Categories